通过所有样例的0分

P1957 口算练习题

sss6666 @ 2020-12-27 14:35:08

#include<stdio.h>
#include<string.h>
int main()
{
    int n;
    int a, b;
    int result;
    char str[20];
    scanf_s("%d", &n);
    getchar();
    int len;
    while(n-->0)
    {
        gets_s(str);
        len = strlen(str);
        a = b = 0;
        if (str[0] >= 'a')
        {
            int i;
            for (i = 2; str[i] != 32; i++)
                a = a * 10 + (str[i] - '0');
            i++;
            for (; i < len; i++)
            {
                b = b * 10 + str[i] - '0';
            }
            switch (str[0])
            {
            case 'a':result = a + b; break;
            case 'b':result = a - b; break;
            case 'c':result = a * b; break;
            }
            if (result < 0)
            {
                result *= -1;
                len++;
            }
            if (result == 0)
                len++;
            else
                while (result)
                {
                    result /= 10;
                    len++;
                }
            len--;
            if(n!=0)
                switch (str[0])
                {
                case 'a':printf("%d+%d=%d\n%d\n", a, b, a + b, len); break;
                case 'b':printf("%d-%d=%d\n%d\n", a, b, a - b, len); break;
                case 'c':printf("%d*%d=%ld\n%d\n", a, b, a * b, len); break;
                }
            else
                switch (str[0])
                {
                case 'a':printf("%d+%d=%d\n%d", a, b, a + b, len); break;
                case 'b':printf("%d-%d=%d\n%d", a, b, a - b, len); break;
                case 'c':printf("%d*%d=%ld\n%d", a, b, a * b, len); break;
                }

        }
        else
        {
            int i;
            for (i = 0; str[i] != 32; i++)
                a = a * 10 + str[i] - '0';
            i++;
            for (; i < len; i++)
                b = b * 10 + str[i] - '0';
            int result = a + b;
            if (result < 0)
            {
                result *= -1;
                len++;
            }
            if (result == 0)
                len++;
            else
                while (result)
                {
                    result /= 10;
                    len++;
                }
            len++;
            if (n != 0)
                printf("%d+%d=%d\n%d\n", a, b, a + b, len);
            else
                printf("%d+%d=%d\n%d", a, b, a + b, len);
        }
    }
    return 0;
}

(用的vs编译器所以用了scanf_s和gets_s),开了o2优化会发生wa和re的神奇转换


by asdf12 @ 2021-01-02 09:48:25

洛谷不能用getchar()输入回车,不过可以用gets()输入


|