用do-while语句求平均值
#include<stdio.h>
int main()
{
int number;
int sum = 0;
int count = 0;
do{
scanf("%d",&number);
//-1用来终止循环
if (number != -1){
sum += number;
count++;
}
} while (number != -1);
printf("%f\n", 1.0*sum/count);
return 0;
}
用while语句求平均值
#include<stdio.h>
int main()
{
int number;
int sum = 0;
int count = 0;
scanf("%d",&number);
//-1用来终止循环
while (number != -1){
sum += number;
count++;
scanf("%d",&number);
}
printf("%f\n", 1.0*sum/count);
return 0;
}