全wa,求助,dev能跑出样例,太难了

P1957 口算练习题

ming0819 @ 2022-02-24 01:38:34

#include <iostream>
#include <math.h>
#include <typeinfo>
#include<math.h>
#include <cstdlib>
using namespace std;
int strisnum(string s)
{
    string::iterator it;
    it = s.begin();
    if (*it<'0' || *it>'9')
        return 0;

    return 1;
}
int main() {
    int n,count,a1=0,a2=0,flag=1;
    string s1,s;
    string::iterator it;
    char temp,f[50],k;
    cin>>n;
    cin.ignore();
    for(int i=0;i<n;i++){
        getline(cin,s1);
        if(!strisnum(s1)) temp=s1[0];
        f[i]=temp;
        s+=s1;
        s+='\n';
    }
    for(int j=0;j<s.size();j++){

        while(s[j]!='\n'){
            k=s[j];

            if(k>='a'&&k<='c') {
               temp=s[j];
               flag=1;
            }else{
                j-=2;
                flag=0;
            }
            if(temp=='a'){
                count=0;
                int c1=0;
                for(j+=2;s[j-1]!='\n'||!flag;j++){
                    flag=1;
                    k=s[j];
                    if(k==' '||s[j]=='\n') {
                        int c2=c1;
                        int k2=j;
                        for(int i=0;c2>0;c2--){
                            char t1=s[--k2];
                            int num=t1-'0';
                            a1+=pow(10,i)*num;
                            i++;
                        }
                        a2+=a1;
                        if(s[j]!='\n') {
                           c1=0;
                           a1=0;
                           cout<<'+';
                        }
                    }
                    else {
                        c1++;
                        cout<<s[j];
                    }

                    count++;
                }
                j--;
                cout<<"="<<a2<<endl;
                while(a2){
                    a2/=10;
                    count++;
                }
                cout<<count<<endl;
                a2=0;
                a1=0;                       
            }
            if(temp=='c'){
                count=0;
                int c1=0;
                int a2=1;
                for(j+=2;s[j-1]!='\n'||!flag;j++){
                    flag=1;
                    k=s[j];
                    if(k==' '||s[j]=='\n') {
                        int c2=c1;
                        int k2=j;
                        for(int i=0;c2>0;c2--){
                            char t1=s[--k2];
                            int num=t1-'0';
                            a1+=pow(10,i)*num;
                            i++;
                        }
                        a2*=a1;
                        if(s[j]!='\n') {
                           c1=0;
                           a1=0;
                           cout<<'*';
                        }
                    }
                    else {
                        c1++;
                        cout<<s[j];
                    }

                    count++;
                }
                j--;
                cout<<"="<<a2<<endl;
                while(a2){
                    a2/=10;
                    count++;
                }
                cout<<count<<endl;
                a2=0;
                a1=0;           
            }
            if(temp=='b'){
                count=0;
                int c1=0;
                int a2=0;
                int flag2=1;
                for(j+=2;s[j-1]!='\n'||!flag;j++){
                    flag=1;
                    k=s[j];
                    if(k==' '||s[j]=='\n') {
                        int c2=c1;
                        int k2=j;
                        for(int i=0;c2>0;c2--){
                            char t1=s[--k2];
                            int num=t1-'0';
                            a1+=pow(10,i)*num;
                            i++;
                        }
                        flag2--;
                        if(flag2==0){
                            a2=a1;
                        }else{
                            a2-=a1;
                        }

                        if(s[j]!='\n') {
                           c1=0;
                           a1=0;
                           cout<<'-';
                        }
                    }
                    else {
                        c1++;
                        cout<<s[j];
                    }

                    count++;
                }
                j--;
                cout<<"="<<a2<<endl;
                if(a2<0) count++;
                while(a2){
                    a2/=10;
                    count++;
                }
                cout<<count<<endl;
                a2=0;
                a1=0;           
            }
        }

    }
}

by Jasper08 @ 2022-03-16 15:14:37

@ming0819

//P1957 口算练习题

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 100010;

int nsize(int n) // 计算一个数字的大小 
{
    if (n == 0)
        return 1;

    int i = 0;
    if (n < 0)
        n = 0 - n, i++;
    while (n >= 1)
    {
        n /= 10;
        ++i;
    }
    return i;
}

int change(string a) //从字符串转为数字 
{
    int n = 0;
    for (int i = 0; i < a.size(); ++i)
        n += (a[i] - '0') * pow(10, a.size()-i-1);
    return n;
}

int main()
{
    int n;
    cin >> n;

    char op;
    for (int i = 0; i < n; ++i)
    {
        string s;
        cin >> s;

        if (s[0] >= 'a' && s[0] <= 'c')
        {
            op = s[0];
            int a, b;
            cin >> a >> b;
            if (op == 'a')
            {
                printf("%d+%d=%d\n", a, b, a+b);
                cout << 2+nsize(a)+nsize(b)+nsize(a+b) << endl;
            }
            else if (op == 'b')
            {
                printf("%d-%d=%d\n", a, b, a-b);
                cout << 2+nsize(a)+nsize(b)+nsize(a-b) << endl;
            }
            else
            {
                printf("%d*%d=%d\n", a, b, a*b);
                cout << 2+nsize(a)+nsize(b)+nsize(a*b) << endl;
            }
        }
        else
        {
            int a = change(s);
            int b;
            cin >> b;
            if (op == 'a')
            {
                printf("%d+%d=%d\n", a, b, a+b);
                cout << 2+nsize(a)+nsize(b)+nsize(a+b) << endl;
            }
            else if (op == 'b')
            {
                printf("%d-%d=%d\n", a, b, a-b);
                cout << 2+nsize(a)+nsize(b)+nsize(a-b) << endl;
            }
            else
            {
                printf("%d*%d=%d\n", a, b, a*b);
                cout << 2+nsize(a)+nsize(b)+nsize(a*b) << endl;
            }
        }
    }
    return 0;
}

|