sicily 1200欢迎提出优化方案

水题来的……我的做法是用a[10]数组表示每个数字出现的次数。

1200. Stick

限制条件

时间限制: 1 秒, 内存限制: 32 兆

题目描述

 

 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. 

 

输入格式

 

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. 

 

输出格式

 

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

 

样例输入

3

1

2

1

0

样例输出

2

题目来源

ZSUACM Team Member

 1 #include<iostream>

 2 #include<stdio.h>

 3 using namespace std;

 4 int main()

 5 {

 6    int n,a[101];

 7    while(cin>>n && n){

 8      for(int i=0;i<n;i++) cin>>a[i];

 9      for(int i=0;i<n-1;i++) if(a[i]){

10           for(int j=i+1;j<n;j++)

11                         if(a[i]==a[j]){

12                               a[i]=0;a[j]=0;break;

13                             }

14            continue;

15             }

16       for(int i=0;i<n;i++) if(a[i]) cout<<a[i]<<endl;

17      }

18     return 0;

19 }
View Code

 

你可能感兴趣的:(优化)