关于洛谷IDE的疑问

P1957 口算练习题

MrDoge @ 2022-11-29 21:27:01

求助代码在本地没有问题,在洛谷在线ide上测试,只算一组数的时候没有问题,只要输入多组数就只有最后一组是正常的,剩下的每一组的第二个数都会变成一些奇怪的数,这是什么原理。。。

比如在洛谷ide输入

2

a 10 4

10 4

就会输出

10+5=15

8

10+4=14

7

但是本地真的没有问题

(洛谷要比本地多加一个getchar();才能正常输入也不知道为什么。。。)

#include<cstdio>
#include<cmath>
#include<string.h>
using namespace std;
char all[100], ca[10], cb[10];
int x1[100], x2[100], x3[100], x4[100];
char s1[100];
int main(){
    int n;
    char f;
    int i = 0, cnt = 0, na = 0, nb = 0, numa = 0, numb = 0, out;
    scanf("%d", &n);
    getchar(); 
    getchar();
    for(int k = 1; k <= n; k++){
        memset(all, '\0', sizeof(all));
        memset(ca, '\0', sizeof(ca));
        memset(cb, '\0', sizeof(cb));
        gets(all);
        if(all[0] == 'a' || all[0] == 'b' || all[0] == 'c'){
            switch(all[0]){
                case 'a':
                    f = '+';
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 2; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa+numb;
                    out = numa+numb;
                    cnt = na+nb+2+2;
                    while(out){
                        cnt++;
                        out /= 10;
                    }
                    x4[k] = cnt;
                    break;
                case 'b':
                    f = '-';
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 2; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa-numb;
                    out = numa-numb;
                    if(numa-numb>=0){
                        cnt = na+nb+2+2;
                        while(out){
                            cnt++;
                            out /= 10;
                        }
                        x4[k] = cnt;
                    }
                    else{
                        cnt = na+nb+2+3;
                        out = -out;
                        while(out){
                            cnt++;
                            out /= 10;
                        }
                        x4[k] = cnt;
                    }
                    break;
                case 'c':
                    f = '*';
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 2; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa*numb;
                    out = numa*numb;
                    cnt = na+nb+2+2;
                    while(out){
                        cnt++;
                        out /= 10;
                    }
                    x4[k] = cnt;
                    break;
            }

        }
        else{
            switch(f){
                case '+':
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 0; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    out = numa+numb;
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa+numb;
                    cnt = na+nb+2+2;
                    while(out){
                        cnt++;
                        out /= 10;
                    }
                    x4[k] = cnt;
                    break;
                case '-':
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 0; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    out = numa-numb;
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa-numb;
                    if(numa-numb>=0){
                        cnt = na+nb+2+2;
                        while(out){
                            cnt++;
                            out /= 10;
                        }
                        x4[k] = cnt;
                    }
                    else{
                        cnt = na+nb+2+3;
                        out = -out;
                        while(out){
                            cnt++;
                            out /= 10;
                        }
                        x4[k] = cnt;
                    }
                    break;
                case '*':
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 0; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    out = numa*numb;
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa*numb;
                    cnt = na+nb+2+2;
                    while(out){
                        cnt++;
                        out /= 10;
                    }
                    x4[k] = cnt;
                    break;
            }
        }
    }
    for(int k = 1; k <= n; k++){
        printf("%d%c%d=%d\n%d\n", x1[k], s1[k], x2[k], x3[k], x4[k]);
    }
}

by olegekei @ 2022-11-29 21:32:35

@MrDoge 是gets的问题,你可以读入n也用gets,这样就不用再getchar了


by sunzz3183 @ 2022-11-29 21:48:33

不要用 gets,不要用 gets,不要用 gets!

(不知有多少OIer曾栽在gets上


by MrDoge @ 2022-11-29 22:06:50

@olegekei 我用下面的代码输入了n,在洛谷ide上试着输出了一下也能转换成int型,但整个程序直接没有输出了,一个也算不了了。。。(本地还是没问题)

    gets(n);
   int n1, n2 = 0;
   for(n1 = 0; n[n1] !='\0'; n1++);
   for(int k = n1-1; k >= 0; k--){
       n2 += (n[cnt]-'0')*pow(10,k);
       cnt++;
    }

完整的代码是下面的(好冗长觉得自己好菜。。)感谢大佬帮助

#include<cstdio>
#include<cmath>
#include<string.h>
using namespace std;
char all[100], ca[10], cb[10];
int x1[100], x2[100], x3[100], x4[100];
char s1[100];
char n[10];
int main(){
    int i = 0, cnt = 0, na = 0, nb = 0, numa = 0, numb = 0, out;
    gets(n);
    int n1, n2 = 0;
    for(n1 = 0; n[n1] !='\0'; n1++);
    for(int k = n1-1; k >= 0; k--){
        n2 += (n[cnt]-'0')*pow(10,k);
        cnt++;
    }
    char f;
    for(int k = 1; k <= n2; k++){
        memset(all, '\0', sizeof(all));
        memset(ca, '\0', sizeof(ca));
        memset(cb, '\0', sizeof(cb));
        gets(all);
        if(all[0] == 'a' || all[0] == 'b' || all[0] == 'c'){
            switch(all[0]){
                case 'a':
                    f = '+';
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 2; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa+numb;
                    out = numa+numb;
                    cnt = na+nb+2+2;
                    while(out){
                        cnt++;
                        out /= 10;
                    }
                    x4[k] = cnt;
                    break;
                case 'b':
                    f = '-';
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 2; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa-numb;
                    out = numa-numb;
                    if(numa-numb>=0){
                        cnt = na+nb+2+2;
                        while(out){
                            cnt++;
                            out /= 10;
                        }
                        x4[k] = cnt;
                    }
                    else{
                        cnt = na+nb+2+3;
                        out = -out;
                        while(out){
                            cnt++;
                            out /= 10;
                        }
                        x4[k] = cnt;
                    }
                    break;
                case 'c':
                    f = '*';
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 2; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa*numb;
                    out = numa*numb;
                    cnt = na+nb+2+2;
                    while(out){
                        cnt++;
                        out /= 10;
                    }
                    x4[k] = cnt;
                    break;
            }

        }
        else{
            switch(f){
                case '+':
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 0; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    out = numa+numb;
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa+numb;
                    cnt = na+nb+2+2;
                    while(out){
                        cnt++;
                        out /= 10;
                    }
                    x4[k] = cnt;
                    break;
                case '-':
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 0; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    out = numa-numb;
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa-numb;
                    if(numa-numb>=0){
                        cnt = na+nb+2+2;
                        while(out){
                            cnt++;
                            out /= 10;
                        }
                        x4[k] = cnt;
                    }
                    else{
                        cnt = na+nb+2+3;
                        out = -out;
                        while(out){
                            cnt++;
                            out /= 10;
                        }
                        x4[k] = cnt;
                    }
                    break;
                case '*':
                    cnt = 0, numa = 0, numb = 0;
                    for(i = 0; all[i] != ' '; i++){
                        ca[cnt] = all[i];
                        cnt++;
                    }
                    cnt = 0;
                    for(i++; all[i] != '\0'; i++){
                        cb[cnt] = all[i];
                        cnt++;
                    }
                    for(na = 0; ca[na] != '\0'; na++);
                    na--;
                    for(nb = 0; cb[nb] != '\0'; nb++);
                    nb--;
                    cnt = na;
                    for(int j = 0; j <= na; j++){
                        numa += (ca[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    cnt = nb;
                    for(int j = 0; j <= nb; j++){
                        numb += (cb[j]-'0')*pow(10, cnt);
                        cnt--;
                    }
                    out = numa*numb;
                    s1[k] = f;
                    x1[k] = numa;
                    x2[k] = numb;
                    x3[k] = numa*numb;
                    cnt = na+nb+2+2;
                    while(out){
                        cnt++;
                        out /= 10;
                    }
                    x4[k] = cnt;
                    break;
            }
        }
    }
    for(int k = 1; k <= n2; k++){
        printf("%d%c%d=%d\n%d\n", x1[k], s1[k], x2[k], x3[k], x4[k]);
    }
}

by MrDoge @ 2022-11-29 22:09:06

@sunzz3183 因为scanf输入字符串不能带空格,只学了gets就一直用了。。出现了两三次这种情况。。大佬能给几个能输入带空格字符串的函数名吗,我自己查一下学一下。。感谢大佬


by InnitTimmer @ 2022-11-29 22:14:02

@MrDoge getline可以考虑,但是有时会出锅


by MrDoge @ 2022-11-29 22:20:00

@InnitTimmer 好的,感谢


by sunzz3183 @ 2022-11-30 06:51:31

@MrDoge getline比较稳定了,用这个基本不出锅


by MrDoge @ 2022-11-30 09:13:16

@sunzz3183 okk


by sunzz3183 @ 2022-11-30 10:47:26

@MrDoge 这个自动忽略换行

cin.get可以读入换行

getchar的数量由评测环境定,有时win转linux或linux转win就要用两个getchar


by MrDoge @ 2022-11-30 13:59:53

@sunzz3183 明白了,感谢


|