首页 > 其他 > 详细

比赛--找丢失的数--解题报告T

时间:2015-07-19 21:27:33      阅读:147      评论:0      收藏:0      [点我收藏+]

  找丢失的数

题目大意:

There is a permutation without two numbers in it, and now you know what numbers the permutation has. Please find the two numbers it lose.

要求:

Input

There is a number 技术分享 shows there are 技术分享 test cases below. (T<=10)  For each test case , the first line contains a integers N, which means the number of numbers the permutation has. In following a line , there are N distinct postive integers.(1<=N<=1000)
 

Output

For each case output two numbers , small number first.
 输入样例:

Sample Input

2
3
3 4 5
1
1
 

Sample Output

1 2
2 3
题目分析:
 找出丢失的两个数,从1开始。并且要从小到大输出。
程序代码:
 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 
 5 const int maxn = 1100;
 6 
 7 int T[10];
 8 int n[1000];
 9 int a[maxn];
10 
11 int main ()
12 {
13     int T;
14     scanf("%d",&T);
15     while(T--){
16         int n,i;
17         memset(a,0,sizeof(a));
18         scanf("%d",&n);
19         for( i=0; i<n; i++ )
20         {
21             int m;
22             scanf("%d",&m);
23             a[m]=1;
24         }
25         int f=1;
26         for( i=1;i<=n+2;i++)
27             if(!a[i])
28         {
29                 if (f)
30                 {
31                     cout<<i<<" ";
32                     f=0;
33                 }
34                 else
35                     cout<<i;
36         }
37             cout<<endl;
38     }
39     return 0;
40 }

心得:

这道题开始有一些思路,但是在写程序是遇到一些困难,后来百度了一下,发现用memset函数很简单,就又查了一些memset()的用法,知道了函数的作用是对较大的结构体或数组进行清零操作的一种快速方法。写这个程序学到了很多。

 
 

比赛--找丢失的数--解题报告T

原文:http://www.cnblogs.com/ttmj865/p/4657410.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!