天天看點

2005. Lovely Number2005. Lovely NumberConstraintsDescriptionInputOutputSample InputSample OutputProblem Source

Submit

2005. Lovely Number

Constraints

Time Limit: 1 secs, Memory Limit: 64 MB

Description

Every time after got a solution from kevin, ivan repeats again and again,"Are there any better ones?". It seems more worse this time. Given a sequence of numbers, which ivan is sure that all of them appear even times except only one "lovely number" appears odd times, kevin must find out the lovely number as soon as possible. So, let's rock!

Input

There are multiple test cases.

Each test case contains two lines. The first line is an integer N, 1 <= N <= 10001, and N % 2 = 1. The second line contains N integers, separated by spaces.

Input is terminated by EOF.

Output

For each test case, output a line of an integer, which should be the lovely number.

Sample Input

3
1 2 1
5
7 7 7 7 3
7
2 2 4 4 4 2 2      

Sample Output

2      
3      
4      

Problem Source

2010中山大學新手賽-網絡預選賽 [email protected]

【解題方案】

這個題目本身是一道水題,就是把出現偶數次數的數字挑出來。

值得一提的是這個題使用了Vector來減小資源消耗,本想開一個大容量的數組。但是不好判斷目前的數目,而且題目中未講明數不會為0。是以使用了vector。

如果之後的輸入在vector中,則将vector中該元素删掉。如果沒有在vector中則将該元素添加到vector中。這樣就巧妙的獎出現次數為奇數的元素留在了vector中。

【遇到問題】

這次很快将解決方案做出來,實作了挑選odd次數的數的功能。然而在如何結束程式上出了問題。

題目要求“Input is terminated by EOF.”,我不知道如何解決EOF。(End Of File,在電腦的術語縮寫通常為 EOF,在作業系統決定資料源無更多的資料可讀取。資料源通常稱為檔案或串流。)

然後通過查資料,将循環條件改為while(!cin.eof()),便實作了該功能。

然後出現wrong answer,我将輸入num<=0,的情況改為跳出循環,即AC了,也不知道是不是這裡的問題

【源代碼】

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector <int> v;
    while(!cin.eof())
    {                    
            int num = 0;
            cin>>num;
            if(num <= 0)break;
            while(num > 0 )
            {
                      num--;
                      int temp = 0;
                      cin>>temp;
                      if(v.size() == 0)
                      {
                                  v.push_back(temp);
                                  continue;//結束本次循環 
                      }
                      
                      for(int i=0;i<v.size();i++)
                      {                              
                              if(v[i]==temp)
                              {
                                      v.erase(v.begin()+i);
                                      break;
                              }
                              if(i==v.size()-1)
                              {
                                       v.push_back(temp);
                                       break;                 
                              }                              
                      }
                     
            }
            cout<<v[0]<<endl;
            v.clear();
    }
return 0;
}
                        
           

【通過界面】

2005. Lovely Number2005. Lovely NumberConstraintsDescriptionInputOutputSample InputSample OutputProblem Source

通過時間略長不知道是哪裡的原因

繼續閱讀