sicily Stick

来源:互联网 发布:网络拓扑图算法 编辑:程序博客网 时间:2024/06/05 00:12

题目

Description

Anthony has collected a large amount of sticks for manufacturing chopsticks. In order to simplify his job, he wants to fetch two equal-length sticks for machining at a time. After checking it over, Anthony finds that it is always possible that only one stick is left at last, because of the odd number of sticks and some other unknown reasons. For example, Anthony may have three sticks with length 1, 2, and 1 respectively. He fetches the first and the third for machining, and leaves the second one at last. You task is to report the length of the last stick.

Input

The input file will consist of several cases.
Each case will be presented by an integer n (1<=n<=100, and n is odd) at first. Following that, n positive integers will be given, one in a line. These numbers indicate the length of the sticks collected by Anthony.
The input is ended by n=0.

Output

For each case, output an integer in a line, which is the length of the last stick.

Sample Input

31210

Sample Output

2

解题思路

简单得我都觉得不会有人来看这个了,虽然代码中直接用了set,但其实开一个bool数组应该会占用更少空间,更快

代码

#include <iostream>#include <set>#include <iterator>using namespace std;int main(int argc, const char * argv[]) {    int n;    set<int> stick_lengths;    set<int>::iterator iter;    while (1) {        cin >> n;        if (n == 0) break;        int t_length;        while (n--) {            cin >> t_length;            iter = stick_lengths.find(t_length);            if (iter == stick_lengths.end()) {                stick_lengths.insert(t_length);            } else {                stick_lengths.erase(iter);            }        }        cout << *stick_lengths.begin() << endl;        stick_lengths.clear();    }    return 0;}
0 0
原创粉丝点击