#4#5过不去 80分 Help

P1957 口算练习题

Acpang @ 2024-11-17 11:55:52

include<bits/stdc++.h>
using namespace std;
int b(int c)
{   
    int i ;
    if (c < 0)
        i = 1;
    else
        i = 0;
    while (c)
    {
        c = c / 10;
        i++;
    }
    return i;
}
int main()
{
    int n, x, y,t,i;
    string a;
    char d={};
    cin >> n;
    while (n--)
    {

        cin >> a, t = 2;
            if (a[0] == 'a')
            {
                d = a[0];
                cin >> x >> y;
                cout << x << "+" << y << "=" << x + y << '\n';
                t = t + b(x) + b(y) + b(x + y);
                if(n==0)
                cout << t;
                else
                cout << t << '\n';
            }
            else if (a[0] == 'b')
            {
                d = a[0];
                cin >> x >> y;
                cout << x << "-" << y << "=" << x - y << '\n';
                t = t + b(x) + b(y) + b(x - y);
                if (n == 0)
                    cout << t;
                else
                    cout << t << '\n';
            }
            else if (a[0] == 'c')
            {
                d = a[0];
                cin >> x >> y;
                cout << x << "*" << y << "=" << x * y << '\n';
                t = t + b(x) + b(y) + b(x * y);
                if (n == 0)
                    cout << t;
                else
                    cout << t << '\n';
            }
            else
            {
                int s = 0, c = 10;
                for (i = 0; a[i] != '\0'; i++)
                {
                    s = s * c + (a[i] - '0');
                }
                cin >> y;
                if (d == 'a')
                {
                    cout << s << "+" << y << "=" << s + y << '\n';
                    t = t + b(s) + b(y) + b(s + y);
                    if (n == 0)
                        cout << t;
                    else
                        cout << t << '\n';
                }
                else if (d == 'b')
                {
                    cout << s << "-" << y << "=" << s - y << '\n';
                    t = t + b(s) + b(y) + b(s - y);
                    if (n == 0)
                        cout << t;
                    else
                        cout << t << '\n';
                }
                else if (d == 'c')
                {
                    cout << s << "*" << y << "=" << s * y << '\n';
                    t = t + b(s) + b(y) + b(s * y);
                    if (n == 0)
                        cout << t;
                    else
                        cout << t << '\n';
                }

            }
    }

}

by Flora_liu @ 2024-11-27 21:33:04

#include<bits/stdc++.h>
using namespace std;
int sl(int n)//统计每个数有多少位?
{
    int ans=0;
    if(n==0)
        return 1;
    while(n)
    {
        n/=10;
        ans++;
    }
    return ans;
}
void jg(int a,int b,char s)//判断并输出。
{
    int c=sl(a)+sl(b);
    if(s=='a')
        cout<<a<<'+'<<b<<'='<<a+b<<endl<<c+sl(a+b)+2<<endl;
    else if(s=='b')
    {
        if(a-b<0)
            c++;
        cout<<a<<'-'<<b<<'='<<a-b<<endl<<c+sl(abs(a-b))+2<<endl;
    }
    else if(s=='c')
        cout<<a<<'*'<<b<<'='<<a*b<<endl<<c+sl(abs(a*b))+2<<endl;    
} 
int main()
{
    int n;
    char a[50];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        char s[5];
        cin>>s;
        int b,c;
        if(s[0]<'a')
        {
            cin>>c;
            b=atoi(s);//函数功能是把字符串转为数字。(int)
        }
        else
            cin>>b>>c;
        a[i]=s[0];
        if(s[0]>='a')
            jg(b,c,a[i]);
        else
        {
            jg(b,c,a[i-1]);
            a[i]=a[i-1];//注意这里可能会有多个连续不写'a' 'b' 'c'的样例为了保持连贯性添一句。
        }
    }
    return 0;
} 

by Acpang @ 2024-12-02 23:43:28

@Flora_liu 感谢


|