I want the program to only print the 9 digit numbers that obey all the rules set by the IF statement. However, I feel like it is unnecessarily long and I would love it if someone could show me some syntax that would shorten it. Thanks in advance :)
EDIT: I need to figure out every single different combination of a 3x3 grid using only numbers 1,2 & 3. However, 1, 2 & 3 can only go in a single row ONCE and also go in a single column ONCE. N.b. I think there are 12 different combinations.
public static void main (String [] args){
int count =0;
for (int i=111111111; i<1000000000; i++){
int X=i;
String x= String.valueOf(X);
String a1= x.substring(0,1);
String a2= x.substring(1,2);
String a3= x.substring(2,3);
String b1= x.substring(3,4);
String b2= x.substring(4,5);
String b3= x.substring(5,6);
String c1= x.substring(6, 7);
String c2= x.substring(7,8);
String c3= x.substring(8,9);
int A1= Integer.parseInt(a1);
int A2= Integer.parseInt(a2);
int A3= Integer.parseInt(a3);
int B1= Integer.parseInt(b1);
int B2= Integer.parseInt(b2);
int B3= Integer.parseInt(b3);
int C1= Integer.parseInt(c1);
int C2= Integer.parseInt(c2);
int C3= Integer.parseInt(c3);
if (A1+B1+C1+A2+B2+C2+A3+B3+C3==18
&& A1<=3 && A2<=3 && A3<=3 && B1<=3 && B2<=3 && B3<=3 && C1<=3 && C2<=3 && C3<=3
&& A1+B1+C1==6 && A1+A2+A3==6
&& A1!=B1 && B1!=C1 && A1!=C1 && A1!=A2 && A2!=A3 && A1!=A3 && C1!=C2 && C2!=C3
&& C1!=C3 && B1!= B2 && B1!=B3 && B2!=B3 && A3!=B3 && A2!=B2 && A3!=C3 && A2!=C2
&& B2!=C2)
{
count++;
System.out.println(count);
System.out.println(i);
Aucun commentaire:
Enregistrer un commentaire