木棒

来源:互联网 发布:人事调配网络 编辑:程序博客网 时间:2024/05/02 01:10
Problem 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
3
1
2
1
0
Sample Output

2

/*map容器是映射容器,提供一对一的映射*/#include<iostream>#include<map>using namespace std;int main(){//freopen("b.txt","r",stdin);int n,x;while(scanf("%d",&n)==1&&n){map<int,int>mp;//map容器的声明,map<int,int>::iterator it;//map容器迭代器的声明while(n--){scanf("%d",&x);mp[x]++;}//while循环完后,mp[1]=2,mp[2]=1;for(it=mp.begin();it!=mp.end();it++)//用迭代器对mp的所以元素遍历{if(it->second%2==1)//如果某元素对应的值为级数,如在此题中,mp[2]=1{printf("%d\n",it->first);//输出该元素,在此题中输出2break;}}}return 0;}


0 0
原创粉丝点击