[PAT 乙级]1054 求平均值 (20 分) (字符串处理)(内含测试点 3 ,4) 2021-07-31


在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第一次代码

坑太多,要学习的东西太多了。
写judge(string s)不用加&,因为不需要修改s,这里只是遍历一下有无字母。

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int judge(string s) {
    int flag = 0;
        for (int i = 0; i < s.length(); i++) {
            if (isalpha(s[i])) {
                flag = 1;
            }
        }
    return flag;
}
int main() {
    int n;
    cin >> n;
    vector<string> v;
    string temp;
    int count = 0;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        /*可以不写这段代码
        if(temp=="."){
            cout<<"ERROR: "<<"."<<" is not a legal number"<<endl;
            break;
        }*/
        if (judge(temp)==1) {//这里不周道,原先这里是用了stoi(),忘记了double的情况
            cout << "ERROR: " << temp << " is not a legal number" << endl;
        }
        else {
            double num;//这里不周,原先这里是用了stoi(),忘记了double的情况
            num=atof(const_cast<const char*>(temp.c_str()));
            if ( num>1000||num<-1000) {
                cout << "ERROR: " << temp << " is not a legal number" << endl;//stoi(temp)是不周的
            }
            else {
                int pos;
                pos=temp.find(".");
                if (pos == -1) {//整数
                    v.push_back(temp);
                    count++;
                }
                else {
                    
                    if (temp.length()-1 - pos > 2) {
                        cout << "ERROR: " << temp << " is not a legal number" << endl;
                    }
                    else {//两位小数
                        v.push_back(temp);
                        count++;
                    }
                }
            }
        }
    }

    if (count == 1) {
        double p=atof(const_cast<const char*>(v[0].c_str()));
        printf("The average of 1 number is %.2f\n", p);
               
    }
    else if(count > 1){
        double ans=0;
        for (auto it = v.begin(); it != v.end(); it++) { 
            ans += atof(const_cast<const char*>((*it).c_str()));
        }
        double anss = ans / count;
        printf("The average of %d numbers is %.2f\n", count, anss);
    }
    else{
        cout << "The average of 0 numbers is Undefined" << endl;
    }
    return 0;
}

柳神的代码

分析:使⽤sscanf和sprintf函数~
sscanf() – 从⼀个字符串中读进与指定格式相符的数据
sprintf() – 字符串格式化命令,主要功能是把格式化的数据写⼊某个字符串中

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

二刷

有两个坑,下面注释。
代码还是太复杂,学柳神的代码。

#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool judg(string s){
    bool flag=false;
    if(s[0]=='.') return false;
    if((s[0]=='+'||s[0]=='-')&&s.size()==1) return false;
    for(int i=0;i<s.size();i++) if(isalpha(s[i])) flag=true;
    if(flag==false) {
        if(stod(s) < -1000 || stod(s) > 1000) return false;//这里是大于以及小于
        int x=0;
        for(int i=0;i<s.size();i++) {if(s[i]=='.') x++;}
        string t;
        if(s.find('.')!=s.npos) {
            int w=s.find('.'); 
            t=s.substr(w+1);
        }
        if(x>=2) return false;
        else if(t.size()>2) return false;
        else return true;
    }
    return false;
}
int main(){
    vector<string> v;
    int n,count=0;
    double sum=0.0;
    cin>>n;
    for(int i=0;i<n;i++){
         string temp;
        cin>>temp;
        if(!judg(temp)) printf("ERROR: %s is not a legal number\n",temp.c_str());
        else{
            count++;
            v.push_back(temp);
        }
    }
    if(count==0) printf("The average of 0 numbers is Undefined\n");
    else if(count==1) printf("The average of 1 number is %.2f\n",stod(v[0]));//只有一个数时候也要输出%.2f
    else {
        for(auto it=v.begin();it!=v.end();it++) sum+=stod((*it));
        printf("The average of %d numbers is %.2f\n",v.size(),(sum/v.size()));
    }
    return 0;
}

测试点3,4

测试点3:
输入:

3
aaa -9999 5

标准输出:

ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 1 number is 5.00

测试点4:
输入:

2
1000 1dsere7

标准输出:

ERROR: 1dsere7 is not a legal number
The average of 1 number is 1000.00
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值