为什么全RE呀麻烦各位大佬帮忙看看

P1957 口算练习题

Alan_M_Turing @ 2020-08-05 17:15:30

样例能过,测试点貌似也能过,为什么全RE QAQ

#include<bits/stdc++.h>

using namespace std;

int n;
string s;
char last;
int a,b;
int c;
int len;

int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        string ans;
        a=0;
        b=0;
        c=0;
        int j=0;
        fflush(stdin);
        getline(cin,s);
        len=s.length();
        if(s[0]>='a'&&s[0]<='z'){
            last=s[0];
            j=2;
        }else{
            j=0;
        }
        for(;;j++){
            if(s[j]==' '){
                break;
            }else{
                ans=ans+s[j];
                a=a*10+(s[j]-'0');
            }
        }
        switch(last){
            case 'a':{
                ans=ans+'+';
                break;
            }
            case 'b':{
                ans+='-';
                break;
            }
            case 'c':{
                ans+='*';
                break;
            }
        }
        j++;
        for(;j<len;j++){
            if(s[j]==' '){
                break;
            }else{
                ans+=s[j];
                b=b*10+(s[j]-'0');
            }
        }
        int la,cc,lc;
        switch(last){
            case 'a':{
                c=a+b;
                break;
            }
            case 'b':{
                c=a-b;
                break;
            }
            case 'c':{
                c=a*b;
                break;
            }
        }
        ans+='=';
        la=ans.length();
        lc=0;
        if(c>=0){
            cc=c;
        }else{
            cc=-c;
            lc++;
        }
        while(cc>0){
            cc/=10;
            lc++;
        }
        cout<<ans<<c<<endl;
        cout<<la+lc<<endl;
    }
    return 0;
} 

by Eason_AC @ 2020-08-05 17:22:12

借你的帖求助一下我的50分代码QwQ:

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

int n;
char a[7], b[7], c[7];

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%s%s", a, b);
        if(a[0] >= '0' && a[0] <= '9') {
            int num1 = 0, num2 = 0, lena = strlen(a), lenb = strlen(b);
            for(int i = 0; i < lena; ++i)
                num1 = num1 * 10 + a[i] - '0';
            for(int i = 0; i < lenb; ++i)
                num2 = num2 * 10 + b[i] - '0';
            printf("%d+%d=%d\n", num1, num2, num1 + num2);
            int num3 = num1 + num2, leng = 0;
            while(num3) {
                leng++;
                num3 /= 10;
            }
            printf("%d\n", lena + lenb + leng + 2);
        } else {
            scanf("%s", c);
            if(a[0] == 'a') {
                int num1 = 0, num2 = 0, len1 = strlen(b), len2 = strlen(c);
                for(int i = 0; i < len1; ++i)
                    num1 = num1 * 10 + b[i] - '0';
                for(int i = 0; i < len2; ++i)
                    num2 = num2 * 10 + c[i] - '0';
                printf("%d+%d=%d\n", num1, num2, num1 + num2);
                int num3 = num1 + num2, leng = 0;
                while(num3) {
                    leng++;
                    num3 /= 10;
                }
                printf("%d\n", len1 + len2 + leng + 2);
            } else if(a[0] == 'b') {
                int num1 = 0, num2 = 0, len1 = strlen(b), len2 = strlen(c);
                for(int i = 0; i < len1; ++i)
                    num1 = num1 * 10 + b[i] - '0';
                for(int i = 0; i < len2; ++i)
                    num2 = num2 * 10 + c[i] - '0';
                printf("%d-%d=%d\n", num1, num2, num1 - num2);
                int num3 = num1 - num2, leng = 0;
                if(num3 < 0)    leng++;
                while(num3) {
                    leng++;
                    num3 /= 10;
                }
                printf("%d\n", len1 + len2 + leng + 2);
            } else {
                int num1 = 0, num2 = 0, len1 = strlen(b), len2 = strlen(c);
                for(int i = 0; i < len1; ++i)
                    num1 = num1 * 10 + b[i] - '0';
                for(int i = 0; i < len2; ++i)
                    num2 = num2 * 10 + c[i] - '0';
                printf("%d*%d=%d\n", num1, num2, num1 * num2);
                int num3 = num1 * num2, leng = 0;
                while(num3) {
                    leng++;
                    num3 /= 10;
                }
                printf("%d\n", leng + len1 + len2 + 2);
            }
        }
    }
    return 0;
}

by xixike @ 2020-08-05 17:44:28

@Alan_M_Turing

fflush(stdin);

您的这行代码我看不懂,没用过QWQ


by xixike @ 2020-08-05 17:51:14

@Eason_AC

这是AC代码,您的错误在于

  1. 当输入只有两个数时,进行的运算是上一次的运算,而不是 a (加法运算)。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <iostream>
using namespace std;

int n, flag;
char a[10], b[10], c[10];

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%s%s", a, b);
        if(a[0] >= '0' && a[0] <= '9') {
            int num1 = 0, num2 = 0, lena = strlen(a), lenb = strlen(b);
            for(int i = 0; i < lena; ++i)
                num1 = num1 * 10 + a[i] - '0';
            for(int i = 0; i < lenb; ++i)
                num2 = num2 * 10 + b[i] - '0';
            int num3,leng;
            if(flag==1) {
                printf("%d+%d=%d\n", num1, num2, num1 + num2);
                num3 = num1 + num2, leng = 0;
            }
            if(flag==2) {
                printf("%d-%d=%d\n", num1, num2, num1 - num2);
                num3 = num1 - num2, leng = 0;
            }
            if(flag==3) {
                printf("%d*%d=%d\n", num1, num2, num1 * num2);
                num3 = num1 * num2, leng = 0;
            }   
            if(num3==0) leng = 1;
            else {
                int temp = abs(num3);
                while(temp) {
                    leng++;
                    temp /= 10;
                }
                if(num3<0) leng++;
            }
            printf("%d\n", lena + lenb + leng + 2);
        } else {
            scanf("%s", c);
            if(a[0] == 'a') {
                flag = 1;
                int num1 = 0, num2 = 0, len1 = strlen(b), len2 = strlen(c);
                for(int i = 0; i < len1; ++i)
                    num1 = num1 * 10 + b[i] - '0';
                for(int i = 0; i < len2; ++i)
                    num2 = num2 * 10 + c[i] - '0';
                printf("%d+%d=%d\n", num1, num2, num1 + num2);
                int num3 = num1 + num2, leng = 0;
                if(num3==0) leng = 1;
                else {
                    while(num3) {
                        leng++;
                        num3 /= 10;
                    }
                }
                printf("%d\n", len1 + len2 + leng + 2);
            } else if(a[0] == 'b') {
                flag = 2;
                int num1 = 0, num2 = 0, len1 = strlen(b), len2 = strlen(c);
                for(int i = 0; i < len1; ++i)
                    num1 = num1 * 10 + b[i] - '0';
                for(int i = 0; i < len2; ++i)
                    num2 = num2 * 10 + c[i] - '0';
                printf("%d-%d=%d\n", num1, num2, num1 - num2);
                int num3 = num1 - num2, leng = 0;
                if(num3==0) leng = 1;
                else {
                    int temp = abs(num3);
                    while(temp) {
                        leng++;
                        temp /= 10;
                    }
                    if(num3<0) leng++;
                }
                printf("%d\n", len1 + len2 + leng + 2);
            } else {
                flag = 3;
                int num1 = 0, num2 = 0, len1 = strlen(b), len2 = strlen(c);
                for(int i = 0; i < len1; ++i)
                    num1 = num1 * 10 + b[i] - '0';
                for(int i = 0; i < len2; ++i)
                    num2 = num2 * 10 + c[i] - '0';
                printf("%d*%d=%d\n", num1, num2, num1 * num2);
                int num3 = num1 * num2, leng = 0;
                if(num3==0) leng = 1;
                else {
                    while(num3) {
                        leng++;
                        num3 /= 10;
                    }
                }
                printf("%d\n", leng + len1 + len2 + 2);
            }
        }
    }
    return 0;
}

by xixike @ 2020-08-05 17:52:36

@Eason_AC flag 就是用来判断上一次进行的什么运算


by Eason_AC @ 2020-08-05 21:22:31

@xixike 哦好的我再调一下


by Alan_M_Turing @ 2020-08-05 21:59:17

@xixike 因为一开始如果没有这行代码就会把第一行的换行符当作一次操作,于是我就想刷新一下标准输入


|