Created
January 5, 2013 14:17
-
-
Save anselmobattisti/4461750 to your computer and use it in GitHub Desktop.
solucao do problema de soma 15 de forma aleatoria
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cubo { | |
int[] num = {1,2,3,4,8,6,7,5,9}; | |
int t = 0; | |
public static void main (String args[]){ | |
new Cubo(); | |
} | |
Cubo(){ | |
while(!calcula()){ | |
int aux1 = ( int ) Math.floor( ( Math.random() * 9 ) ); | |
int aux2 = ( int ) Math.floor( ( Math.random() * 9 ) ); | |
int a = num[aux1]; | |
num[aux1] = num[aux2]; | |
num[aux2] = a; | |
} | |
} | |
private boolean calcula(){ | |
t++; | |
if( | |
((num[0]+num[1]+num[2]) == 15) && | |
((num[3]+num[4]+num[5]) == 15) && | |
((num[6]+num[7]+num[8]) == 15) && | |
((num[0]+num[4]+num[8]) == 15) && | |
((num[2]+num[4]+num[6]) == 15) && | |
((num[0]+num[3]+num[6]) == 15) && | |
((num[1]+num[4]+num[7]) == 15) && | |
((num[2]+num[5]+num[8]) == 15)){ | |
imprimir(); | |
return true; | |
} | |
//imprimir(); | |
return false; | |
} | |
private void imprimir(){ | |
for(int i=0; i < 9; i++){ | |
System.out.print("\t"+num[i]); | |
if(i == 2 || i == 5 || i == 8){ | |
System.out.println(""); | |
} | |
} | |
System.out.println("--- Tentativa "+t+"\n\n\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment