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;
}