diff --git a/semester1/pset11/2-2storage.c b/semester1/pset11/2-2-storage.c similarity index 100% rename from semester1/pset11/2-2storage.c rename to semester1/pset11/2-2-storage.c diff --git a/semester1/pset12/1-1-ReverseChar.c b/semester1/pset12/1-1-ReverseChar.c new file mode 100644 index 0000000..053e6fc --- /dev/null +++ b/semester1/pset12/1-1-ReverseChar.c @@ -0,0 +1,38 @@ +#include +#pragma GCC optimize("Ofast,inline") + +//将字符串str中的内容反向打印的函数 +void reversePrint(char str[]); + +int main() { + char s[100]; + + scanf("%s", s); + reversePrint(s); + printf("\n"); + + return 0; +} + +/* 请在这里填写答案 */ +#include +#include +void reversePrint(char str[]) { + if (str == NULL) { + return; + } + if (strlen(str) == 1) { + printf("%c", str[0]); + } else { + char *tmp = malloc(sizeof(char) * strlen(str)); + /* Why this: + * char *tmp = malloc(sizeof(char) * (strlen(str) - 1)); + * doesn't work? */ + for (int i = 1, n = strlen(str), j = 0; i < n; i++, j++) { + tmp[j] = str[i]; + } + reversePrint(tmp); + free(tmp); + printf("%c", str[0]); + } +} diff --git a/semester1/pset12/1-2-count.c b/semester1/pset12/1-2-count.c new file mode 100644 index 0000000..87122cb --- /dev/null +++ b/semester1/pset12/1-2-count.c @@ -0,0 +1,47 @@ +#include + +#define MAXLEN 110 + +int isPrime(int n); //判断一个整数是否是质数,是则返回1,否则返回0 +int getResult(char word[]); + +int main() { + char word[MAXLEN]; + + scanf("%s", word); + printf("%d\n", getResult(word)); + + return 0; +} + +/* 请在这里填写答案 */ +int getResult(char word[]) { + int hashtable[26] = {}; + + // Load word into hash table + for (int i = 0; word[i] != '\0'; i++) { + hashtable[word[i] - 'a']++; + } + + // Analyze words + int max = 0; + int min = MAXLEN; + for (int i = 0; i < 26; i++) { + // If hashtable[i] == 0, the letter is not in the word. + if (hashtable[i] == 0) { + continue; + } + if (hashtable[i] > max) { + max = hashtable[i]; + } + if (hashtable[i] < min) { + min = hashtable[i]; + } + } + + if (isPrime(max - min)) { + return max - min; + } else { + return -1; + } +} diff --git a/semester1/pset12/1-3-INCORRECT-GetVowel.c b/semester1/pset12/1-3-INCORRECT-GetVowel.c new file mode 100644 index 0000000..d562652 --- /dev/null +++ b/semester1/pset12/1-3-INCORRECT-GetVowel.c @@ -0,0 +1,35 @@ +/* ⚠*** This code is not perfect *** */ +#include + +//将只包含小写字母的字符串str中的元音字母复制到字符数组vowel,并返回元音字符的个数。 +int getVowel(char str[], char vowel[]); + +int main() { + char vowel[101], str[101]; //每个数组都至少要101个字节 + int len; + + scanf("%s", str); //读入字符串 + len = getVowel(str, vowel); //复制 + if (len > 0) + printf("%d %s\n", len, vowel); //输出复制后结果 + else + printf("%d\n", len); //仅输出长度 + + return 0; +} + +/* 请在这里填写答案 */ +#include +#pragma GCC optimize("Ofast,inline") +int getVowel(char str[], char vowel[]) { + char vowelstr[] = {'a', 'e', 'i', 'o', 'u'}; + int len = 0; + for (int i = 0, n = strlen(str); i < n; i++) { + for (int j = 0; j < 5; j++) { + if (vowelstr[j] == str[i]) { + vowel[len++] = str[i]; + } + } + } + return len; +}