fix two answers to make them correct (100 pts.)

This commit is contained in:
juan 2021-12-05 14:37:52 +08:00
parent a5d49c0c3f
commit e5ebc0c533
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
2 changed files with 12 additions and 5 deletions

View file

@ -1,5 +1,4 @@
// https://bbc3502.hopto.org/c2021/index.php/home-17/590-3
/* ⚠*** This code is not perfect *** */
#include <stdio.h>
//将只包含小写字母的字符串str中的元音字母复制到字符数组vowel,并返回元音字符的个数。
@ -23,14 +22,17 @@ int main() {
#include <string.h>
#pragma GCC optimize("Ofast,inline")
int getVowel(char str[], char vowel[]) {
char vowelstr[] = {'a', 'e', 'i', 'o', 'u', '\0'};
char vowelstr[] = {'a', 'e', 'i', 'o', 'u'};
int len = 0;
for (int i = 0, n = strlen(str); i < n; i++) {
for (int j = 0, m = strlen(vowelstr); j < m; j++) {
for (int j = 0, m = sizeof(vowelstr) / sizeof(char); j < m; j++) {
if (vowelstr[j] == str[i]) {
vowel[len++] = str[i];
}
}
}
// vowel[len] is a trash value, and will lead to undefined behavior in some
// cases
vowel[len] = '\0';
return len;
}

View file

@ -11,9 +11,14 @@ int main(void) {
// Process string
int count = 0;
for (int i = 0, n = strlen(str); i < n; i++) {
int mismatch = 0;
for (int i = 0, n = strlen(str); i < n && !mismatch; i++) {
if (str[i] == '(') {
count++;
if (count < 0) { // )(
mismatch = 1;
} else {
count++;
}
} else if (str[i] == ')') {
count--;
}