全WA,IDE测了数据是没问题的

P1957 口算练习题

yzy0323 @ 2024-02-26 16:48:34

#include <iostream>
#include <algorithm>
using namespace std;
int cal(int num)
{
    int wei = 0;
    if (num == 0)
        return 1;
    while (num >= 1 || num <= -1)
    {
        num /= 10;
        wei++;
    }
    return wei;
}
int main()
{
    int n, a, b, c;
    char ch,cht;
    cin >> n;
    cin >> ch;
    for (int i = 0; i < n; i++)
    {
        cin >> cht;
        int wei;
        if (cht >= '0' && cht <= '9')
        {
            char tnum[10005];
            int j;
            cin.get(tnum+1,10004);
            tnum[0] = cht;
            a = b = 0;
            for (j = 0; tnum[j] != ' '; j++)
                a = a * 10 + tnum[j] - '0';
            while (tnum[++j] != '\0')
            {
                b = b * 10 + tnum[j] - '0';
            }
        }
        else
        {
            cin >> a >> b;
            ch = cht;
        }
        switch (ch)
        {
        case 'a':
            c = a + b;
            wei = cal(a) + cal(b) + cal(c) + 2;
            printf("%d+%d=%d\n%d\n", a, b, c, wei);
            break;
        case 'b':
            c = a - b;
            wei = cal(a) + cal(b) + cal(c) + 2;
            if (c < 0)
                wei++;
            printf("%d-%d=%d\n%d\n", a, b, c, wei);
            break;
        case 'c':
            c = a * b;
            wei = cal(a) + cal(b) + cal(c) + 2;
            printf("%d*%d=%d\n%d\n", a, b, c, wei);
            break;
        }
    }
    return 0;
}

//


by 违规用户名971024 @ 2024-02-26 17:51:12

#include<bits/stdc++.h>
using namespace std;
int main()
{
   int n,a,b,c;
   char last,s[20],ans[20];
   scanf("%d\n",&n);
   while(n--)
   {
        fgets(s,sizeof(s),stdin);
        if(s[0]=='a'||s[0]=='b'||s[0]=='c')
             last=s[0],s[0]=' ';
        sscanf(s,"%d %d",&a,&b);
        switch(last)
        {
            case 'a': c=a+b;sprintf(ans,"%d+%d=%d",a,b,c);break;
            case 'b': c=a-b;sprintf(ans,"%d-%d=%d",a,b,c);break;
            case 'c': c=a*b;sprintf(ans,"%d*%d=%d",a,b,c);break;    
        }
        printf("%s\n%d\n",ans,strlen(ans));
   }
} 

by 违规用户名971024 @ 2024-02-26 17:51:43

不用那么麻烦,看看我的代码。


by 违规用户名971024 @ 2024-02-26 17:52:48

给个关注呗


by 违规用户名971024 @ 2024-02-26 17:53:11

@yzy0323


by yzy0323 @ 2024-02-26 21:01:48

@feizhukkksc03 ok已关


by yzy0323 @ 2024-02-26 21:02:21

@feizhukkksc03 还是想知道问题出在哪


by cpp_xhq @ 2024-03-16 09:27:15

@yzy0323


cin >> ch;
这行有问题

by yzy0323 @ 2024-03-16 09:40:34

@cpp_xhq 请问是怎样的运行问题呢?我在自己IDE测的数据是没问题的


by cpp_xhq @ 2024-03-16 09:47:40

@yzy0323 我没理解您什什么意思 能讲一下思路吗


by cpp_xhq @ 2024-03-16 09:53:33

@yzy0323 我的思路是先读取次数 在循环读取第一个数或字母 如果他是字母就继续输入 如果他是数字把字符串数字转换成int


| 下一页