题目大意:
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 }
心得:
这样的思路以后应该还会用到,好好积累。刷题也可以带来乐趣(=_=关键是要会),好好加油吧!
跟这么多同届的同学,还有学姐学长一起学习,感觉很充实,但是自己平时问的题目还太少了,跟别人交流也太少了,要改改,因为这样才能学到更多的东西。进步才会更快~~~~~