下载数据自测和答案一样,但提交上是爆零,请求支援!!

P1957 口算练习题

znouc1314 @ 2020-12-10 14:31:16

大佬们帮忙看一下,谢谢啦!

#include<cstdio>
#include<cstring>
#include<iostream>
#define maxn 51
using namespace std;
char rec[maxn],res[maxn][maxn],op[maxn];
int Num1(int x,int y,int i){
    int ss=0;
    for(int k=x;k<=y;++k){
        ss=ss*10+res[i][k]-'0';
    }
    return ss;
}
int main(){
    int n,len,num1,num2,num,cnt1=0,nn;
    scanf("%d\n",&n);
    for(int i=1;i<=n;++i){
        cin.getline(rec,52);
        len=strlen(rec);
        if(rec[0]=='a'||rec[0]=='b'||rec[0]=='c'){
            op[i]=rec[0];
            for(int j=2;j<len;++j){
                if(rec[j]!=' '){
                    res[i][j-2]=rec[j];
                }else res[i][j-2]=op[i];
            }
        }else{
            op[i]=op[i-1];
            for(int j=0;j<len;++j){
                if(rec[j]!=' '){
                    res[i][j]=rec[j];
                }else res[i][j]=op[i];
            }
        }   
    } 
    for(int i=1;i<=n;++i){
        len=strlen(res[i]);
        for(int j=0;j<len;++j){
            if(res[i][j]==op[i]){
                num1=Num1(0,j-1,i);
                num2=Num1(j+1,len-1,i);
                cnt1=0;
                switch(op[i]){
                    case'a': num=num1+num2; 
                            nn=num;
                            if(nn==0) cnt1++;
                            while(nn){
                                cnt1++;
                                nn/=10; 
                            } 
                            printf("%d+%d=%d\n%d\n",num1,num2,num,len+1+cnt1);
                            break;
                    case'b': num=num1-num2;
                             nn=num;
                             if(nn<0){
                                nn=(-1)*nn;
                                cnt1++;
                             }else if(nn==0) cnt1++; 
                            while(nn){
                                cnt1++;
                                nn/=10; 
                            } 
                            printf("%d-%d=%d\n%d\n",num1,num2,num,len+1+cnt1);
                            break;
                    case'c': num=num1*num2; 
                             nn=num;
                            if(nn==0) cnt1++;
                            while(nn){
                                        cnt1++;
                                        nn/=10; 
                            } 
                            printf("%d*%d=%d\n%d\n",num1,num2,num,len+1+cnt1);
                            break;
                }
            }
        }   
    }
    return 0;
} 

by ahaahaahaha @ 2020-12-11 21:41:59

一样的问题


|