#4 #5 这两个测试点WA的过来

P1957 口算练习题

Cloud_Ivy37 @ 2024-08-10 11:55:43

在计数时记得特殊判断0的长度为1:

int count(int x){
    if(x==0) return 1;//特判
    int res=0;
    if(x<0) res++;
    while(x!=0){
        x/=10;
        res++;
    }
    return res;
}

AC_code:

#include<iostream>
#include<sstream>
using namespace std;
int n,x,y;
char op;
string s;
int count(int x){
    if(x==0) return 1;
    int res=0;
    if(x<0) res++;
    while(x!=0){
        x/=10;
        res++;
    }
    return res;
}
void show(char op,int x,int y){
    if(op=='a') cout<<x<<"+"<<y<<"="<<x+y<<endl<<2+count(x)+count(y)+count(x+y)<<endl;
    else if(op=='b') cout<<x<<"-"<<y<<"="<<x-y<<endl<<2+count(x)+count(y)+count(x-y)<<endl;
    else cout<<x<<"*"<<y<<"="<<x*y<<endl<<2+count(x)+count(y)+count(x*y)<<endl;
}
int main(){
    cin>>n;
    getchar();
    while(n--){
        getline(cin,s);
        int flag=0;
        for(int i=0;i<s.size();i++) if(s[i]==' ') flag++;
        stringstream in(s);
        if(flag==1) in>>x>>y;
        else in>>op>>x>>y;
        show(op,x,y);
    }
    return 0;
}

by vegetable555 @ 2024-08-14 19:29:35

阴间啊


by MC_dream_tsr @ 2024-08-23 17:10:16

@Cloud_Ivy37 古来圣言皆寂寞,唯有wa四五者留其名(没想到一直没找到错误原来是没特判0


by wangmuze @ 2024-08-31 11:39:08

@Cloud_Ivy37 求关QwQQwQ count不是map里有吗,为啥不会报错哩???


by wangmuze @ 2024-08-31 11:40:06

@Cloud_Ivy37 I'm your 唯一粉丝......


by wangmuze @ 2024-08-31 11:42:35

@MC_dream_tsr 求关QwQQwQ


by wangmuze @ 2024-08-31 11:46:18

@Cloud_Ivy37 stringstream in(s); in>>x>>y; in>>op>>x>>y; 这几个是什么啊(蒟蒻提问)


by Cloud_Ivy37 @ 2024-09-08 18:07:34

@wangmuze 这个count是自定义的不会报错


by Cloud_Ivy37 @ 2024-09-08 18:16:58

@wangmuze 这题从第二行开始输入时有可能是2个,有可能是3个,用cin或者scanf不太好输入,可以按行输入,因此先使用getline按行输入,使用 stringstream 可以很方便地从字符串中分离出不同的数据项,这样输入就完成了,具体用法可以上网搜一搜


by wangmuze @ 2024-09-08 18:33:29

@Cloud_Ivy37 Thank


by wangmuze @ 2024-09-08 18:34:29

@Cloud_Ivy37 Thanks 求关


|