PAT A1108 Finding Average (20 分)——字符串,字符串转数字

The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [1000,1000] and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (100). Then N numbers are given in the next line, separated by one space.

Output Specification:

For each illegal input number, print in a line ERROR: X is not a legal number where X is the input. Then finally print in a line the result: The average of K numbers is Y where K is the number of legal inputs and Y is their average, accurate to 2 decimal places. In case the average cannot be calculated, output Undefined instead of Y. In case K is only 1, output The average of 1 number is Y instead.

Sample Input 1:

7
5 -3.2 aaa 9999 2.3.4 7.123 2.35

Sample Output 1:

ERROR: aaa is not a legal number
ERROR: 9999 is not a legal number
ERROR: 2.3.4 is not a legal number
ERROR: 7.123 is not a legal number
The average of 3 numbers is 1.38

Sample Input 2:

2
aaa -9999

Sample Output 2:

ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined
 
 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <set>
 4 #include <string.h>
 5 #include <vector>
 6 #include <math.h>
 7 #include <queue>
 8 #include <iostream>
 9 #include <string>
10 using namespace std;
11 const int maxn = 1011;
12 int n;
13 double res=0;
14 bool isint(char c){
15     if(c>='0' && c<='9')return true;
16     else return false;
17 }
18 bool isnum(string s){
19     res=0;
20     if(s[0]!='-' && s[0]!='+' && (s[0]<'0' || s[0]>'9')) return false;
21     int flag=1;
22     if(s[0]=='-'){
23         flag=-1;
24         s.erase(0,1);
25     }
26     else if(s[0]=='+'){
27         s.erase(0,1);
28     }
29     int fp=0,np=0;
30     for(int i=0;i<s.length();i++){
31         if(isint(s[i])){
32             res=res*10+s[i]-'0';
33         }
34         else if(s[i]=='.' && np==0){
35             np++;
36             fp=i;
37         }
38         else{
39             flag=0;
40             break;
41         }
42     }
43     if(flag==0)return false;
44     if(np==1){
45         int xiao = s.length()-fp-1;
46         if(xiao>2) return false;
47         res = res / pow(10,xiao);
48     }
49     if(res>1000) return false;
50     //printf("%f %d
",res,flag);
51     res = res*flag;
52     //printf("%f %d
",res,flag);
53     return true;
54 }
55 
56 int main(){
57     scanf("%d",&n);
58     int cnt=0;
59     double total=0;
60     for(int i=1;i<=n;i++){
61         string s;
62         cin>>s;
63         //res=0;
64         if(isnum(s)){
65             cnt++;
66             total = total + res;
67             //printf("cnt: %d total: %f
",cnt,total);
68         }
69         else{
70             printf("ERROR: %s is not a legal number
",s.c_str());
71         }
72     }
73     if(cnt==0)printf("The average of 0 numbers is Undefined
");
74     else if(cnt!=1)printf("The average of %d numbers is %.2f
",cnt,total/cnt);
75     else printf("The average of %d number is %.2f
",cnt,total/cnt);
76 }
View Code

注意点:字符串转数字的判断,按条件一个个判断就好了,注意输入输出时的double要写对。测试点3是只有一个数的情况,1个数时number没有s,没注意所以错了

---------------- 坚持每天学习一点点
原文地址:https://www.cnblogs.com/tccbj/p/10452877.html