gpt4 book ai didi

c - SPOJ 显示我对 : http://www. spoj.com/BSCPROG/problems/SMPCPH1/的解决方案的错误答案

转载 作者:行者123 更新时间:2023-11-30 21:28:46 26 4
gpt4 key购买 nike

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

main() {
int n, m, i, j, k;
char a[100], b[100];
scanf("%d", &n);
if (n > 26 && n <= 1)
exit(0);
scanf("%s", a);
scanf("%d", &m);
if (m > 100 && m <= 1)
exit(0);
for (i = 0; i < m; i++) {
fgets(b, 100, stdin);
for (j = 0; b[j] != '\0'; j++) {
for (k = 0; k < n; k++) {
if (a[k] == b[j]) {
if (k == (n - 1)) {
b[j] = a[0];
break;
} else {
b[j] = a[k+1];
break;
}
}
}
}
printf("%s",b);
}
return 0;
}

I just have written C code for the problem: http://www.spoj.com/BSCPROG/problems/SMPCPH1/ With the given example my program gives desired result but spoj says it is wrong. How can I find mistakes?

最佳答案

您需要对代码进行 3 处更改:

  • 在参数验证测试中使用 || 而不是 &&
  • 扫描并忽略数字解析为m后的换行符,否则下一个fgets()将读取空行

这是更正后的代码:

if (n > 26 || n <= 1)
exit(0);
scanf("%s", a);
scanf("%d%*c", &m);
if (m > 100 || m <= 1)
exit(0);

关于c - SPOJ 显示我对 : http://www. spoj.com/BSCPROG/problems/SMPCPH1/的解决方案的错误答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38272301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com