Is there another algorithm to find all different
numeric values of RATS in the addition DOG+CAT=RATS
?
Program #1:
// CAT+DOG=RATS (32 different additions)
import java.util.*;
public class program1 {
public static void main (String []args) {
int cat,dog,rats,top;
int c,a,t,d,o,g,r,s,k,x;
TreeSet kume=new TreeSet();
List lst = new ArrayList();
for(k=0;k<10;k++){
lst.add(new Integer(k) ); }
for(x=1;x<150;x++) {
do {
do {
Collections.shuffle(lst);
c=((Integer) lst.get(0)).intValue();
r=((Integer) lst.get(6)).intValue();
d=((Integer) lst.get(3)).intValue();
} while ( c==0 || d==0 || r==0);
a=((Integer) lst.get(1)).intValue();
t=((Integer) lst.get(2)).intValue();
o=((Integer) lst.get(4)).intValue();
g=((Integer) lst.get(5)).intValue();
s=((Integer) lst.get(7)).intValue();
cat=100*c+10*a+t;
dog=100*d+10*o+g;
top=cat+dog;
rats=1000*r+100*a+10*t+s;
} while ( rats !=top );
kume.add(new Integer(rats));
} //end for
int tane=kume.size();
Object rat[]=kume.toArray();
System.out.println("Do the following additions : ");
int num;
for(int loop=0;loop<tane;loop++){
num=loop+1;
System.out.println(num+") CAT + DOG = "+rat[loop] );
}
} //end main
} //end class
----------------------------
Program #2:
import java.util.*;
public class program2 {
public static void write(String s) {
System.out.print(s);
} /* to print stirng on the screen*/
public static boolean different(int a,int b,int c,int
d,int e,int f,int g,int h) {
if (a==b || a==c || a==d || a==e || a==f || a==g ||
a==h)
return false;
if (b==c || b==d || b==e || b==f || b==g || b==h)
return false;
if (c==d || c==e || c==f || c==g || c==h)
return false;
if (d==e || d==f || d==g || d==h)
return false;
if (e==f || e==g || e==h)
return false;
if (f==g || f==h)
return false;
if (g==h)
return false;
return true;
} /* to control wheather all inputs are different or
not */
public static void main(String[] args) {
int CAT,DOG,RATS;
int CAT_number=0; /* to control how many different
(CAT,DOG) exists */
ArrayList al=new ArrayList(); /* to store possible
RAST */
for (int C=1;C<10;C++)
for(int A=0;A<10;A++)
for(int T=0;T<10;T++)
for(int D=1;D<10;D++)
for(int O=0;O<10;O++)
for(int G=0;G<10;G++)
for(int R=1;R<2;R++)
/* since the sumation of
two 3-digit
cannot be grater than 1998, R must
be only "1"*/
for(int
S=0;S<10;S++) {
CAT=100*C+10*A+T;
DOG=100*D+10*O+G;
RATS=1000*R+100*A+10*T+S;
if
(CAT+DOG==RATS)
if
(different(C,A,T,D,O,G,R,S)) {
al.add(new Integer(RATS));
CAT_number++;
}
}
/* by using these "8 for" loops, we can test all
possible numbers */
{
int s=al.size();
int ar[]=new int[s];
for (int i=0;i<s;i++)
ar[i]=((Integer)al.get(i)).intValue();
Arrays.sort(ar); /* to get all solutions in an
order */
int solution_number=1;
write("\n\n different RATS (=CAT+DOG)
SOLUTION LIST\n");
write("
------------------------------------------------\n
\n");
write(" 1 --> "+ar[0]+" "); /* to print the
first solution */
for (int i=1;i<s;i++) {
if (ar[i]!=ar[i-1]) { /* to eleminate the same
solutions */
solution_number++;
if (solution_number<10) {
write(" ");
} /* to print solutions like a table */
write(solution_number+" --> "+ar[i]+" "); /*
to print all solutions */
if (solution_number%4==0) {
write("\n");
}
}
}
write("\n\nall different RAST solutions :
"+solution_number+"\n");
write("all different (CAT,DOG) solutions :
"+CAT_number+"\n");
}
}
}
__________________________________________________
Do you Yahoo!?
HotJobs - Search new jobs daily now
http://hotjobs.yahoo.com/
____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm
Be respectful! Clean up your posts before replying
____________________________________________________