Missing number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 93 Accepted Submission(s): 48
Problem Description
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
T
shows there are
T
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≤1,000 )
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≤1,000 )
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
Source
Recommend
不得不说,这个水题实在.....太坑了!BestCoder真的坑了我一回!
首先题目根本就没说排序是从1开始的好吗?我今天wa了半天。
他喵的,居然是从小到大排序,从1开始从小到大排序,他喵的根本就没说!!!!
那个n(n+1)/2是好像是根据等差数列的前n项和.至于求两个数字平方和.博主没写出来......狂汗!求大牛指教!Orz!!!!
ac之路任重而道远......各位Oier与君共勉!
中文题目在下面.
题解:
1001 Missing number 直接开一个数组记录某个数字是否出现过,就能找到没有出现的两个数字了。更好的空间为 O(1) 的做法是用 n(n+1)2 减去出现数字可以得到两个数字的和,同理一样的方法也可以得到两个数字的平方和,就能解出这两个数字了。
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for (int i = 0; i < t; i++)
{
int n = input.nextInt();
int a[] = new int[n];
for (int j = 0; j < n; j++)
{
a[j] = input.nextInt();
}
Arrays.sort(a);
int b[] = new int[n+2];
for(int j=0;j<n+2;j++)
{
b[j]=j+1;
}
boolean flag=false;
int l=0;
int x[] = new int[2];
for(int j=0;j<n+2;j++)
{
for(int k=0;k<n;k++)
{
if(b[j]==a[k])
{
flag=true;
break;
}
else
{
flag=false;
}
}
if(flag==false)
{
x[l++]=b[j];
}
}
System.out.println(x[0]+" "+x[1]);
}
}
}
Missing number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 748 Accepted Submission(s): 328
问题描述
小yb有一个排列,但他不小心弄丢了其中的两个数字。现在他告诉你他现在手上还有哪些数字,需要你告诉他他丢了哪两个数字。
输入描述
有多组数据,第一行为数据组数 T ( T≤10 )。 对于每组数据,第一行为一个正整数 n ,表示yb现在手上有的数字个数。 在接下来一行有 n 个整数,保证所有数字互不相同且合法。 1≤n≤1,000
输出描述
对于每组数据,输出两个数字,为排列缺少的两个数字,小的在前。
输入样例
2 3 3 4 5 1 1
输出样例
1 2 2 3