酷代码 AI
菜单
服务商

给定一个长度为 n 的字符串,我们想要得到更多的"our"字符串,可以往字符串中添加任意个字符’u’或者’U’。注意:比较不区分大小写,即 “our”、“Our”、“oUr”、“OUr” 都是等价的,即他们都可以看成"our"字符串。 输入格式: 一个字符串 s(1<=s的长度<=3000) ,含义如上,仅包含大小写字母。 输出格式: 最多可以得到多少个"our"字符串(不区分大小写)。用C语言 实现代码

```c #include <stdio.h> #include <string.h> #include <ctype.h> int main() { char s[3001]; scanf("%s", s); int count_o = 0, count_ou = 0, count_our = 0; for (int i = 0; s[i] != '\0'; i++) { char c = tolower(s[i]); if (c == 'o') { count_o++; } else if (c == 'u') { if (count_o > 0) { count_ou++; count_o--; } } else if (c == 'r') { if (count_ou > 0) { count_our++; count_ou--; } } } printf("%d\n", count_our); return 0; } ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 1. 字符串长度限制要符合题目要求,数组大小需要足够容纳输入数据 2. 字符比较时要注意大小写问题,使用tolower统一处理 3. 状态计数变量需要正确维护,确保在匹配过程中不会出现负数 [2025-10-30 19:20:36 | AI写代码神器 | 257点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多