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
         
  • 相关阅读:
    设计模式---工厂模式和抽象工厂模式
    设计模式---简单工厂模式
    设计模式---设计模式的分类及六大原则
    分布式---Raft算法
    分布式---Paxos算法
    分布式---CAP和BASE理论
    分布式---分布式事务
    分布式---分布式锁
    css
    react生命周期
  • 原文地址:https://www.cnblogs.com/Auroras/p/10799348.html
Copyright © 2011-2022 走看看