/*
題意:(字符串處理)
1、N個(gè)實(shí)數(shù)乌企,求平均值虑润,不要計(jì)算非法(1000正負(fù),小數(shù)點(diǎn)兩位
2加酵、非法輸出
最后輸出幾個(gè)數(shù)+平均值
也可以輸出拳喻,0個(gè)數(shù)+ 沒定義
解題
1、設(shè)置變量猪腕,n,cnt舞蔽,a,b
然后設(shè)置temp以及sum(double)
2、for循環(huán)杜甫
3码撰、輸入這個(gè)字符
scanf讀成double到a渗柿,spring讀到b
4、for循環(huán)脖岛,判定a和b是否相同朵栖,不同則flag為1,在給一個(gè)for循環(huán)柴梆,判斷temp是否在區(qū)間
learn && wrong:
1陨溅、如何判定結(jié)果才是重要的小數(shù)點(diǎn)后幾位
用字符串嗎,
2绍在、sscanf的用法门扇,sprint的用法
sscanf(str,"%d",&n);
sprintf(str,"%d",n);
而且,可以以很多格式讀入字符串
int main() {
int n;
double db;
char str[100] = "2048:3.14,hello", str2[100];
sscanf(str, "%d:%lf,%s", &n, &db, str2);
printf("n = %d, db= %.2f, str2 = %s\n", n, db, str2);
return 0;
}
int main() {
int n = 12;
double db = 3.1415;
char str[100], str2[100] = "good";
sprintf(str, "%d:%.2f,%s", n, db, str2);
printf("str = %s\n", str);
return 0;
}
*/
#include <iostream>
int main()
{
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main() {
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, "%.2f", 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 number\n", a);
continue;
}
else {
sum += temp;
cnt++;
}
}
if (cnt == 1)
printf("The average of 1 number is %.2f", sum);
else if (cnt > 1)
printf("The average of %d numbers is %.2f", cnt, sum / cnt);
else
printf("The average of 0 numbers is Undefined");
return 0;
}
}