Average Number
Time Limit: 1 Sec Memory Limit: 128 MDescription
Please calculate the average number of the given n numbers without doing calculations of addition, multiplication, or division, or using any library fuction which is not in "stdio.h". It is guaranteed that the results are all integers and they are not less than 1 and not bigger than 100.
Input
Output
For each test case, output the average number, one per line.
Sample Input
Sample Output
HINT
If any character of "+", "*", "/" or any name of library fuction which is not in "stdio.h" appers in your code, you will get "Invalid Word" and your code will not be accepted.
分析:这个题目不难,关键是思路,不能用加法,乘法,除法,那为什么没说不能用减法。所以这个题的关键就是把所有的运算都转化为减法。
#include <stdio.h>
int main()
{
int n,m,i,j;
int temp,sum;
double avr;
scanf("%d",&n);
for(i=1;i<=n;i=(i-(-1)))
{
scanf("%d",&m);
for(j=1,sum=0;j<=m;j=(j-(-1)))
{
scanf("%d",&temp);
sum = sum - (-temp);
}
for(avr=0;sum!=0;avr=(avr-(-1)))
{
sum = sum - m;
}
printf("%.0lf\n",avr);
}
return 0;
}
本文介绍了一种在不使用加法、乘法或除法的情况下计算一组整数平均值的方法,并提供了一个C语言实现的例子。

被折叠的 条评论
为什么被折叠?



