首页 > 学院 > 开发设计 > 正文

1054. 求平均值 (20) PAT乙级

2019-11-11 02:28:55
字体:
来源:转载
供稿:网友

传送门

#include<stdio.h>#include<string.h>#include<stdlib.h>char str[110][1100];int main(){ int n; int count=0; double average=0; scanf("%d",&n); for(int i=0;i<n;i++){ bool isNumber=true; int dot=0; int dotnum=0; scanf("%s",str[i]); int len=strlen(str[i]); for(int j=0;j<len;j++){ if(dot==1) dotnum++; if(str[i][j]=='-'&&j!=0){ isNumber=false; break; } if(str[i][j]=='.'){ dot++; } else{ if((str[i][j]<'0'||str[i][j]>'9')&&(str[i][j]!='-')){ isNumber=false; break; } } if(dot>=2||dotnum>=3){ isNumber=false; break; } } double num=atof(str[i]); if(num>1000||num<-1000){ isNumber=false; } if(isNumber){ count++; average+=num; } else{ PRintf("ERROR: %s is not a legal number/n",str[i]); } } if(count==0) printf("The average of 0 numbers is Undefined"); else if(count==1) printf("The average of %d number is %.2f",count,average); else printf("The average of %d numbers is %.2f",count,average/count);}
上一篇:元数据和事务

下一篇:面向对象基础

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表