add more
This commit is contained in:
parent
198d437e1a
commit
4626b75d12
38
semester1/pset12/1-1-ReverseChar.c
Normal file
38
semester1/pset12/1-1-ReverseChar.c
Normal file
|
@ -0,0 +1,38 @@
|
|||
#include <stdio.h>
|
||||
#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 <stdlib.h>
|
||||
#include <string.h>
|
||||
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]);
|
||||
}
|
||||
}
|
47
semester1/pset12/1-2-count.c
Normal file
47
semester1/pset12/1-2-count.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
35
semester1/pset12/1-3-INCORRECT-GetVowel.c
Normal file
35
semester1/pset12/1-3-INCORRECT-GetVowel.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/* ⚠*** This code is not perfect *** */
|
||||
#include <stdio.h>
|
||||
|
||||
//将只包含小写字母的字符串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 <string.h>
|
||||
#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;
|
||||
}
|
Loading…
Reference in a new issue