杭电2025 查找最大元素

  链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2025

  题目的意思很简单,应该不是什么大问题。然后因为最近在练习STL的用法,所以就想用链表来完成插入的操作。然后附上用STL写的代码:

#include <iostream>
#include<math.h>
#include <iomanip>
#include<cstdio>
#include<string>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<stdlib.h>
#include<iterator>
using namespace std;


int main()
{
   string input;
   //int n;
  // cin>>n;
   //
   while(getline(cin,input))
   {
       //cin.get();
       int len1=input.size();

       list<char>word;

       list<int>num;
       for(int i=0;i<len1;i++)
       {
           word.push_back(input[i]);//把读到的字符串全部放到链表中
            //num.push_back((int)input[i]);
       }
        list<char>::iterator pr;
        list<char>::iterator pr2;
        pr=max_element(word.begin(),word.end());//找到最大元素的下标;

        char big;
        big=*pr;
         pr=word.begin();
        pr2=word.begin();


        for(pr=word.begin();pr!=word.end();pr++)
        {
            if(*pr==big)
            {

                 pr2=++pr;

                word.insert(pr2,'(');

                word.insert(pr2,'m');

                word.insert(pr2,'a');

                word.insert(pr2,'x');

                word.insert(pr2,')');
//因为链表在插入过程中迭代器是不会变的(这和string类不同 所以上面我可以用 。end()来判断结尾) pr这时候指向的是(max)中的‘)’的后一位,又因为我之后pr要++
                pr--;//所以这时候让pr指向‘)’,那下一个元素就是max元素的下一个了
            }
        }

        for(pr=word.begin();pr!=word.end();pr++)
        {
            cout<<*pr;
        }
        cout<<endl;

   }

    return 0;
}

  然后在ac之后我去看了下别人的代码,发现string类自带插入功能,但是它在插入的时候是长度和迭代器都会发生变化的,应该是类似vector<char>一样的存在,但是这个代码没用到迭代器,用的是数,所以还是在循环的时候用.size()比较好,因为长度在不断变化嘛~(当然这也提醒了我们下次必要乱用那个size())接下来附上代码:

  

#include <iostream>
#include<string>
using namespace std;

int main(void)
{
    string input_str,insert_str="(max)";
    int lenght;
    char max_char;
    while(cin>>input_str)
    {
        lenght=input_str.size();
        max_char=input_str[0];
        //求出字符串中的最大字符
        for(int i=1; i<lenght; i++)
        {
            if(input_str[i]>max_char)
            {
                max_char=input_str[i];
            }
        }

        /*注意:在这里j的范围必须写成j<input_str.size(),不能用lenght代替。
        因为下面插入字符串后,整个字符串长度一直在变化
        */
        for(int j=0; j<input_str.size(); j++)
        {
            if(input_str[j]==max_char)
            {
                input_str.insert(j+1,insert_str);
                //插入的(max)占据了5个位置,所以j在扫描时候直接跳过
                j+=5;
            }
        }

        cout << input_str << endl;
    }

    return 0;
}

  再之后附上是string类的一些应用,通过代码体现:

#include <iostream> 
#include <string> 
using std::cout; 
using std::endl; 
using std::string; 
int main(void){ 
    string str1="We can insert a string"; 
    string str2="a str into "; 
         //在字符串指定位置前面插入指定字符串 
    cout <<str1.insert(14,str2)<<endl; 
    //在字符串指定位置前面插入指定字符串的子串(从指定索引开始的指定个数的字符) 
    cout <<str1.insert(14,str2,2,9)<<endl; 
    //插入指定字符串的前n个字符 
    cout <<str1.insert(14,"test hello",5)<<endl; 
    //插入n个相同字符到字符串中 
    cout <<str1.insert(14,6,'*')<<endl; 

    //替换指定索引开始的指定长度的子串 
    cout <<str1.replace(3,3,"may")<<endl; 
    //用给定字符串的指定子串来进行替换 
    //如下,实际上使用的是could来进行替换 
           cout <<str1.replace(3,3,"can could",4,5)<<endl; 
    //使用给定字符串的前n个字符来进行替换:can 
    cout <<str1.replace(3,5,"can could",3)<<endl; 
    //使用指定个数的重复字符来进行替换 
    cout <<str1.replace(3,3,5,'*')<<endl; 

    string word="We"; 
    size_t index=str1.find(word); 
    if(index!=string::npos) 
    //删除指定索引开始的指定长度的字符 
    cout <<str1.erase(index,word.length())<<endl; 
    return 0; 

}

要详细看string类的解法请转向:http://blog.csdn.net/always2015/article/details/45508527

原文地址:https://www.cnblogs.com/William-xh/p/6838170.html