样例正确,结果全WA

P1957 口算练习题

afreshmanofclanguage @ 2024-02-05 15:51:40

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstring>
#include <string.h>
#include <algorithm>
using namespace std;
int n; char fuhao;
string s; int a, b; int cnt; char before; char tem[100];
void print(int cnt,int i)
{
    if (i == n - 1)
        cout << cnt - 1;
    else
        cout << cnt - 1 << endl;
}
int main()
{
    cin >> n; getchar();
    for (int i = 0; i < n; i++)
    {
        fgets(tem, 100, stdin); 
        s = tem;
        if (isdigit(s[0]))
            sscanf(s.c_str(), "%d %d", &a, &b);
        else
            sscanf(s.c_str(),"%*c %d %d", &a, &b);
        switch (s[0])
        {
        case 'a':cnt = printf("%d+%d=%d\n", a, b, a + b); print(cnt,i); before = 'a'; break;
        case 'b':cnt = printf("%d-%d=%d\n", a, b, a - b); print(cnt, i); before = 'b'; break;
        case 'c':cnt = printf("%d*%d=%d\n", a, b, a * b); print(cnt, i); before = 'c'; break;
        default:
        {
            switch (before)
            {
            case 'a':cnt = printf("%d+%d=%d\n", a, b, a + b); print(cnt, i); before = 'a'; break;
            case 'b':cnt = printf("%d-%d=%d\n", a, b, a - b); print(cnt, i); before = 'b'; break;
            case 'c':cnt = printf("%d*%d=%d\n", a, b, a * b); print(cnt, i); before = 'c'; break;
            }
        }
        }
    }
    return 0;
}

by hetong321 @ 2024-02-07 11:23:00

输入和输出要全部分开,全部输入完了以后再输出


by Himner @ 2024-02-07 21:13:51

@hetong321 不用的,输入一行再输出也能过,他wa并不是这个问题


|