程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 一道c++小編程題,

一道c++小編程題,

編輯:C++入門知識

 

<span style="font-family: 宋體, Arial, Helvetica, san-serif;">題目:</span>  
編寫一個小程序,從標准輸入讀入一系列string對象,尋找連續重復出現的單詞,程序應該找出滿足以下條件的單詞的輸入位置:該單詞的後面緊跟著再次出現自己本身,跟 蹤重復次數量多的單詞及其重復次數.輸出重復次數的最大值,
例如.如果輸入是:
how now now now brown cow cow
則輸出表明now這個單詞出現了三次

  本人解法:  
#include <iostream>  
#include <string>  
#include <cctype>  
#include <vector>  
using namespace std;  
int main() { <pre name="code" class="cpp"><span style="white-space:pre">    </span>string s;  
    vector<string> vec;  
    int maxRepeat = 0;  
    while (cin >> s){  
        if(ispunct(s[s.size()-1])) s = s.substr(0,s.size() - 1);  //去除最後的標點符號  
        vec.push_back(s);  
    }  
    vector<string>::size_type i = 0;  
    vector<int> vecInt(vec.size(),1);  
    while (i != vec.size() - 1){  
        int j = i;  
        string str = vec[i];  
        while(i != vec.size() - 1)  
        {  
            if(str == vec[++i]) vecInt[j] ++;  
            else break;  
        }  
    }  
    vector<int>::size_type k = 0;  
    int max = vecInt[k];  
    int flag = 0;  
    while (k != vecInt.size() - 1){  
        if(max < vecInt[++k]){  
            max = vecInt[k];  
            flag = k;  
        }  
    }  
    cout << "The word of " <<  vec[flag] << " repeats: " << vecInt[flag] << " times." << endl;  
    cout << endl;</pre><pre name="code" class="cpp"><pre name="code" class="cpp"> return 0;  
  
}</pre>  
<pre></pre>  
<pre></pre>  
<p>自己的解法答案是對的 但從空間復雜度來說要復雜的多,所以在網上搜了下,尋找到另外一種解決辦法,修改後的代碼如下:</p>  
<p></p>  
<pre name="code" class="cpp">#include <iostream>  
#include <string>  
#include <cctype>  
using namespace std;  
int main() { </pre><pre name="code" class="cpp"><pre name="code" class="cpp"><span style="white-space:pre"> </span>string nowWord,beforeWord,result;  
    cin >> nowWord;  
    if(ispunct(nowWord[nowWord.size() - 1]))nowWord = nowWord.substr(0,nowWord.size() - 1);  
    result = beforeWord = nowWord;  
    int count = 1,maxCount = 1;  
    while(cin >> nowWord){  
        if(ispunct(nowWord[nowWord.size() - 1]))nowWord = nowWord.substr(0,nowWord.size() - 1);  
        if (beforeWord == nowWord){  
            count++;  
        }  
        else{  
            beforeWord = nowWord;  
            count = 1;  
        }  
        if(count > maxCount){  
            maxCount = count;  
            result = nowWord;  
        }  
    }  
    if(maxCount == 1) cout << "There is no word repeating." << endl;  
    else cout << "The word of " << result << " repeats: " << maxCount << " times." << endl;  
    return 0;  
  
}</pre>  
<pre></pre>  
<p></p>  
<pre></pre>  
特在此分享給大家 參考或給予好的想法參考改進。。謝謝~~  
<p></p>  
<p></p>  
<p><span style="font-family:宋體,Arial,Helvetica,san-serif; font-size:14px; line-height:22.390625px"></span></p>  
<p><span style="font-family:宋體,Arial,Helvetica,san-serif; font-size:14px; line-height:22.390625px"></span></p>  
<pre></pre>  
<pre></pre>  
  
</pre></pre>  

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved