为什么过不了啊 自测都是对滴

P1957 口算练习题

WenxinHu @ 2023-03-05 13:08:07

#include<iostream>
#include<string>
using namespace std;
int number(int n)//求数字有几位数
{
    int count = 0;
    while (n / 10 != 0)
    {
        count++;
        n /= 10;
    }
    return count+1;
}
int main()
{
    int i = 0;
    cin >> i;
    //getline(cin, i);
    string arr[50];
    cin.ignore();
    char sig='a';
    for (int m = 1; m <= i; m++)
    {
        getline(cin, arr[m]);
    }
    for (int m = 1; m <= i; m++)
    {
        if (arr[m][0] == 'a')//+
        {
            int a = 0, b = 0;
            char sig = arr[m][0];
            for (int j = 0; j < arr[m].length(); j++)
            {
                if (arr[m][j] <= '9' && arr[m][j] >= '0')
                {
                    a += arr[m][j] - '0';
                    while (arr[m][j + 1] != ' ' && (j + 1) != arr[m].length())
                    {
                        a *= 10;
                        break;
                    }
                }
                else if (arr[m][j] == ' ')
                {
                    b = a;
                    a = 0;
                    continue;
                }
            }
            cout << b << '+' << a << '=' << b + a << endl;
            cout << number(a) + number(b) + number(a + b) + 2<<endl;
        }
        else if (arr[m][0] == 'b')//-
        {
            char sig = arr[m][0];
            int a = 0, b = 0;
            for (int j = 0; j < arr[m].length(); j++)
            {
                if (arr[m][j] <= '9' && arr[m][j] >= '0')
                {
                    a += arr[m][j] - '0';
                    while (arr[m][j + 1] != ' ' && (j + 1) != arr[m].length())
                    {
                        a *= 10;
                        break;
                    }
                }
                else if (arr[m][j] == ' ')
                {
                    b = a;
                    a = 0;
                    continue;
                }
            }
            cout << b << '-' << a << '=' << b - a << endl;
            cout << number(a) + number(b) + number(a + b) + 2<<endl;

        }
        else if (arr[m][0] == 'c')//*
        {
            int a = 0, b = 0;
            char sig = arr[m][0];

            for (int j = 0; j < arr[m].length(); j++)
            {
                if (arr[m][j] <= '9' && arr[m][j] >= '0')
                {
                    a += arr[m][j] - '0';
                    while (arr[m][j + 1] != ' ' && (j + 1) != arr[m].length())
                    {
                        a *= 10;
                        break;
                    }
                }
                else if (arr[m][j] == ' ')
                {
                    b = a;
                    a = 0;
                    continue;
                }
            }
            cout << b << '*' << a << '=' << b * a << endl;
            cout << number(a) + number(b) + number(a * b) + 2<<endl;

        }
        else
        {
            if (sig == 'a')//+
            {
                int a = 0, b = 0;
                char sig = arr[m][0];
                for (int j = 0; j < arr[m].length(); j++)
                {
                    if (arr[m][j] <= '9' && arr[m][j] >= '0')
                    {
                        a += arr[m][j] - '0';
                        while (arr[m][j + 1] != ' ' && (j + 1) != arr[m].length())
                        {
                            a *= 10;
                            break;
                        }
                    }
                    else if (arr[m][j] == ' ')
                    {
                        b = a;
                        a = 0;
                        continue;
                    }
                }
                cout << b << '+' << a << '=' << b + a << endl;
                cout << number(a) + number(b) + number(a + b) + 2<<endl;
            }
            else if (sig == 'b')//-
            {
                char sig = arr[m][0];
                int a = 0, b = 0;
                for (int j = 0; j < arr[m].length(); j++)
                {
                    if (arr[m][j] <= '9' && arr[m][j] >= '0')
                    {
                        a += arr[m][j] - '0';
                        while (arr[m][j + 1] != ' ' && (j + 1) != arr[m].length())
                        {
                            a *= 10;
                            break;
                        }
                    }
                    else if (arr[m][j] == ' ')
                    {
                        b = a;
                        a = 0;
                        continue;
                    }
                }
                cout << b << '-' << a << '=' << b - a << endl;
                cout << number(a) + number(b) + number(a + b) + 2<<endl;

            }
            else if (sig == 'c')//*
            {
                int a = 0, b = 0;
                char sig = arr[m][0];

                for (int j = 0; j < arr[m].length(); j++)
                {
                    if (arr[m][j] <= '9' && arr[m][j] >= '0')
                    {
                        a += arr[m][j] - '0';
                        while (arr[m][j + 1] != ' ' && (j + 1) != arr[m].length())
                        {
                            a *= 10;
                            break;
                        }
                    }
                    else if (arr[m][j] == ' ')
                    {
                        b = a;
                        a = 0;
                        continue;
                    }
                }
                cout << b << '*' << a << '=' << b * a << endl;
                cout << number(a) + number(b) + number(a * b) + 2<<endl;
            }
        }
    }
    return 0;
}

by 已注销hUjZvzYv @ 2023-03-05 13:12:02

代码写错了

666


by Zbc20211226 @ 2023-03-19 19:33:27

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int x[n],y[n];
    char c[n];
    for(int i=0;i<n;++i){
        string a;
        cin>>a;
        if(a[0]>='a'){
            cin>>x[i]>>y[i];
            c[i]=a[0];
        }
        else{
        int l=a.size(),j=0;x[i]=0;
        while(j<l){
            x[i]=x[i]*10+(a[j]-'0');
            ++j;}
        cin>>y[i];
        c[i]=c[i-1];
        }
    }
    for(int i=0;i<n;++i){
        int s,e,f=1;
        if(c[i]=='a'){
            s=x[i]+y[i];
            e=abs(s);
            while(e>=10){
                e/=10;++f;
            }
            printf("%d+%d=%d\n",x[i],y[i],s);
        }
        else if(c[i]=='b'){
            s=x[i]-y[i];
            e=abs(s);
            while(e>=10){
                e/=10;++f;
            }
            printf("%d-%d=%d\n",x[i],y[i],s);
        }
        else{
            s=x[i]*y[i];
            e=abs(s);
            while(e>=10){
                e/=10;++f;
            }
            printf("%d*%d=%d\n",x[i],y[i],s);
        }
        if(s<0)++f;
        while(x[i]>=10){
            x[i]/=10;++f;
        }
        while(y[i]>=10){
            y[i]/=10;++f;
        }
        printf("%d\n",f+4);
    }
}

用getline会从第一行开始读,过不了


|