本地测试没有问题,为什么全是WA

P1957 口算练习题

Ljh20040202 @ 2022-10-22 11:48:22


#include <string.h>
int lenth(int num) // 计算一个数的长度
{
    int ans = 0;
    if (num < 0 ) ans++;
    while (num)
    {
        ans++;
        num/=10;
    }

    return ans;
}

int jisuan(int begin , char fh , char read1[50])
{
    int num1 = 0 , num2 = 0;
    int *p = & num1;
    for (int t = begin; t < strlen(read1); t++) // 将两个数分开来
    {
        if (read1[t] != ' ')
            *p = *p * 10 + read1[t] - '0';
        else
            p = &num2;
    }
    switch(fh)  // 分情况计算式子
    {
        case 'a':
        {
            printf("%d+%d=%d\n", num1 , num2 , num1 + num2);
            printf("%d\n", lenth(num1) + lenth(num2) + lenth(num1 + num2) + 2);
            break;
        }
        case 'b':
        {
            printf("%d-%d=%d\n", num1 , num2 , num1 - num2);
            printf("%d\n", lenth(num1) + lenth(num2) + lenth(num1 - num2) + 2);
            break;
        }
        case 'c':
        {
            printf("%d*%d=%d\n", num1 , num2 , num1 * num2);
            printf("%d\n", lenth(num1) + lenth(num2) + lenth(num1 * num2) + 2);
            break;
        }
        default : break;
    }
    return 0;
}
int main()
{
    int n;
    char read[55][50] , fuhao;
    scanf("%d", &n);
    fflush(stdin);   // 清空输入缓存;
    for (int i = 1; i <= n; i++)
        gets(read[i]);
    for (int i = 1; i <= n; i++)
        if (read[i][0] >= 'a' && read[i][0] <='c')
        {
            fuhao = read[i][0];
            jisuan(2 , fuhao , read[i]);
        }
        else
            jisuan(0 , fuhao , read[i]);
    return 0;
}

|