This commit is contained in:
juan 2021-12-05 10:54:29 +08:00
parent 198d437e1a
commit 4626b75d12
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
4 changed files with 120 additions and 0 deletions

View 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]);
}
}

View 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;
}
}

View 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;
}