样例和第一个数据点本地测试都对 但是0分

P1957 口算练习题

fzy1026 @ 2022-05-01 11:46:48

RT

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

int n;

int Len(int x)
{
    int i = 0;
    if(x == 0)
    {
        return 1;
    }
    if(x < 0)
    {
        i++;
        x = -x;
    }

    while(x > 0)
    {
        x /= 10;
        i++;
    }

    return i;
}

int main()
{

    cin>>n;
    string s;
    char cc;
    int len;
    int Ans;
    int A = 0,B = 0;
    getline(cin,s);
    for(int i=1; i<=n; i++)
    {
        A = 0;
        B = 0;
        getline(cin,s);
        len = s.length();
        if(s[0] >= 'a'&&s[0] <='c')
        {
            cc = s[0];
            int j = 2;
            while(s[j] >= '0'&&s[j] <= '9')
            {
                A *= 10;
                A += s[j] - '0';
                j++;
            }
            j++;
            while(j < len)
            {
                B *= 10;
                B += s[j] - '0';
                j++;
            }
            switch(cc)
            {
                case 'a':
                    Ans = A + B;
                    cout<<A<<"+"<<B<<"="<<Ans<<endl;
                    break;
                case 'b':
                    Ans = A - B;
                    cout<<A<<"-"<<B<<"="<<Ans<<endl;
                    break;
                case 'c':
                    Ans = A * B;
                    cout<<A<<"*"<<B<<"="<<Ans<<endl;
                    break;
            }
        }
        else
        {
            char pt[10000];
            int j = 0;
            A = 0,B = 0;
            while(s[j] >= '0'&&s[j] <= '9')
            {
                A *= 10;
                A += s[j] - '0';
                j++;
            }
            j++;
            while(j < len)
            {
                B *= 10;
                B += s[j] - '0';
                j++;
            }
            switch(cc)
            {
                case 'a':
                    Ans = A + B;
                    cout<<A<<"+"<<B<<"="<<Ans<<endl;
                    break;
                case 'b':
                    Ans = A - B;
                    cout<<A<<"-"<<B<<"="<<Ans<<endl;
                    break;
                case 'c':
                    Ans = A * B;
                    cout<<A<<"*"<<B<<"="<<Ans<<endl;
                    break;
            }
        }
        cout<<Len(A)+Len(B)+Len(Ans)+2<<endl;
    }
    return 0;
}

by 编码落寞 @ 2022-05-05 10:09:09

@fzy1026

你用洛谷的IDE测试吧,可能是换行符的问题


by fzy1026 @ 2022-05-05 11:06:23

@编码落寞 试了一下确实有问题

我再看看,万分感谢Orz


by fzy1026 @ 2022-05-05 11:45:36

已过,读入时把结尾的换行符也读进去了


|