PAT甲級1108 Finding Average (20分) sscanf sprintf****

1108 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

這個題吧,有點迷,測試點3 是需要number而不是numbers,而且吧,20. 也算合格。。。。。

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
int is(string str)
{
    int start=0;
    //可以有負數
    if(str[0]=='-')
    {
        start++;
    }
    //第一個開頭的數字不能是0
    if(str[start]=='0'){

            return 0;
    }

    int netNum=0;
    int netPos=0;
    for(int i=start; i<str.size()&&netNum<2; i++)
    {
        if(str[i]=='-')
            return 0;//還又負號肯定不行了
        else if(str[i]=='.')
        {
            netNum++;
            netPos=i;
        }
        else if(!(str[i]>='0'&&str[i]<='9'))
            return 0;//不是數字肯定不行
    }
    if(netNum>1)
        return 0;
    //看看有幾個小數位
    else if(netNum!=0)
    {
        int decimalNum=str.size()-1-netPos;
        if(decimalNum<=2)
            return 1;
        else
            return 0;
    }
    else
        return 1;
}
int main()
{
    int N;
    cin>>N;
    vector<string> vec;
    string str;
    for(int i=0; i<N; i++)
    {
        cin>>str;
        double get;
        sscanf(str.c_str(),"%f",&get);
        string out;

        cout<<get;
    }
    return 0;
}

也有用sscanf解決的,感覺更簡單
參考網址

	scanf("%s",a);
    sscanf(a,"%lf",&temp);
    sprintf(b,"%.2f",temp);

上面三行代碼意思就是先把輸入保存到a裏面,然後從a讀取一個double類型的數據到temp,如果a的數據不滿足double類型,temp裏面的數據不變。
然後再把double裏面的數據以保留兩位小數的形式輸出到b這個char數組,再比較a 和 經過過濾後得到的b 是否一樣



#include<stdio.h>
#include<string.h>
int main(){
    int n,num=0;
    scanf("%d",&n);
    char a[50],b[50];
    double temp,sum=0.0;
    for(int i=0;i<n;i++){
        int flag=0;
        scanf("%s",a);
        sscanf(a,"%lf",&temp);
        sprintf(b,"%.2f",temp);
        for(int j=0;j<strlen(a);j++){
            if(a[j]!=b[j]){
                flag=1;
                break;
            }
        }
        if(flag||temp<-1000||temp>1000){
            printf("ERROR: %s is not a legal number\n",a);
        }
        else{
            sum+=temp;
            num++;
        }
    }
    if(num == 0)
        printf("The average of 0 numbers is Undefined\n");
    else if(num == 1)
        printf("The average of 1 number is %.2f\n", sum);
    else {
        printf("The average of %d numbers is %.2f\n", num, sum / num);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章