zoukankan      html  css  js  c++  java
  • 查找最大元素



    Problem Description
    对于输入的每个字符串,查找其中的最大字母,在该字母后面插入字符串“(max)”。
     
    Input
    输入数据包括多个测试实例,每个实例由一行长度不超过100的字符串组成,字符串仅由大小写字母构成。
     
    Output
    对于每个测试实例输出一行字符串,输出的结果是插入字符串“(max)”后的结果,如果存在多个最大的字母,就在每一个最大字母后面都插入"(max)"。
     
    Sample Input
    abcdefgfedcba xxxxx
     
    Sample Output
    abcdefg(max)fedcba
    x(max)x(max)x(max)x(max)x(max)
         有一点小技巧就是从后面往前查找位置。这样位置之间就不会相互影响。
      
     1 #include<bits/stdc++.h>
     2 #define LL long long
     3 using namespace std;
     4 int main()
     5 {
     6     string s;
     7     while(cin>>s)
     8     {
     9        // string s1=s;
    10         int l=s.size();int max_=-10;
    11         for(int i=0;i<l;++i)
    12         {
    13             if(max_<s[i]) max_=s[i];
    14         }
    15         for(int i=l-1;i>=0;--i)
    16         {
    17             if(s[i]==max_) {
    18                     s.insert(i+1,"(max)");
    19             }
    20         }
    21         cout<<s<<endl;
    22     }
    23   }
    View Code
         
  • 相关阅读:
    MYSQL
    数据中台的思考与总结
    基于SpringCloud分布式架构
    java常用工具库使用
    String性能提升10倍的几个方法
    Validator 注解使用
    【数据结构】trie树
    几种排序算法小结
    Core data的batch update
    UIScrollView和UIPageControl结合实现图片浏览
  • 原文地址:https://www.cnblogs.com/Auroras/p/10799348.html
Copyright © 2011-2022 走看看