gpt4 book ai didi

c++ - 在 C++ 中查找非元音对

转载 作者:太空宇宙 更新时间:2023-11-04 14:46:58 25 4
gpt4 key购买 nike

我有一个编程问题,我应该编写一个程序来计算不是“a”、“e”、“i”、“o”的字母对(彼此相邻的字母)的数量, 'u'(元音)。

例子:

  • jas 0
  • olovo 0
  • skok 1 (sk)
  • stvarnost 4 (st, tv, rn, st)

输入由小写字母组成,使单词不超过 200 个字符,输出应输出非元音字母(a、e、i、o、u)的字母对数。

时间限制:

  • 1 秒

内存限制:

  • 64MB

我得到的问题中提供的示例:

  • 输入 skok
  • 输出 1

但是,当我输入单词“skok”时,程序无法运行(它似乎一直在后台运行,但没有在屏幕上显示任何内容)。然而,“stvarnost”(现实)这个词有效,显示“4”——正如问题中给出的那样。

在 10 个测试用例中,两个测试用例给出了正确的输出,一个给出了错误的输出,另外七个测试用例告诉我我已经超过了时间限制。

现在,我还想获得有关如何避免超过给定时间限制以及如何在下面的程序中修复它的建议。

这是我开始的代码:

#include <iostream>
#include <string.h>

using namespace std;

int main() {

char zbor[200];
cin.get(zbor, 200);
int length = strlen(zbor);
int j_value = 0;
int check_pairs = 0;
int pairs = 0;
int non_vowels = 0;

for (int i = 0; i < length; i++) {
if (zbor[i] == 'a' || zbor[i] == 'e' || zbor[i] == 'i' || zbor[i] == 'o' || zbor[i] == 'u') {
continue;
} else {
non_vowels++;
for (int j = i + 1; j < length; j++) {
if (zbor[j] == 'a' || zbor[j] == 'e' || zbor[j] == 'i' || zbor[j] == 'o' || zbor[j] == 'u') {
break;
} else {
non_vowels++;
if (non_vowels % 2 != 0) {
check_pairs = non_vowels / 2 + 1;
} else {
check_pairs = non_vowels / 2;
}
if (pairs < check_pairs) {
pairs++;
}
j_value = j;
}
}
i = j_value + 1;
}
}

cout << pairs;

return 0;
}

编辑:

#include <iostream>
#include <string.h>

using namespace std;

int main() {

char zbor[200];
cin.get(zbor, 200);
int length = strlen(zbor);
int pairs = 0;
int non_vowels = 0;

for (int i = 0; i < length; i++) {
if (zbor[i] == 'a' || zbor[i] == 'e' || zbor[i] == 'i' || zbor[i] == 'o' || zbor[i] == 'u') {
non_vowels = 0;
continue;
} else {
non_vowels++;
if (non_vowels >= 2) {
if (non_vowels % 2 != 0) {
pairs = non_vowels / 2 + 1;
} else if (non_vowels % 2 == 0) {
pairs = non_vowels / 2;
}
}
}
}

cout << pairs;

return 0;
}

编辑代码,使用下面答案的代码片段,(brunoOzzy)这是有效的最终版本:

#include <iostream>
#include <string.h>

using namespace std;

bool vowel(char c) {
switch(c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
default:
return false;
}
}

int main()
{

char zbor[200];
cin.get(zbor, 200);
int N = strlen(zbor);
int non_vowels = 0;
int pairs = 0;

for (int i = 0; i < N; i++) {
if (!vowel(zbor[i])) {
non_vowels = 0;
} else {
non_vowels++;
if (!vowel(zbor[i])) {
non_vowels = 0;
} else {
non_vowels++;
if (non_vowels > 1) {
pairs++;
}
}
}
}

cout << pairs;

return 0;
}

最佳答案

您可以通过使用 C++ 功能轻松简化您的代码,建议:

#include <algorithm>
#include <iostream>
#include <array>
#include <vector>

const std::array<char, 5> vowels = {'a', 'e', 'i', 'o', 'u'};

bool isVowel(char c)
{
return std::find(vowels.begin(), vowels.end(), c) != vowels.end();
}

bool checker(char c)
{
static bool lastFound = false;

if (lastFound && !isVowel(c))
return true;
else
lastFound = !isVowel(c);

return false;
}

int main()
{
std::vector<char> v{'s', 'k', 'o', 'k', 'a', 'k'};

int num_items = std::count_if(v.begin(), v.end(), &checker);

std::cout << num_items << std::endl;
}

关于c++ - 在 C++ 中查找非元音对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55142496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com