• C++primer习题6.12找出重复单词


    看了答案之后,我表示我考虑多了,写复杂了,但是还是贴出来吧:

    the code in c++ primer :

    #include <iostream>
    #include<vector>
    #include<string>

    using namespace std;

    int main()
    {
      string preword, currword,repword;
      int currCnt = 0, maxCnt =1;
      while (cin >> currword)
      {
          if (currword == preword)
          {
              ++currCnt;
          }
          else
          {
              if (currCnt > maxCnt)
              {
                  maxCnt = currCnt;
                  repword =preword;
               
              }
              currCnt = 1;
          }
          preword = currword;
      }
      if (maxCnt != 1)
      {
          cout << repword << " " << maxCnt <<endl;
      }
      else
      {
          cout << "no repeated word." << endl;
      }
    }
    mine :
    #include <iostream>
    #include<vector>
    #include<string>
    
    using namespace std;
    struct note
    {
    	int local;
    	int re_num;
    };
    int main()
    {
    	vector<string> stc;
    	string s; 
    	cout << "Please input the strings gapping with balck space and end with 'ctrl+A' "<<endl;
    	while(cin >> s)
    		stc.push_back(s);
    	int j = 0 , num = 0, size = stc.size();
    	vector<note> ivec;
    	for(size_t i = 0; i != size-1; ++i)
    	{
    		num = 1;
    	    j = i+1;
    		while(j != size-1 && stc[i] == stc[j++])
    		{
    			++num;
    		}
    		if (num != 0)
    		{
    			note a = {i,num};
    			ivec.push_back(a);
    		}
    	}
    	note re_max = ivec[0];
    	for (size_t i = 1; i < ivec.size(); ++i)
    	{
           if (re_max.re_num < ivec[i].re_num)
           {
    		   re_max = ivec[i];
           }
    	}
    	cout << "The word : " << stc[re_max.local] << " repeat " << re_max.re_num << " times" << endl;
    }
    
  • 相关阅读:
    Python-爬虫-解析库(pyquery)的使用
    Python-爬虫-解析库(Beautiful Soup)的使用
    Python-爬虫-基本库(requests)使用-抓取猫眼电影Too100榜
    Python-爬虫-基本库(requests)使用
    Go基础及语法(五)
    Go基础及语法(四)
    Go基础及语法(三)
    Go基础及语法(二)
    Go基础及语法(一)
    MySQL8.0随笔整理
  • 原文地址:https://www.cnblogs.com/xiangshancuizhu/p/2090735.html
Copyright © 2020-2023  润新知