2005. Lovely Number

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中山大学新手赛-网络预选赛 ivankevin@argo


【解题方案】

这个题目本身是一道水题,就是把出现偶数次数的数字挑出来。

值得一提的是这个题使用了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;
}
                        

【通过界面】



通过时间略长不知道是哪里的原因






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值