求助,输出的和答案一摸一样,全wa了

P1957 口算练习题

00000000007f @ 2024-02-02 13:22:53

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,now=1;
    cin>>n;
    string a;
    getline(cin,a);
    while(n--){
        string x,y,s;
        int i=0,flag=1;
        getline(cin,a);
        if(a[0]<='d'&&a[0]>='a'){
            i++;
            if(a[0]=='a')now=1;
            if(a[0]=='b')now=2;
            if(a[0]=='c')now=3;
            if(a[0]=='d')now=4;
        }
        for(;i<a.size();i++){
            if(a[i]==' ')continue;
            if(flag) x+=a[i];
            else y+=a[i];
            if(a[i+1]==' ') flag=0;
        }
        s=x;
        if(now==1)s+='+'+y+'='+to_string(stoi(x)+stoi(y));
        if(now==2)s+='-'+y+'='+to_string(stoi(x)-stoi(y));
        if(now==3)s+='*'+y+'='+to_string(stoi(x)*stoi(y));
        if(now==4)s+='/'+y+'='+to_string(stoi(x)/stoi(y));
        cout<<s<<'\n'<<s.size()<<'\n';
    }

    return 0;
} 

by summer_000 @ 2024-02-02 16:30:19

用一个样例进行测试,结果如下图,最后一次输出后多输出了空行,导致WA


by 00000000007f @ 2024-02-03 20:14:32

@summer_000 谢谢大佬


by wei_6 @ 2024-02-08 01:45:50

大佬能看下我的问题吗


#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
int le(int a)
{

    int p = 0;
    if (a < 0)
    {
        p++;
        a = -a;
    }
    if (a == 0)
    {
        return 1;
    }
    while (a)
    {
        a /= 10;
        p++;
    }
    return p;
} // 返回一个数字的位数
int main()
{
    char cmd[1000][100] = {};
    int p = 0, n = 0;
    cin >> n; // p用于存储上一个指令类型,123依次对应abc,+-*;
    cin.get();
    int turn = 0;
    for (int l = 0; l < n; l++)
    {

        cin.getline(cmd[l], 1000);
    }
    for (int l = 0; l < n; l++)
    {
        int i = 0, j = 0;
        char fu;
        int s1 = 0, s2 = 0, s3 = 0;
        int a = 0, b = 0, sum = 0; // 存储三项数据
        int len = strlen(cmd[l]);
        if (cmd[l][0] == 'a' || cmd[l][0] == 'b' || cmd[l][0] == 'c')
            turn = 2;
        else
            turn = 1; // 判断指令的种类
        if (turn == 1)
        {
            for (i = 0; i < len; i++)
            {
                if (cmd[l][i] == ' ')
                    break;
                a = a * 10 + cmd[l][i] - '0';
            }
            j = i;
            for (j = i + 1; j < len; j++)
            {
                b = b * 10 + cmd[l][j] - '0';
            } // 执行数据b
        }     // 两个数据类型的指令
        else
        {
            for (i = 2; i < len; i++)
            {
                if (cmd[l][i] == ' ')
                    break;
                a = a * 10 + cmd[l][i] - '0';
            }
            j = i;
            for (j = i + 1; j < len; j++)
            {
                b = b * 10 + cmd[l][j] - '0';
            } // 执行数据b
            p = cmd[l][0] - 'a' + 1;
        }
        if (p == 1)
        {
            sum = a + b;
            fu = '+';
        }
        else if (p == 2)
        {
            sum = a - b;
            fu = '-';
        }
        else
        {
            sum = a * b;
            fu = '*';
        }
        s1 = le(a);
        s2 = le(b);
        s3 = le(sum);
        cout << a << fu << b << '=' << sum << '\n';
        cout << s1 + s2 + s3 + 2 <<endl;
    } //
    return 0;
}

by summer_000 @ 2024-02-17 18:00:49

@wei_6

应该是和贴主同一个问题,最后一行不要换行!


|