求助

P1957 口算练习题

Kirisamex @ 2022-08-12 11:03:41

#include <iostream>
using namespace std;

int digit(int x)
{
    if(x == 0)return 1;
    int y = x, cnt = 0;
    while(y)
    {
        y /= 10;
        cnt++;
    }
    return cnt;
}

int main()
{
    int n, a, b, flag;
    char c;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> c;
        if(c >= '0' && c <= '9')
        {
//----------------------------
        //这里有问题
            int sum = c - '0';
            cin >> a;
            for(int i = 1; i <= digit(a); i++)
            {
                sum *= 10;
            }
            a += sum;
//----------------------------
            cin >> b;
            cout << a;
            if(flag == 1)
            {
                cout << '+' << b << '=' << a + b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a + b < 0)num++;
                cout << digit(a) + digit(b) + digit(a + b) + 2 + num << endl;
            }else if(flag == 2)
            {
                cout << '-' << b << '=' << a - b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a - b < 0)num++;
                cout << digit(a) + digit(b) + digit(a - b) + 2 + num << endl;
            }else if(flag == 3)
            {
                cout << '*' << b << '=' << a * b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a * b < 0)num++;
                cout << digit(a) + digit(b) + digit(a * b) + 2 + num << endl;
            }
        }
        else if(c >= 'a' && c <= 'c')
        {
            cin >> a >> b;
            cout << a;
            if(c == 'a')
            {
                flag = 1; cout << '+' << b << '=' << a + b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a + b < 0)num++;
                cout << digit(a) + digit(b) + digit(a + b) + 2 + num<< endl;
            }
            else if(c == 'b')
            {
                flag = 2; cout << '-' << b << '=' << a - b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a - b < 0)num++;
                cout << digit(a) + digit(b) + digit(a - b) + 2 + num << endl;
            }
            else if(c == 'c')
            {
                flag = 3; cout << '*' << b << '=' << a * b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a * b < 0)num++;
                cout << digit(a) + digit(b) + digit(a * b) + 2 + num << endl;
            }
        }        
    }
    return 0;
}
/*
4
a 64 46
275 125
c 11 99
b 46 64
*/

90分代码,#2wa

看了题解区后发现,当测试点为

2
a 10 10
100 200

时,我的代码输出为

10+10=20
8
10+200=210
10

所以是a的输入出了问题。 求大佬帮忙解决一下,自己实在搞不动了


by Kirisamex @ 2022-08-12 11:04:55

我代码可能写的有点长,但是别的都不用看,就帮忙解决一下a的输入问题就行qwq,一道橙题我已经写一个多小时了


by hepp @ 2022-08-12 11:10:52

@i_will_ak_ioi 别用快读


by Kirisamex @ 2022-08-12 11:13:35

@hepp 那用啥啊(?)


by hepp @ 2022-08-12 11:16:21

@i_will_ak_ioi 用 string(吧?)


by hepp @ 2022-08-12 11:26:20

@i_will_ak_ioi

#include <bits/stdc++.h>
using namespace std;

int digit(int x)
{
    if(x == 0)return 1;
    int y = x, cnt = 0;
    while(y)
    {
        y /= 10;
        cnt++;
    }
    return cnt;
}

int main()
{
    int n, a, b, flag;
    string c;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> c;
        if(c[0] >= '0' && c[0] <= '9')
        {
            a=0;
            cin >> b;
            cout << c;
            for(int i=0;i<c.size();i++)
                a=a*10+(c[i]-'0');
            if(flag == 1)
            {
                cout << '+' << b << '=' << a + b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a + b < 0)num++;
                cout << digit(a) + digit(b) + digit(a + b) + 2 + num << endl;
            }else if(flag == 2)
            {
                cout << '-' << b << '=' << a - b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a - b < 0)num++;
                cout << digit(a) + digit(b) + digit(a - b) + 2 + num << endl;
            }else if(flag == 3)
            {
                cout << '*' << b << '=' << a * b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a * b < 0)num++;
                cout << digit(a) + digit(b) + digit(a * b) + 2 + num << endl;
            }
        }
        else if(c[0] >= 'a' && c[0] <= 'c')
        {
            cin >> a >> b;
            cout << a;
            if(c[0] == 'a')
            {
                flag = 1; cout << '+' << b << '=' << a + b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a + b < 0)num++;
                cout << digit(a) + digit(b) + digit(a + b) + 2 + num<< endl;
            }
            else if(c[0] == 'b')
            {
                flag = 2; cout << '-' << b << '=' << a - b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a - b < 0)num++;
                cout << digit(a) + digit(b) + digit(a - b) + 2 + num << endl;
            }
            else if(c[0] == 'c')
            {
                flag = 3; cout << '*' << b << '=' << a * b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a * b < 0)num++;
                cout << digit(a) + digit(b) + digit(a * b) + 2 + num << endl;
            }
        }        
    }
    return 0;
}

改好了

我交了一遍会不会算抄袭


by Kirisamex @ 2022-08-12 11:34:12

@hepp

谔谔,刚看到,不过我已经过了

感谢您

给您看看我的

#include <iostream>
using namespace std;

int digit(int x)
{
    if(x == 0)return 1;
    int y = x, cnt = 0;
    while(y)
    {
        y /= 10;
        cnt++;
    }
    return cnt;
}

int main()
{
    int n, a, b, flag;
    char c;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> c;
//----------------------------
        if(c >= '0' && c <= '9')
        {
            string s;//c = '1'
            cin >> s;//s = "00"
            a = c - '0';//a = 1
            for(int i = 0; i < s.size(); i++)
            {
                a *= 10;
                a += s[i] - '0';
            }

//----------------------------
            cin >> b;
            cout << a;
            if(flag == 1)
            {
                cout << '+' << b << '=' << a + b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a + b < 0)num++;
                cout << digit(a) + digit(b) + digit(a + b) + 2 + num << endl;
            }else if(flag == 2)
            {
                cout << '-' << b << '=' << a - b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a - b < 0)num++;
                cout << digit(a) + digit(b) + digit(a - b) + 2 + num << endl;
            }else if(flag == 3)
            {
                cout << '*' << b << '=' << a * b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a * b < 0)num++;
                cout << digit(a) + digit(b) + digit(a * b) + 2 + num << endl;
            }
        }
        else if(c >= 'a' && c <= 'c')
        {
            cin >> a >> b;
            cout << a;
            if(c == 'a')
            {
                flag = 1; cout << '+' << b << '=' << a + b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a + b < 0)num++;
                cout << digit(a) + digit(b) + digit(a + b) + 2 + num<< endl;
            }
            else if(c == 'b')
            {
                flag = 2; cout << '-' << b << '=' << a - b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a - b < 0)num++;
                cout << digit(a) + digit(b) + digit(a - b) + 2 + num << endl;
            }
            else if(c == 'c')
            {
                flag = 3; cout << '*' << b << '=' << a * b << endl;
                int num = 0;
                if(a < 0)num++; if(b < 0)num++; if(a * b < 0)num++;
                cout << digit(a) + digit(b) + digit(a * b) + 2 + num << endl;
            }
        }        
    }
    return 0;
}
/*
10+10=20
8
10+200=210
10
*/

by Kirisamex @ 2022-08-12 11:34:54

咱俩一样的/qiang


|