1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <cstring> #include <iostream> using namespace std; int () { int n, cnt = 0; char a[50], b[50]; double temp, sum = 0.0; cin >> n; for(int i = 0; i < n; i++) { scanf("%s", a); sscanf(a, "%lf", &temp); sprintf(b, "%.2lf",temp); int flag = 0; for(int j = 0; j < strlen(a); j++) { if(a[j] != b[j]) flag = 1; } if(flag || temp < -1000 || temp > 1000) { printf("ERROR: %s is not a legal numbern", a); continue; } else { sum += temp; cnt++; } } if(cnt == 1) { printf("The average of 1 number is %.2lf", sum); } else if(cnt > 1) { printf("The average of %d numbers is %.2lf", cnt, sum / cnt); } else { printf("The average of 0 numbers is Undefined"); } return 0; }
|