代码在 java 中正常工作,但相同的代码在 C 中的某些测试用例中失败
Code working properly in java, but same code failing in some Test Cases in C
我在 HackerRank 网站上做算法练习题,我先用 C 语言提交了我的代码,但我得到了一些不正确的测试用例。我认为我的逻辑是正确的,所以我将代码移植到 Java 并通过了所有测试用例。
Link 到问题定义:
https://www.hackerrank.com/challenges/caesar-cipher-1
这是我在 C 中的代码:
int main(){
int n;
scanf("%d", &n);
char* s = (char *)malloc(n * sizeof(char));
scanf("%s", s);
int k;
scanf("%d", &k);
k = k % 26;
for(int i = 0; i < n; i++){
if(s[i] >= 65 && s[i] <= 90){
s[i] += k;
if(s[i] > 90){
s[i] = s[i] - 90 + 64;
}
}
if(s[i] >= 97 && s[i] <= 122){
s[i] += k;
if(s[i] > 122){
s[i] = s[i] - 122 + 96;
}
}
}
printf("%s", s);
return 0;
}
这是我在 Java 中的代码:
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String str = br.readLine();
int K = Integer.parseInt(br.readLine());
K %= 26;
for(int i = 0; i < N; i++){
char c = str.charAt(i);
if(c >= 65 && c <= 90){
c += K;
if(c > 90){
c = (char)(c - 90 + 64);
}
}
if(c >= 97 && c <= 122){
c += K;
if(c > 122){
c = (char)(c - 122 + 96);
}
}
System.out.print(c);
}
}
}
我的两个解决方案都通过了示例测试用例,逻辑完全相同。我不明白为什么在某些测试用例中它在 C 中显示 W/A。
P.S。这是练习题解法,不是现场比赛解法。
C 字符串以一个特殊的 '[=14=]'
字符结尾。因此实际字符串总是比可见文本长 1
个字符。
您没有为输入字符串空终止符分配内存:
替换
char* s = (char *)malloc(n * sizeof(char));
和
char* s = malloc(n * sizeof(char) + 1);
甚至
char* s = malloc(n + 1);
因为 sizeof(char)
保证是 1
.
此外,这些行也会有问题:
if(s[i] >= 97 && s[i] <= 122){
s[i] += k;
s[i]
可能是 122
而 k
可能比 5
大得多。由于 s[i]
很可能是有符号 char
类型,它可能会溢出超过 127
的值,这是有符号 char
的最大值。有符号整数溢出具有 未定义行为 。
这个工作正常。
int main(){
int n;
scanf("%d",&n);
char* s = (char *)malloc(10240 * sizeof(char));
scanf("%s",s);
int k;
scanf("%d",&k);
for(int i = 0; i < n; i++){
if(s[i] >= 65 && s[i] <= 90){
s[i] = (s[i]-'A'+k)%26 +'A';
}
if(s[i] >= 97 && s[i] <= 122){
s[i] = (s[i]-'a'+k)%26 +'a';
}
}
printf("%s", s);
return 0;
}
刚刚更新了 if 块和 malloc 大小的内容。
我在 HackerRank 网站上做算法练习题,我先用 C 语言提交了我的代码,但我得到了一些不正确的测试用例。我认为我的逻辑是正确的,所以我将代码移植到 Java 并通过了所有测试用例。
Link 到问题定义: https://www.hackerrank.com/challenges/caesar-cipher-1
这是我在 C 中的代码:
int main(){
int n;
scanf("%d", &n);
char* s = (char *)malloc(n * sizeof(char));
scanf("%s", s);
int k;
scanf("%d", &k);
k = k % 26;
for(int i = 0; i < n; i++){
if(s[i] >= 65 && s[i] <= 90){
s[i] += k;
if(s[i] > 90){
s[i] = s[i] - 90 + 64;
}
}
if(s[i] >= 97 && s[i] <= 122){
s[i] += k;
if(s[i] > 122){
s[i] = s[i] - 122 + 96;
}
}
}
printf("%s", s);
return 0;
}
这是我在 Java 中的代码:
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String str = br.readLine();
int K = Integer.parseInt(br.readLine());
K %= 26;
for(int i = 0; i < N; i++){
char c = str.charAt(i);
if(c >= 65 && c <= 90){
c += K;
if(c > 90){
c = (char)(c - 90 + 64);
}
}
if(c >= 97 && c <= 122){
c += K;
if(c > 122){
c = (char)(c - 122 + 96);
}
}
System.out.print(c);
}
}
}
我的两个解决方案都通过了示例测试用例,逻辑完全相同。我不明白为什么在某些测试用例中它在 C 中显示 W/A。
P.S。这是练习题解法,不是现场比赛解法。
C 字符串以一个特殊的 '[=14=]'
字符结尾。因此实际字符串总是比可见文本长 1
个字符。
您没有为输入字符串空终止符分配内存:
替换
char* s = (char *)malloc(n * sizeof(char));
和
char* s = malloc(n * sizeof(char) + 1);
甚至
char* s = malloc(n + 1);
因为 sizeof(char)
保证是 1
.
此外,这些行也会有问题:
if(s[i] >= 97 && s[i] <= 122){
s[i] += k;
s[i]
可能是 122
而 k
可能比 5
大得多。由于 s[i]
很可能是有符号 char
类型,它可能会溢出超过 127
的值,这是有符号 char
的最大值。有符号整数溢出具有 未定义行为 。
int main(){
int n;
scanf("%d",&n);
char* s = (char *)malloc(10240 * sizeof(char));
scanf("%s",s);
int k;
scanf("%d",&k);
for(int i = 0; i < n; i++){
if(s[i] >= 65 && s[i] <= 90){
s[i] = (s[i]-'A'+k)%26 +'A';
}
if(s[i] >= 97 && s[i] <= 122){
s[i] = (s[i]-'a'+k)%26 +'a';
}
}
printf("%s", s);
return 0;
}
刚刚更新了 if 块和 malloc 大小的内容。