c++这样写为什么不行啊

P5705 【深基2.例7】数字反转

Edward_Ellick @ 2023-06-20 21:26:41

#include<bits/stdc++.h>
using namespace std;
char a[10],c=13;
int i=0,j=0;
int main() {
    while(1){
        c = getchar();
        if(c=='\n')
            break;
        if(c=='.')
            j=i;
        a[i]=c;
        i++;
    }
    for(int k=strlen(a)-1;k>=0;k--){
        bool cha=false;
        if(cha!=1&&a[k]==0)
            continue;
        else
            cha=1;
        if(a[k]=='0'&&k>=j)
            continue;
        if(k!=j)
            printf("%c",int(a[k]));
        else
            printf(".");
    }
    return 0;
}

显示

Wrong Answer.wrong answer On line 1 column 1, read (ASCII 13), expected 5.


by FuckYouJinhai @ 2023-06-20 21:33:09

@Edward_Ellick 你似乎输出了一个\r,数据在win造的缘故


by Edward_Ellick @ 2023-06-20 21:35:44

@FiveFourierTransform "数据在win造的缘故

",这个是什么意思?


by FuckYouJinhai @ 2023-06-20 21:50:36

@Edward_Ellick windows换行符为\r\n,然后你就多读了什么、、


by CH_mengxiang @ 2023-06-21 11:07:47

#include<bits/stdc++.h>
using namespace std;
char a[10],c=13;
int i=0,j=0;
int main() {
    while(1){
        c = getchar();
        if(c=='\n'||c=='\r')
            break;
        if(c=='.')
            j=i;
        a[i]=c;
        i++;
    }
    for(int k=strlen(a)-1;k>=0;k--){
        bool cha=false;
        if(cha!=1&&a[k]==0)
            continue;
        else
            cha=1;
        if(a[k]=='0'&&k>=j)
            continue;
        if(k!=j)
            printf("%c",int(a[k]));
        else
            printf(".");
    }
    return 0;
}

A了


|