CS50、替换、check50 在重复测试中失败并超时
CS50, substitution, check50 fails in duplicate tests with time out
我的问题可能与 or CS50 - pset2 - substitution有关,但我还是想不通。
当我 运行 手动测试时它通过了,但是当我 运行 check50
它失败了:
:( handles duplicate characters in key
timed out while waiting for program to exit
:( handles multiple duplicate characters in key
timed out while waiting for program to exit
您认为 check50 的最后两个测试在哪里失败
编辑:我删除了我的代码,因为我刚刚发现课程的“学术诚实”条款,而这个问题/答案线程可能违反了这些条款。
永不放弃,我找到了解决方案。
问题是我不了解字符串数组如何工作的一些细节(不变性),所以我做了一些蹩脚的变通办法(记住 "aaaaaaaa"
赋值...)。
最终 , How to convert a string to character array in c (or) how to extract a single char form string?, C char array initialization 加上实施密钥应恰好包含每个字符一次的规则(我认为没有必要!)使我找到了通过所有测试的解决方案。我post它在这里:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
// Remember argc before argv control; otherwise segmentation fault
if (argc < 2)
{
printf("Usage: ./substitution key.\n");
return 1;
}
if (strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
string key = argv[1];
int key_length = strlen(key);
for (int i = 0; i < key_length; i++)
{
if (!isalpha(argv[1][i]))
{
printf("Key must contain 26 characters.\n");
return 1;
}
}
// Error when key contains duplicate characters
for (int i = 0; i < key_length; i++)
{
for (int j = 0; j < key_length; j++)
{
if (i == j)
{
continue;
}
if (islower(key[i]))
{
char current_key_letter = key[i];
}
else
{
char current_key_letter = key[i] + 'A' - 'a';
}
if (key[i] == key[j])
{
printf("Key must contain 26 unique characters.\n");
return 1;
}
}
}
string plaintext = get_string("plaintext: ");
int plaintext_length = strlen(plaintext);
char output_text[plaintext_length];
strcpy(output_text, plaintext);
char lowcase_key[key_length];
strcpy(lowcase_key, key);
for (int i = 0, n = strlen(lowcase_key); i < n; i++)
{
lowcase_key[i] = tolower(key[i]);
}
for (int i = 0, n = plaintext_length; i < n; i++)
{
if (islower(plaintext[i]))
{
output_text[i] = lowcase_key[plaintext[i] - 'a'];
}
if (!isalpha(plaintext[i]))
{
output_text[i] = plaintext[i];
}
if (isupper(plaintext[i]))
{
output_text[i] = lowcase_key[plaintext[i] - 'A'] + 'A' - 'a';
}
}
printf("ciphertext: %s", output_text);
printf("\n");
return 0;
}
还有更优雅的解决方案,虽然我很喜欢- 'A'] + 'A' - 'a'
部分,但今天就这样了。感谢大家的评论!
我的问题可能与
当我 运行 手动测试时它通过了,但是当我 运行 check50
它失败了:
:( handles duplicate characters in key
timed out while waiting for program to exit
:( handles multiple duplicate characters in key
timed out while waiting for program to exit
您认为 check50 的最后两个测试在哪里失败
编辑:我删除了我的代码,因为我刚刚发现课程的“学术诚实”条款,而这个问题/答案线程可能违反了这些条款。
永不放弃,我找到了解决方案。
问题是我不了解字符串数组如何工作的一些细节(不变性),所以我做了一些蹩脚的变通办法(记住 "aaaaaaaa"
赋值...)。
最终
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
// Remember argc before argv control; otherwise segmentation fault
if (argc < 2)
{
printf("Usage: ./substitution key.\n");
return 1;
}
if (strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
string key = argv[1];
int key_length = strlen(key);
for (int i = 0; i < key_length; i++)
{
if (!isalpha(argv[1][i]))
{
printf("Key must contain 26 characters.\n");
return 1;
}
}
// Error when key contains duplicate characters
for (int i = 0; i < key_length; i++)
{
for (int j = 0; j < key_length; j++)
{
if (i == j)
{
continue;
}
if (islower(key[i]))
{
char current_key_letter = key[i];
}
else
{
char current_key_letter = key[i] + 'A' - 'a';
}
if (key[i] == key[j])
{
printf("Key must contain 26 unique characters.\n");
return 1;
}
}
}
string plaintext = get_string("plaintext: ");
int plaintext_length = strlen(plaintext);
char output_text[plaintext_length];
strcpy(output_text, plaintext);
char lowcase_key[key_length];
strcpy(lowcase_key, key);
for (int i = 0, n = strlen(lowcase_key); i < n; i++)
{
lowcase_key[i] = tolower(key[i]);
}
for (int i = 0, n = plaintext_length; i < n; i++)
{
if (islower(plaintext[i]))
{
output_text[i] = lowcase_key[plaintext[i] - 'a'];
}
if (!isalpha(plaintext[i]))
{
output_text[i] = plaintext[i];
}
if (isupper(plaintext[i]))
{
output_text[i] = lowcase_key[plaintext[i] - 'A'] + 'A' - 'a';
}
}
printf("ciphertext: %s", output_text);
printf("\n");
return 0;
}
还有更优雅的解决方案,虽然我很喜欢- 'A'] + 'A' - 'a'
部分,但今天就这样了。感谢大家的评论!