c++本地下载数据无误,洛谷0分蒟蒻求助

P1957 口算练习题

SilVeR__WolF @ 2024-02-23 20:39:12

代码有点长而且啰嗦,恳请各位大佬给点意见。

#include<bits/stdc++.h>
using namespace std;
#define equal equaltion
int n;
char p;
string equal;
int main(){
    scanf("%d\n",&n);
    for(int i=1;i<=n;i++)
    {
        getline(cin,equal);
        if(equal[0]>='a'&&equal[0]<='z')
        {
            p=equal[0];
            int sa=0,sb=0,j=0;
            while(equal[j+2]!=' ')
            {
                sa=sa*10+equal[j+2]-'0';
                j++;
            }
            int d=0;
            while(d+j+3<equal.size())
            {
                sb=sb*10+equal[d+j+3]-'0';
                d++;
            }
            int h=0,t;
            switch(equal[0])
            {
                case 'a':
                    cout<<sa<<"+"<<sb<<"="<<sa+sb<<endl;
                    t=sa+sb;
                    while(t)
                    {
                        h++;
                        t/=10;
                    }
                    cout<<d+j+h+2<<endl;
                    break;
                case 'b':
                    cout<<sa<<"-"<<sb<<"="<<sa-sb<<endl;
                    t=sa-sb;
                    while(t)
                    {
                        h++;
                        t/=10;
                    }
                    if(sa-sb<0)
                        h++;
                    cout<<d+j+h+2<<endl;
                    break;
                case 'c':
                    cout<<sa<<"*"<<sb<<"="<<sa*sb<<endl;
                    t=sa*sb;
                    while(t)
                    {
                        h++;
                        t/=10;
                    }
                    cout<<d+j+h+2<<endl;
                    break;
            }
        }
        else
        {
            int sa=0,sb=0,j=0;
            while(equal[j]!=' ')
            {
                sa=sa*10+equal[j]-'0';
                j++;
            }
            int d=0;
            while(d+j+1<equal.size())
            {
                sb=sb*10+equal[d+j+1]-'0';
                d++;
            }
            int h=0,t;
            switch(p)
            {
                case 'a':
                    cout<<sa<<"+"<<sb<<"="<<sa+sb<<endl;
                    t=sa+sb;
                    while(t)
                    {
                        h++;
                        t/=10;
                    }
                    cout<<d+j+h+2<<endl;
                    break;
                case 'b':
                    cout<<sa<<"-"<<sb<<"="<<sa-sb<<endl;
                    t=sa-sb;
                    while(t)
                    {
                        h++;
                        t/=10;
                    }
                    if(sa-sb<0)
                        h++;
                    cout<<d+j+h+2<<endl;
                    break;
                case 'c':
                    cout<<sa<<"*"<<sb<<"="<<sa*sb<<endl;
                    t=sa*sb;
                    while(t)
                    {
                        h++;
                        t/=10;
                    }
                    cout<<d+j+h+2<<endl;
                    break;
            }
        }
    }
    return 0;
}

球球了


by quxiangyu @ 2024-02-23 20:53:07

@SilVeR__WolF 能给个关注吗இ௰இ

#include <bits/stdc++.h>
using namespace std;
int main(){
    char a;
    int n,c,d;
    char s[100],b[10];
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>b;
        if(b[0]>='a' && b[0]<='z'){
            a=b[0];
            cin>>c>>d;
        }else{
            sscanf(b,"%d",&c);
            cin>>d;
        }
        memset(s,0,sizeof(s));
        if(a=='a')
            sprintf(s,"%d+%d=%d",c,d,c+d);
        else if(a=='b')
            sprintf(s,"%d-%d=%d",c,d,c-d);
        else if(a=='c')
            sprintf(s,"%d*%d=%d",c,d,c*d);
        cout<<s<<endl<<strlen(s)<<endl;
    }
    return 0;
}

by quxiangyu @ 2024-02-23 20:53:50

自测AC代码


by 11514zbs @ 2024-02-23 20:56:21

/*
1
a 0 0
*/

如果是这样就不对了


by SilVeR__WolF @ 2024-02-24 16:24:03

@quxiangyu @11514zbs 谢谢!已关


|