1.题目介绍
2.考察点,难度
字符串处理类,字符串判断,难度中
3.解题代码
#include<iostream>
#include<cstring>
using namespace std;
int main(){
int n, k = 0;
scanf("%d", &n);
char s[50], t[50];
double num, sum = 0.0;
for (int i = 0; i < n; i++){
scanf("%s", s);
sscanf(s, "%lf", &num); // 从s字符串中读进与指定格式相符的数据
sprintf(t, "%.2f", num); // 字符串格式化命令,主要功能是把格式化的数据写入某个字符串中
bool islegal = true;
for (int j = 0; j < strlen(s); j++)
if (s[j] != t[j]) islegal = false;
if (islegal && num >= -1000 && num <= 1000){
k++;
sum += num;
}
else printf("ERROR: %s is not a legal number\n", s);
}
if (!k) printf("The average of 0 numbers is Undefined\n");
else if (k == 1) printf("The average of 1 number is %.2f\n", sum);
else printf("The average of %d numbers is %.2f\n", k, sum / k);
return 0;
}
4.原题地址
https://pintia.cn/problem-sets/994805342720868352/problems/994805360777347072