QwQ,70分,救急救!改不对啊!

P1957 口算练习题

Max6700 @ 2023-05-06 22:03:38

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

int a,b,c;
int n;
char c1;
string c2;

int ws(int n)
{
    int cnt=0;
    while(abs(n)>0)
    {
        n/=10;
        cnt++;
    }
    return cnt;
}

void pd()
{
    if(c2=="a")
    {
        cout<<a<<"+"<<b<<"="<<a+b<<"\n";
        c=a+b;
        cout<<ws(a)+ws(b)+ws(c)+2<<"\n";
    }
    else if(c2=="b")
    {
        cout<<a<<"-"<<b<<"="<<a-b<<"\n";
        c=a-b;
        if(c>=0)
            cout<<ws(a)+ws(b)+ws(c)+2<<"\n";
        else
            cout<<ws(a)+ws(b)+ws(c)+3<<"\n";

    }
    else if(c2=="c")
    {
        cout<<a<<"*"<<b<<"="<<a*b<<"\n";
        c=a*b;
        cout<<ws(a)+ws(b)+ws(c)+2<<"\n";
    }
}
int main(){

    cin>>n;

    for(int i=1;i<=n;i++)
    {
        cin>>c1;
        if(c1!='a' && c1!='b' && c1!='c')
        {
            cin>>a;
            int x=int(c1-'0');
            for(int i=1;i<=ws(a);i++)
            {
                x*=10;
            }
            a+=x;
            cin>>b;
            pd();
        }
        else
        {
            cin>>a>>b;
            c2=c1;
            pd();    
        }

    }
    return 0;
}

by Max6700 @ 2023-06-03 15:50:30

二改……

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

int a,b,c;
int n;
char c1;
char c2;

int ws(int n)
{
    int cnt=0;
    if(n<0)
        cnt=1;

    n=abs(n);
    while(n)
    {
        n/=10;
        cnt++;
    }
    return cnt;
}

void pd()
{
    if(c2=='a')
    {
        cout<<a<<"+"<<b<<"="<<a+b<<"\n";
        c=a+b;
        cout<<ws(a)+ws(b)+ws(c)+2<<"\n";
    }
    else if(c2=='b')
    {
        cout<<a<<"-"<<b<<"="<<a-b<<"\n";
        c=a-b;
        cout<<ws(a)+ws(b)+ws(c)+2<<"\n";
    }
    else if(c2=='c')
    {
        cout<<a<<"*"<<b<<"="<<a*b<<"\n";
        c=a*b;
        cout<<ws(a)+ws(b)+ws(c)+2<<"\n";
    }
}
int main(){

    cin>>n;

    for(int i=1;i<=n;i++)
    {
        cin>>c1;
        if(c1!='a' && c1!='b' && c1!='c')
        {
            cin>>a;
            int x=int(c1-'0');
            for(int i=1;i<=ws(a);i++)
            {
                x*=10;
            }
            a+=x;
            cin>>b;
            pd();
        }
        else
        {
            cin>>a>>b;
            c2=c1;
            pd();    
        }

    }
    return 0;
}

by wYYSZLwSSY @ 2023-06-03 17:32:24

@Max6700 hack:

2
a 1 2
1 2 

by MrTerry @ 2023-06-03 17:36:57

实际上,你可以用一些STL库中的函数,例如atoi();

int num1 = atoi(T.c_str());
//T为string类型,若T为1234,则num1为1234。
//atoi可以将string转为int

再来一些C语言风格的sprintf和strlen以及字符数组

char ans[20];
if (his_opt == 'a') sprintf(ans, "%d+%d=%d", num1, num2, num1 + num2);
if (his_opt == 'b') sprintf(ans, "%d-%d=%d", num1, num2, num1 - num2);
if (his_opt == 'c') sprintf(ans, "%d*%d=%d", num1, num2, num1 * num2);
cout << ans << endl << strlen(ans) << endl;

这道题重点就完成了 下面粘code:

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

int main()
{
    char his_opt;
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        string T;
        int num1, num2;
        cin >> T;
        if (T[0] >= 'a' && T[0] <= 'c')
        {
            his_opt = T[0];
            cin >> num1 >> num2;
        }
        else
        {
            num1 = atoi(T.c_str());
            cin >> num2;
        }
        char ans[20];
        if (his_opt == 'a') sprintf(ans, "%d+%d=%d", num1, num2, num1 + num2);
        if (his_opt == 'b') sprintf(ans, "%d-%d=%d", num1, num2, num1 - num2);
        if (his_opt == 'c') sprintf(ans, "%d*%d=%d", num1, num2, num1 * num2);
        cout << ans << endl << strlen(ans) << endl;
    }
    return 0;
}

by Max6700 @ 2023-06-03 18:58:32

@wYYSZLwSSY 啊……


by Max6700 @ 2023-06-03 18:59:12

@MrTerry emm……thx,看来还是要学内置函数啊![感叹]


|