0分,求助,明明自己测试没有问题

P1957 口算练习题

RANce875 @ 2023-11-19 21:28:49

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int digit_sum(int n)//求一个整数的位数(写的超烂,希望指点(各种方面))
{
    if (n >= 0)
    {
        if (n >= 10)
            return digit_sum(n / 10) + 1;
        else
            return 1;
    }
    else
    {
        n = -n;
        if (n >= 10)
            return digit_sum(n / 10) + 2;
        else
            return 2;

    }
}//我的思路是,首先把a 99 999作为三个字符串输入,然后把99和999转成整形数字相加,至于最后的所求字符串的长度,我的思路是三部分相加,99,999和转成数字的99+999的位数。
int main()
{
    int n;
    scanf("%d", &n);
    getchar();
    for (int u = 1; u <= n; u++)
    {

        char c1[200];
        char c2[200];
        char c3[200];
        scanf("%s",&c1);
        if (isalpha(c1[0]))
        {
            scanf("%s",c2);
            int len2 = strlen(c2);
            scanf("%[^\n]", c3);

            int len3 = strlen(c3);
            if (c1[0] == 'a')
            {
                int w = atoi(c2) + atoi(c3);
                printf("%d+%d=%d\n",atoi(c2),atoi(c3),w);
                printf("%d", len2 + len3 + digit_sum(w) + 1);
            }
            if (c1[0] == 'b')
            {
                int w = atoi(c2) - atoi(c3);
                printf("%d-%d=%d\n", atoi(c2), atoi(c3), w);
                printf("%d", len2 + len3 + digit_sum(w) + 1);
            }
            if (c1[0] == 'c')
            {
                int w = atoi(c2) * atoi(c3);
                printf("%d*%d=%d\n", atoi(c2), atoi(c3), w);
                printf("%d", len2 + len3 + digit_sum(w) + 1);
            }
        }
        else
        {
            strcpy(c2, c1);
            int len2 = strlen(c2);
            scanf("%[^\n]", c3);
            getchar();
            int len3 = strlen(c3);
            int w = atoi(c2) + atoi(c3);
            printf("%d+%d=%d\n", atoi(c2), atoi(c3), w);
            printf("%d", len2 + len3 + digit_sum(w) + 1);
        }
    }
    return 0;
}

by RANce875 @ 2023-11-19 22:15:57

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int digit_sum(int n)
{
    if (n >= 0)
    {
        if (n >= 10)
            return digit_sum(n / 10) + 1;
        else
            return 1;
    }
    else
    {
        n = -n;
        if (n >= 10)
            return digit_sum(n / 10) + 2;
        else
            return 2;

    }
}
int main()
{
    int n;
    scanf("%d", &n);
    getchar();
    for (int u = 1; u <= n; u++)
    {

        char c1[200];
        char c2[200];
        char c3[200];
        scanf("%s",&c1);
        if (isalpha(c1[0]))
        {
            scanf("%s",c2);
            int len2 = strlen(c2);
            scanf("%s", c3);
            int len3 = strlen(c3);
            if (c1[0] == 'a')
            {
                int w = atoi(c2) + atoi(c3);
                printf("%d+%d=%d\n",atoi(c2),atoi(c3),w);
                printf("%d\n", len2 + len3 + digit_sum(w) + 2);
            }
            if (c1[0] == 'b')
            {
                int w = atoi(c2) - atoi(c3);
                printf("%d-%d=%d\n", atoi(c2), atoi(c3), w);
                printf("%d\n", len2 + len3 + digit_sum(w) +2);
            }
            if (c1[0] == 'c')
            {
                int w = atoi(c2) * atoi(c3);
                printf("%d*%d=%d\n", atoi(c2), atoi(c3), w);
                printf("%d\n", len2 + len3 + digit_sum(w) + 2);
            }
        }
        else
        {
            strcpy(c2, c1);
            int len2 = strlen(c2);
            scanf("%s", c3);
            getchar();
            int len3 = strlen(c3);
            int w = atoi(c2) + atoi(c3);
            printf("%d+%d=%d\n", atoi(c2), atoi(c3), w);
            printf("%d\n", len2 + len3 + digit_sum(w) + 2);
        }
    }
    return 0;
}
//把scanf("%[^\n]]")改成scanf("%s",)好多了,可还是有三个不过

|