自己测试时没有问题,放进洛谷IDE后得到的输出和本地跑的不一样

P1957 口算练习题

meiyouyandeyu @ 2021-12-30 22:46:47

这是我自己写

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>

char arr[15];

int main()
{
    int i = 0;
    int type = 0;
    scanf("%d", &i);
    getchar();
    getchar();
    while(i--)
    {
        memset(arr, 0, 14);
        int x = 0, y = 0;
        int q = 0;

        char a;
        int n = 0;
        int sum = 0;
        while((a = getchar()) != '\n')
        {
            arr[n++] = a;
        }
        if(arr[0] == 'a' || arr[0] == 'b' || arr[0] == 'c')
        {
            type = arr[0] - 'a' + 1;
            int p = 2;
            while(arr[p] != ' ')
            {
                x = x*10 + arr[p] - '0';
                p++;
            }
            p = p + 1;
            while(arr[p] != ' ' && arr[p] != '\0')
            {
                y = y * 10 + arr[p] - '0';
                p++;
            }
            q = p - 1;
        }
        else
        {
            int p = 0;
            while(arr[p] != ' ')
            {
                x = x*10 + arr[p] - '0';
                p++;
            }
            p = p + 1;
            while(arr[p] != ' ' && arr[p] != '\0')
            {
                y = y * 10 + arr[p] - '0';
                p++;
            }
            q = p + 1;
        }

        if(type == 1)
        {
            sum = x + y;
            printf("%d+%d=%d\n", x, y, sum);
        }
        else if(type == 2)
        {
            sum = x - y;
            printf("%d-%d=%d\n", x, y, sum);
        }
        else if(type == 3)
        {
            sum = x * y;
            printf("%d*%d=%d\n", x, y, sum);
        }

        int count = 1;
        while(sum / 10 != 0)
        {
            sum = sum / 10;
            count++;
        }
        if(sum < 0)
        {
            count++;
        }
        printf("%d", count + q);
        if(i)
        {
            printf("\n");
        }
    }
}

希望大哥能尽量用c解释,萌新只会c 本地没问题,洛谷全WA


by ud2_ @ 2021-12-30 22:49:23

getchar()?


by suspect_x @ 2022-01-11 20:34:34

洛谷getchar()好像会读不到换行


by fafu_y46 @ 2022-04-27 10:56:22

洛谷中getchar()读不了换行,导致程序会多读了一行空数据


|