2596 - C二级辅导-计负均正
从键盘输入任意20个整型数,统计其中的负数个数并求所有正数的平均值。
保留两位小数
Input
Output
Examples
Input
1 2 3 4 5 6 7 8 9 10 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10
Output
10 5.50
Solution C
#include <stdio.h> #define N 20 int main() { int num, amount = 0, sum = 0, amount_zero = 0; for (int i = 0; i < N; i ++) { scanf("%d", &num); if (num > 0) { sum += num; }else if (num < 0) { amount ++; }else { amount_zero ++; } } printf("%d\n%.2f\n", amount, (float)sum / (N - amount - amount_zero)); return 0; }
Solution C++
#include <stdio.h> double a[100000]; int main() { double n; while(scanf("%lf",&n)!=EOF) { a[0]=n; int ansz=0,ansf=0; double sum=0; if(n<0) ansf++; else { ansz++; sum+=n; } for(int i=1;i<=19;i++) { scanf("%lf",&a[i]); if(a[i]<0) ansf++; else { ansz++; sum+=a[i]; } } printf("%d\n%.2lf",ansf,sum/ansz); } return 0; }