思路:可以使用while循环来实现多个输入,输入ctrl+z+enter结束输入,并统计输入的个数和输入数的累加和,最后把累加和除以输入的个数就是平均值。
参考代码:
#include "stdio.h"
int main() {
float sum=0.0,i;
int n=0;
while(scanf("%f",&i)!=EOF){
sum+=i;
n++;
}
printf("累加和为:%.2f,平均值为:%.2f",sum,sum/n);
return 0;
}
#include
void main()
{
int i;
int n = 0;
float sum = 0;
float avg = 0.0;
printf("please input some number\n");
while(i != 0)
{
scanf("%d",&i);
sum += i;
n++;
}
printf("sum = %.2f,avg = %.2f",sum,sum/ 1.0 / n );
}
//解释一下,每输入一个数按回车键,输入0结束
#include
int main()
{
int i, n = 0;
float sum = 0,avg = 0;
while((scanf("%d",&i))!=EOF)
{
sum += i;
n++;
}
printf("sum = %f,avg = %f",sum,sum*1.0 / n );
}
123