1108 Finding Average (20分)测试点分析

只要过了题目提供的标准测试点,前两个测试点是没问题的。
如果后两个测试点没过,一定是下面的问题:

①.没有注意题目的取值范围在【-1000,1000】之间
②.注意题目只有一个元素的时候,要求输出的是 number而不是numbers
(测试点2)
③.小数点后面没有数字也是合法输入。比如 59. 是完全合法的。

附本人代码:

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
string s;
int main() {
	int N, count = 0;
	double ans=0;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		cin >> s;
		int j = 0;
		for (; j < s.length(); j++) {
			if (isalpha(s[j])) {
				printf("ERROR: %s is not a legal number\n", s.c_str());
				break;
			}
		}
		if (j == s.length()) {
			if ((s.find_first_of('.') != s.find_last_of('.')) || ((s.find('.') != string::npos) && (s.length() - 1 - s.find('.') > 2))) {
				printf("ERROR: %s is not a legal number\n", s.c_str());
			}
			else {
				if (stold(s) >= -1000 && stold(s) <= 1000) {
					count++;
					ans += stold(s);
				}
				else printf("ERROR: %s is not a legal number\n", s.c_str());
			}
		}
	}
	if (count == 0)printf("The average of 0 numbers is Undefined");
	else if(count==1)printf("The average of 1 number is %.2lf", ans / count);
	else printf("The average of %d numbers is %.2lf", count, ans / count);
	return 0;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值