从键盘上输出数字求他们的平均值输出-1结束 最好能给我个N-S图谢谢各位大虾
int x,sum=0,n=0;do{ scanf("%d",&x); sum=sum+x; n++;} while(x!=-1);printf("average=%d\n",(sum+1)/(n-1));
int n=0;float x=0,sum=0;do{ n++; sum=sum+x; scanf("%f",&x); } while(x!=-1);printf("average=%f\n",sum/n);
这样不是更好
[此贴子已经被作者于2006-11-4 9:02:52编辑过]
应该是 输入
[CODE][/CODE]#include <stdio.h>#include <stdlib.h>
int main(){ double a, sum = 0, i = 0;
scanf("%lf", &a); while(a != -1) { sum += a; i++; scanf("%lf", &a); } printf("%.1lf\n", sum / i);
return 0;}