找丢失的数
题目大意:
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()的用法,知道了函数的作用是对较大的结构体或数组进行清零操作的一种快速方法。写这个程序学到了很多。
时间: 2024-10-19 23:32:29