public class CMLSC {
public void CMLSC(int A[]){
int n; int i;
n=A.length;
int imax=A.length-1;
int L[]=new int[A.length];
int S[]=new int[A.length];
for(i=n-1; i>=0; i--){
S[i]=-1;
L[i]=1;
for(int j=i+1;j<n;j++){
if(A[j]>A[i]&&L[j]+1>L[i]){
L[i]=L[j]+1;
S[i]=j;
} }
if(L[i]>L[imax])
imax=i;
}
i=imax;
while(i!=-1){
System.out.println(A[i]);
i=S[i];
}
}
public static void main(String[] args) {
int A[]=new int[12];
A[0]=3; A[1]=5; A[2]=76; A[3]=1;
A[4]=45; A[5]=2; A[6]=67; A[7]=52;
A[8]=90; A[9]=0; A[10]=4; A[11]=15;
CMLSC cmlsc=new CMLSC();
cmlsc.CMLSC(A);
}
}
---------------------------------------------------------------------
public class Hanoi {
public static void hanoi(int n, String from, String temp, String to) {
if (n == 0) return;
hanoi(n-1, from, to, temp);
System.out.println("Move disc " + n + " from " + from + " to " + to);
hanoi(n-1, temp, from, to);
}
public static void main(String[] args) {
hanoi(3, "A", "B", "C");
}
}
---------------------------------------------------------------------------
public class regine {
int S[]=new int[4];
public void queen(int k){
int n=4;
for(int i=0;i<n;i++){
if (accept(k,i)){
S[k]=i;
if(k==n-1){
for(k=0;k<n;k++){
System.out.print(S[k]+ " ");
}
}
else queen(k+1);
}
}
}
public boolean accept(int k , int i){
int j;
for(j=0;j<=k-1;j++){
if(S[j]==i) return false;
if (Math.abs(j-k)==Math.abs(S[j]-i))
return false;
}
return true;
}
public static void main(String[] args) {
regine regine1 = new regine();
regine1.queen(0);
}
}
No comments:
Post a Comment