明明答案完全一样但全WA!(cpp)

P1957 口算练习题

LuLingchenC_Code @ 2022-08-22 15:23:34

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 6e4 + 10;
string s, t, s_a, s_b;
char c;
int q, num = 0, a, b;

int string_int(string st) {
    int index = 0, res = 0;
    while(index < st.length() ) {
        res = res*10 + (st[index]-'0');
        index++;
    }
    return res;
}

void check(string st, int &a, int &b) {
    int idx = 1;
    while(st[idx] != ' ') {
        idx++;
    }
    // printf("idx:%d\n", idx);
    s_a = st.substr(0, idx), s_b = st.substr(idx+1);
    // cout << s_a << "#" << s_b << endl;
    a = string_int(s_a);
    b = string_int(s_b);
}

void Plus(int a, int b) {
    num = printf("%d+%d=%d\n", a, b, a+b);
    // printf("%d\n", num-1);
}

void Minus(int a, int b) {
    num = printf("%d-%d=%d\n", a, b, a-b);
    // printf("%d\n", num-1);
}

void Multiple(int a, int b) {
    num = printf("%d*%d=%lld\n", a, b, a*b);
    // printf("%d\n", num-1);
}

int main() {
    scanf("%d ", &q);
    while(q--) {
        getline(cin, s);
        // printf("q:%d ", q);
        // cout << s.substr(2) << endl;
        if('a' <= s[0] && s[0] <= 'c') {
            check(s.substr(2), a, b);
            c = s[0];
        }else{
            check(s, a, b);
        }
        switch(c) {
            case 'a':Plus(a, b);break;
            case 'b':Minus(a, b);break;
            case 'c':Multiple(a, b);break;
        }
        printf("%d\n", num-1);
    }
    return 0;
}

by Nevergonna_CCF @ 2022-08-22 15:33:18

@LuLingchenC_Code 请问样例过了吗


by Nevergonna_CCF @ 2022-08-22 15:33:43

《 完 全 一 样 》


by dengyujie2020 @ 2022-08-22 15:38:09

@lmw_AK_noip 他这个样例好像是过的……


by Amiee1103 @ 2022-08-22 15:41:23

借你参考一下我的AC代码

#include<bits/stdc++.h>
using namespace std;
int n,a,b,c,d,m,j; 
char op,s[100],e[100];
int main(){
    cin>>j;
    for(int i=1;i<=j;i++){
        cin>>e;
        if(e[0]=='a'||e[0]=='b'||e[0]=='c'){
            op=e[0];
            cin>>c>>d;
        }else{
            sscanf(e,"%d",&c);
            cin>>d;
        }
        memset(s,0,sizeof(s));
        if(op=='a')//用sprintf格式化
            sprintf(s,"%d+%d=%d",c,d,c+d);
        else if(op=='b')
            sprintf(s,"%d-%d=%d",c,d,c-d);
        else if(op=='c')
            sprintf(s,"%d*%d=%d",c,d,c*d);
        cout<<s<<endl<<strlen(s)<<endl;

    } 
    return 0;
}

by LuLingchenC_Code @ 2022-09-10 09:58:04

@Amiee1103 额好的好的,谢谢谢谢


|