從一個無序的陣列中查詢二個遺漏的數
阿新 • • 發佈:2019-02-19
{
publicstaticfinalint HOWMANY=10;
publicstaticboolean whetherStep;
//生成這樣一個序列(a[1]到a[8]存放的是1~10少掉兩個數,a[0]不使用,a[9]和a[10]各用掉一個空間),並給出少掉的那兩個數
publicstaticint[] fill(int[]kaka){
ArrayList al=new ArrayList();
for(int i=1;i<=HOWMANY;i++){
al.add(new Integer(i));
}
for(int i=1;i<= HOWMANY-2;i++){
int pos=(int)(Math.random()*al.size());
kaka[i]=((Integer)al.get(pos)).intValue();
al.remove(pos);
}
kaka[HOWMANY-1]=-1;
kaka[HOWMANY]=-2;
int[]ret=newint[2];
ret[0]=((Integer)al.get(0)).intValue();
ret[1]=((Integer)al.get(1)).intValue();
return ret;
}
//本演算法的核心部分,swap用掉一個空間
publicstaticint[] swap(int[]a,int i,int[]index){
//交換a[i]和a[a[i]];
int space2=a[i];
a[i]=a[space2];
a[space2]=space2;
if(a[i]==-1){
int[]ret=newint[2];
ret[0]=i;
ret[1]=index[1];
return ret;
}else{
if(a[i]==-2){
int[]ret =newint[2];
ret[0]=index[0];
ret[1]=i;
return ret;
}else{
return index;
}
}
}
//為了能看清楚其執行的步驟,按一下回車,就輸出一次現在的陣列的情況。
publicstaticvoid pr(int[]a){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
br.readLine();
for(int i=1;i<a.length;i++){
System.out.print(i+"");
}
System.out.println();
printArray(a);
}catch(Exception e){}
}
//本演算法的核心部分
publicstaticint [] find(int[]a){
int[] space1=newint[2];
space1[0]=HOWMANY-1;
space1[1]=HOWMANY;
int i=1;
while(i<=HOWMANY){
if(whetherStep){
pr(a);
}
if (a[i] != i&&space1[0]!=i&&space1[1]!=i) {
//這樣交換意思是為了把i這個數放到a[i]這個位置上去
//最後找出來時,應該是陣列中i全都放到a[i]中,
//跟蹤那兩個空位(即存放-1和-2的地方),就是要找的兩個數
space1=swap(a,i,space1);
}
else{
//如果a[i]==i,即已經就位了,就下一個,
//如果space1[0]==i,那麼a[i]存放的一定是-1,也沒法交換,先放在這裡,處理下一個
//如果space1[1]==i,那麼a[i]存放的一定是-2,也沒法交換,先放在這裡,處理下一個
i++;
}
}
return space1;
}
//打印出陣列
publicstaticvoid printArray(int[]a){
for(int i=1;i<a.length;i++){
System.out.print(a[i]+"");
}
System.out.println();
}
publicstaticvoid main(String[]args){
int []h=newint[HOWMANY+1];
int[]left=fill(h);
System.out.println("the lost two numbers are "+left[0]+" and "+left[1]);
printArray(h);
System.out.println(" ********************************");
System.out.println("缺兩個數的陣列已經給出,現在選擇要不要看分步執行情況[y/n]");
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String ans=br.readLine();
if(ans.trim().equals("y")||ans.trim().equals("Y")){
whetherStep=true;
}else{
whetherStep=false;
}
}catch(Exception e){}
int[]lost=find(h);
System.out.println("now ,find Both of the lost numbers, they are "+lost[0]
+" and "+lost[1]);
}
}
publicstaticfinalint HOWMANY=10;
publicstaticboolean whetherStep;
//生成這樣一個序列(a[1]到a[8]存放的是1~10少掉兩個數,a[0]不使用,a[9]和a[10]各用掉一個空間),並給出少掉的那兩個數
publicstaticint[] fill(int[]kaka){
ArrayList al=new ArrayList();
for(int i=1;i<=HOWMANY;i++){
al.add(new Integer(i));
}
for(int i=1;i<=
int pos=(int)(Math.random()*al.size());
kaka[i]=((Integer)al.get(pos)).intValue();
al.remove(pos);
}
kaka[HOWMANY-1]=-1;
kaka[HOWMANY]=-2;
int[]ret=newint[2];
ret[0]=((Integer)al.get(0)).intValue();
ret[1]=((Integer)al.get(1)).intValue();
return ret;
//本演算法的核心部分,swap用掉一個空間
publicstaticint[] swap(int[]a,int i,int[]index){
//交換a[i]和a[a[i]];
int space2=a[i];
a[i]=a[space2];
a[space2]=space2;
if(a[i]==-1){
int[]ret=newint[2];
ret[0]=i;
ret[1]=index[1];
return ret;
}else{
if(a[i]==-2){
int[]ret
ret[0]=index[0];
ret[1]=i;
return ret;
}else{
return index;
}
}
}
//為了能看清楚其執行的步驟,按一下回車,就輸出一次現在的陣列的情況。
publicstaticvoid pr(int[]a){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
br.readLine();
for(int i=1;i<a.length;i++){
System.out.print(i+"");
}
System.out.println();
printArray(a);
}catch(Exception e){}
}
//本演算法的核心部分
publicstaticint [] find(int[]a){
int[] space1=newint[2];
space1[0]=HOWMANY-1;
space1[1]=HOWMANY;
int i=1;
while(i<=HOWMANY){
if(whetherStep){
pr(a);
}
if (a[i] != i&&space1[0]!=i&&space1[1]!=i) {
//這樣交換意思是為了把i這個數放到a[i]這個位置上去
//最後找出來時,應該是陣列中i全都放到a[i]中,
//跟蹤那兩個空位(即存放-1和-2的地方),就是要找的兩個數
space1=swap(a,i,space1);
}
else{
//如果a[i]==i,即已經就位了,就下一個,
//如果space1[0]==i,那麼a[i]存放的一定是-1,也沒法交換,先放在這裡,處理下一個
//如果space1[1]==i,那麼a[i]存放的一定是-2,也沒法交換,先放在這裡,處理下一個
i++;
}
}
return space1;
}
//打印出陣列
publicstaticvoid printArray(int[]a){
for(int i=1;i<a.length;i++){
System.out.print(a[i]+"");
}
System.out.println();
}
publicstaticvoid main(String[]args){
int []h=newint[HOWMANY+1];
int[]left=fill(h);
System.out.println("the lost two numbers are "+left[0]+" and "+left[1]);
printArray(h);
System.out.println(" ********************************");
System.out.println("缺兩個數的陣列已經給出,現在選擇要不要看分步執行情況[y/n]");
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String ans=br.readLine();
if(ans.trim().equals("y")||ans.trim().equals("Y")){
whetherStep=true;
}else{
whetherStep=false;
}
}catch(Exception e){}
int[]lost=find(h);
System.out.println("now ,find Both of the lost numbers, they are "+lost[0]
+" and "+lost[1]);
}
}