【 OJ 】查找最大

 2023-09-05 阅读 218 评论 0

摘要:查找最大 发布时间: 2017年11月12日 12:31 最后更新: 2017年11月12日 12:34 时间限制: 1000ms 内存限制: 128M 描述   对于输入的每个字符串,查找其中的最大字母(大小写形式认为一样大),在该字母后面插入字符串“(max)”。 输入   输入数据包括多个测试实例

查找最大

发布时间: 2017年11月12日 12:31   最后更新: 2017年11月12日 12:34   时间限制: 1000ms   内存限制: 128M

描述

  对于输入的每个字符串,查找其中的最大字母(大小写形式认为一样大),在该字母后面插入字符串“(max)”。

输入

  输入数据包括多个测试实例,每个实例由一行长度不超过100的字符串组成,字符串仅由大小写字母构成。

输出

  对于每个测试实例输出一行字符串,输出的结果是插入字符串“(max)”后的结果,如果存在多个最大的字母,就在每一个最大字母后面都插入"(max)"。

样例输入1
abcdefgfedcba
xxxxx
样例输出1
abcdefg(max)fedcba
x(max)x(max)x(max)x(max)x(max)
源代码
丢三落四得我发的第四遍才过。。
第一遍错在 A-Z时 将max直接+=32。。很春虫虫。。
第二遍错在 忘了考虑Aa的输出情况 出现 A(max)a的情况。。。
第三遍更弱智。。不说了。。
我突然想到另外一种实现,实现了就发出来
 1 #include <stdio.h>
 2 int main(){
 3     int n;
 4     int i;
 5     char max,max2;
 6     char a[100];
 7 
 8     while(gets(a)){
 9         i = 0;
10         max = a[0];
11         while(a[i] != '\0'){
12             if(a[i] >= 'A' && a[i] <= 'Z'){
13                 if(max < a[i]+32){
14                     max = a[i]+32;
15                 }
16             }else{
17                 if(max < a[i]){
18                     max = a[i];
19                 }
20             }
21             i++;
22         }
23         i = 0;
24         max2 = max - 32;
25         while(a[i] != '\0'){
26             printf("%c",a[i]);
27             if(a[i] == max || a[i] == max2)
28                 printf("(max)");
29             i++;
30         }
31         printf("\n");
32     }
33     return 0;
34 }

  说好的第二种实现,其实也没改多少。。

 1 #include <stdio.h>
 2 int main(){
 3     int n;
 4     int i;
 5     char max,max2;
 6     char a[100];
 7 
 8     while(gets(a)){
 9         i = 0;
10         max  = 'a';
11         max2 = 'A';
12         while(a[i] != '\0'){
13             if(a[i] > max){
14                 max  = a[i];
15                 max2 = a[i] - 32;
16             }else if(a[i] > max2 && a[i] < 'Z'){   // 这里很坑,可能会出现 max = g,max2 = G,a[i] = b;此时仍会进入else if然后就崩了。。所以一定要限制a[i]的范围
17                 max2 = a[i];
18                 max  = a[i] + 32;
19             }
20             i++;
21         }
22         i = 0;
23         while(a[i] != '\0'){
24             printf("%c",a[i]);
25             if(a[i] == max || a[i] == max2)
26                 printf("(max)");
27             i++;
28         }
29         printf("\n");
30     }
31     return 0;
32 }

 

 

转载于:https://www.cnblogs.com/forfriendforfun/p/8035173.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/108.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息