一开始就想用sscanf和sprintf去实现,但是没有设计好思路,卡住了。
看了网上别人的博客,都是用的很复杂的逻辑分别判断,一言不合就七八十航代码
今天早上看了柳神的代码,发现跟自己的思路一样,开心的不得鸟。
但是,但是,但是。
算法很简单,一个小地方却坑了我很久,注意1的时候number没有s/喷血

#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    int N,legnum=0;
    double sum=0;
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        char temp[100],temp3[100];
        scanf("%s",temp);
        double temp2;
        sscanf(temp,"%lf",&temp2);
        sprintf(temp3,"%.2lf",temp2);
        int flag=0;
        for(int j=0;j<strlen(temp);j++)
            if(temp[j]!=temp3[j]) flag=1;
        if(flag|| temp2 < -1000||temp2>1000){
            printf("ERROR: %s is not a legal number\n",temp);
        }else{
            sum+=temp2;legnum++;
        }
    }
    if(legnum==1)
        printf("The average of 1 number is %.2lf",sum);
    else if(legnum>1)
        printf("The average of %d numbers is %.2lf",legnum,sum/legnum);
    else
        printf("The average of 0 numbers is Undefined");
    return 0;
}