/**
* an fault : topN(int[],int) method will
* change the value of the argument int[]
*
*/
package com.yatium;
import java.util.*;
public class TestArray{
/* exchange two element with given indexes of an array */
public static void exchEleArr(int[] arr,int index1,int index2){
int temp=arr[index1];
arr[index1]=arr[index2];
arr[index2]=temp;
}
/* sort by descend of an array */
public static void desSortArr(int[] arr){
Arrays.sort(arr);
int i=0;
int j=arr.length-1;
while(i<j){
exchEleArr(arr,i,j);
i++;
j--;
}
}
/* print an arry */
public static void printArr(int[] arr){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
/*clone an array */
public static int[] copyArr(int[] arr){
int[] tempArr=new int[arr.length];
for(int i=0;i<arr.length;i++) tempArr[i]=arr[i];
return tempArr;
}
/* return the index of the maximum value of an array */
public static int maxArr(int[] arr){
int i=0,j=1;
for(;j<arr.length;j++) if(arr[j]>arr[i]) i=j;
return i;
}
/* return the index of the minimum value of an array */
public static int minArr(int[] arr){
int i=0,j=1;
for(;j<arr.length;j++) if(arr[j]<arr[i]) i=j;
return i;
}
/* set the value with the given index of an array */
public static void setArr(int[] arr,int index,int value){
if(index>=arr.length) throw new ArrayIndexOutOfBoundsException();
arr[index]=value;
}
/* return an array which contains indexes of top n sort by value in an array */
public static int[] topN(int[] arr,int n){
int[] indexArr=new int[n];
for(int i=0;i<n;i++){
indexArr[i]=maxArr(arr);
setArr(arr,maxArr(arr),arr[minArr(arr)]);
}
return indexArr;
}
/* the test */
public static void main(String[] args){
int[] a={6,1,6,2,6,3,7,4,8,9};
printArr(a);
System.out.println(maxArr(a)+" "+minArr(a));
int[] b=topN(a,5);
printArr(b);
printArr(a);
}
}
package com.yatium;
import java.util.*;
import com.yatium.TestArray;
public class Test74{
public static int arrToNum(int[] a,int rad){
int num=0;
for(int i=a.length-1;i>=0;i--)
num+=Math.pow(rad,i)*a[i];
return num;
}
public static void main(String[] args){
int[] first=new int[4];
int[] second=new int[4];
int[] third=new int[4];
for(int i=1;i<10;i++)
for(int j=1;j<10;j++)
for(int k=1;k<10;k++)
for(int l=1;l<10;l++)
{
first[0]=i;
first[1]=j;
first[2]=k;
first[3]=l;
second=TestArray.copyArr(first);
third=TestArray.copyArr(first);
TestArray.desSortArr(third);
Arrays.sort(second);
int a=arrToNum(second,10);
int b=arrToNum(third,10);
int c=arrToNum(first,10);
if((a-b)==c) System.out.println(c);
}
}
}
* an fault : topN(int[],int) method will
* change the value of the argument int[]
*
*/
package com.yatium;
import java.util.*;
public class TestArray{
/* exchange two element with given indexes of an array */
public static void exchEleArr(int[] arr,int index1,int index2){
int temp=arr[index1];
arr[index1]=arr[index2];
arr[index2]=temp;
}
/* sort by descend of an array */
public static void desSortArr(int[] arr){
Arrays.sort(arr);
int i=0;
int j=arr.length-1;
while(i<j){
exchEleArr(arr,i,j);
i++;
j--;
}
}
/* print an arry */
public static void printArr(int[] arr){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
/*clone an array */
public static int[] copyArr(int[] arr){
int[] tempArr=new int[arr.length];
for(int i=0;i<arr.length;i++) tempArr[i]=arr[i];
return tempArr;
}
/* return the index of the maximum value of an array */
public static int maxArr(int[] arr){
int i=0,j=1;
for(;j<arr.length;j++) if(arr[j]>arr[i]) i=j;
return i;
}
/* return the index of the minimum value of an array */
public static int minArr(int[] arr){
int i=0,j=1;
for(;j<arr.length;j++) if(arr[j]<arr[i]) i=j;
return i;
}
/* set the value with the given index of an array */
public static void setArr(int[] arr,int index,int value){
if(index>=arr.length) throw new ArrayIndexOutOfBoundsException();
arr[index]=value;
}
/* return an array which contains indexes of top n sort by value in an array */
public static int[] topN(int[] arr,int n){
int[] indexArr=new int[n];
for(int i=0;i<n;i++){
indexArr[i]=maxArr(arr);
setArr(arr,maxArr(arr),arr[minArr(arr)]);
}
return indexArr;
}
/* the test */
public static void main(String[] args){
int[] a={6,1,6,2,6,3,7,4,8,9};
printArr(a);
System.out.println(maxArr(a)+" "+minArr(a));
int[] b=topN(a,5);
printArr(b);
printArr(a);
}
}
package com.yatium;
import java.util.*;
import com.yatium.TestArray;
public class Test74{
public static int arrToNum(int[] a,int rad){
int num=0;
for(int i=a.length-1;i>=0;i--)
num+=Math.pow(rad,i)*a[i];
return num;
}
public static void main(String[] args){
int[] first=new int[4];
int[] second=new int[4];
int[] third=new int[4];
for(int i=1;i<10;i++)
for(int j=1;j<10;j++)
for(int k=1;k<10;k++)
for(int l=1;l<10;l++)
{
first[0]=i;
first[1]=j;
first[2]=k;
first[3]=l;
second=TestArray.copyArr(first);
third=TestArray.copyArr(first);
TestArray.desSortArr(third);
Arrays.sort(second);
int a=arrToNum(second,10);
int b=arrToNum(third,10);
int c=arrToNum(first,10);
if((a-b)==c) System.out.println(c);
}
}
}