【 OJ 】查找最大

查找最大

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

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

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

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

abcdefgfedcba
xxxxx
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] != ''){
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] != ''){
26             printf("%c",a[i]);
27             if(a[i] == max || a[i] == max2)
28                 printf("(max)");
29             i++;
30         }
31         printf("
");
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] != ''){
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] != ''){
24             printf("%c",a[i]);
25             if(a[i] == max || a[i] == max2)
26                 printf("(max)");
27             i++;
28         }
29         printf("
");
30     }
31     return 0;
32 }
道阻且长,行则将至。
原文地址:https://www.cnblogs.com/forfriendforfun/p/8035173.html