快来找丢失的两个数~~~~

 
题目大意:
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
 
思路分析:
   总共有n+2个数,丢失了两个数。我们把输入的n个数用数组a[maxn]存起来,用b[maxn]来模拟一个排序从1~n+2,用a与数组b做比较,如果比较得出了不相同,那就把数组b上这个位置上的数变为0,最后只要用一个循环找出前两个不为0的数即可。
 
源代码:
 1 #include<iostream>
 2 #define maxn 1010
 3 using namespace std;
 4 int main()
 5 {
 6     int t;
 7     cin >> t;
 8     while (t--)
 9     {
10         int n, a[maxn], b[maxn], i;
11         int count = 0;
12         cin >> n
13         for (i = 0; i < n; i++)
14             cin >> a[i];                  
15                 for (i = 1; i <= n + 2; i++)
16         {
17                 b[i] = i;                 //建立一个1~n+2的排序数组
18          
19               for (int j = 0; j<n; j++)
20                 if (b[i] == a[j])       //两个数组作比较,找出相同的标记为0
21                     b[i] = 0;
22         }
23         for (i = 1; i <= n + 2; i++)
24         {
25             if (b[i] != 0)
26             {
27 
28                 count++;                      //找出不为0的数
29                 if (count == 2)
30                 {
31                     cout << b[i] << endl;     //最后一个数输出时不用空格
32                     break;
33                 }
34                 cout << b[i] << " ";       
35             }
36 
37 
38         }
39 
40 
41     }
42     return 0;
43 
44 }

心得:

这样的思路以后应该还会用到,好好积累。刷题也可以带来乐趣(=_=关键是要会),好好加油吧!

跟这么多同届的同学,还有学姐学长一起学习,感觉很充实,但是自己平时问的题目还太少了,跟别人交流也太少了,要改改,因为这样才能学到更多的东西。进步才会更快~~~~~

 

转载于:https://www.cnblogs.com/Lynn0814/p/4652477.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值