90求助!

P1957 口算练习题

ECNUAT_LZX @ 2023-08-12 12:47:06

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
char last;
int calc(char c,int a,int b){
    if(c=='a')return a+b;
    if(c=='b')return a-b;
    return a*b;
}
int ws(int x){
    int cnt=0;
    if(x<=0)cnt++;
    x=abs(x);
    while(x){x/=10;cnt++;}
    return cnt;
}
char tos(char ch){
    if(ch=='a')return '+';
    if(ch=='b')return '-';
    return '*';
}
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        char c;
        cin>>c;
        if(c>='0'&&c<='9'){
            int a,b;
            cin>>a>>b;
            a+=(c-'0')*pow(10,(ws(a)));
            cout<<a<<tos(last)<<b<<"="<<calc(last,a,b)<<'\n';
            cout<<ws(a)+ws(b)+ws(calc(last,a,b))+2<<'\n';
        }
        else{
            int a,b;
            cin>>a>>b;
            cout<<a<<tos(c)<<b<<"="<<calc(c,a,b)<<'\n';
            cout<<ws(a)+ws(b)+ws(calc(c,a,b))+2<<'\n';
            last=c;
        }
    }
    return 0;
}

by ShwStone @ 2023-08-12 13:13:11

hack:

2
a 3 3
3 3

by ShwStone @ 2023-08-12 13:14:20

@TLE_AC_WA_RE 如果无字母开头是一位数这一段就会有问题:

if(c>='0'&&c<='9'){
            int a,b;
            cin>>a>>b;

by ECNUAT_LZX @ 2023-08-12 13:24:20

Orz


|