关于windows环境下\r\n 和linux环境下\n的区别

P1957 口算练习题

lk_liang @ 2021-10-24 22:35:05

希望有大佬能详细讲下题目中提到的问题

我知道我下面这份代码 本地全过 而洛谷全WA是由这个原因引起的 但我不太清楚为什么 和 怎么 改

#include<iostream>
#include<cstdio> 
using namespace std;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {   
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
        x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return x*f;
}
int num;
int work1(int t)    //  这个函数将t转换成整形输出
{
    int k=0;
    string s;
    while(t)
    {
        k++;
        s+=(t%10+'0');
        t/=10;
    }
    for(int i=s.size()-1;i>=0;i--) printf("%c",s[i]);
    return k;
} 
int main()
{
    int n=read(),x,y;
    char pre;
    for(int i=1;i<=n;i++)
    {
        char c=getchar();
        x=read(),y=read();
        num=2;
        if(c=='a') 
        {
            num+=work1(x);
            printf("+");
            num+=work1(y);
            printf("=");
            num+=work1(x+y);
            printf("\n%d\n",num);
            pre=c;
        }
        else if(c=='c') 
        {
            num+=work1(x);
            printf("*");
            num+=work1(y); 
            printf("=");
            num+=work1(x*y);
            printf("\n%d\n",num);
            pre=c;
        }
        else if(c=='b') 
        {
            num+=work1(x);
            printf("-");
            num+=work1(y);
            printf("=");
            int m=x-y;
            if(m<0)
            {
                num++;
                printf("-");
                m=-m;
            }
            num+=work1(m);
            printf("\n%d\n",num);
            pre=c;
        }
        else
        {
            int t=x,num2=0,p=1;
            while(t)    //  计算录的x有多少位 
            {
                num2++;
                t/=10;
            }
            for(int i=1;i<=num2;i++) p*=10;
            x=x+(c-'0')*p;
            if(pre=='a') 
            {
                num+=work1(x);
                printf("+");
                num+=work1(y);
                printf("=");
                num+=work1(x+y);
                printf("\n%d\n",num);
            }
            else if(pre=='c') 
            {
                num+=work1(x);
                printf("*");
                num+=work1(y); 
                printf("=");
                num+=work1(x*y);
                printf("\n%d\n",num);
            }
            else if(pre=='b') 
            {
                num+=work1(x);
                printf("-");
                num+=work1(y);
                printf("=");
                int m=x-y;
                if(m<0)
                {
                    num++;
                    printf("-");
                    m=-m;
                }
                num+=work1(m);
                printf("\n%d\n",num);
            }
        }
    }
    return 0;
}
/*
1
a 64 46
275 125
c 11 99
b 46 64
46 64
c 11 99
11 99 
*/

来个daloa救救我


by lk_liang @ 2021-10-24 22:38:55

救救孩子吧 调不出来我今晚会睡不着觉的


by lk_liang @ 2021-10-24 22:39:20

本地下样例全是对的啊!!!!


by dn0pf902 @ 2021-10-24 22:45:30

@lk_liang 在

char c=getchar();

前面加一句

getchar();

就可以了


by dn0pf902 @ 2021-10-24 22:45:53

@lk_liang 貌似其他地方还有问题,只有70pts


by lk_liang @ 2021-10-24 22:47:16

@dn0pf902 谢谢哭死 好人一生平安!! 能不能顺便讲下原理


by dn0pf902 @ 2021-10-24 22:48:17

@lk_liang 用getchar()把\r吃掉


by lk_liang @ 2021-10-24 22:56:10

@dn0pf902 为什么呀


by Isprime @ 2021-10-24 23:00:05

输入字符串用cin就完事了,慢一点但是稳(或者scanf)


|