C语言趣味程序百例精解之java实现(61)1-9组成三个三位的平方数
public class Test61{
public static void main(String args[]){
new Test61().PingFangShu61();
}
/**
* 61.1-9组成三个三位的平方数
*/
public void PingFangShu61() {
int n = 0;
int count = 0;
int[][] s = new int[20][4];
for (int a = 1; a <= 9; a++)
for (int b = 1; b <= 9; b++)
for (int c = 1; c <= 9; c++) {
if (a != b && b != c && a != c) {
n = 100 * a + 10 * b + c;
if (Math.sqrt(n) % 1 == 0) {
System.out.println(count + " " + n + "=="
+ Math.sqrt(n) + "^2");
s[count][0] = n;
s[count][1] = a;
s[count][2] = b;
s[count][3] = c;
count = count + 1;
}
}
}
System.out.println("count=" + count);
for (int a = 0; a < count; a++)
for (int b = a; b < count; b++)
for (int c = b; c < count; c++) {
if (s[a][0] != s[0] && s[a][0] != s[c][0]
&& s[0] != s[c][0]) {
if (notEquls(new int[] { s[a][1], s[a][2], s[a][3],
s[1], s[2], s[3], s[c][1], s[c][2],
s[c][3], }))
System.out.println("a=" + s[a][0] + " b=" + s[0]
+ " c=" + s[c][0]);
}
}
}
/**
* 判断是否两两不相等
*/
public boolean notEquls(int[] a) {
if (a == null || a.length == 0 || a.length == 1)
return true;
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
if (a == a[j] && i != j) {
// System.out.println("a[" + i + "]" + a + " a[" + j +
// "]"
// + a[j] + "---");
return false;
}
}
}
return true;
}
}
C:\bat>java Test61
0 169==13.0^2
1 196==14.0^2
2 256==16.0^2
3 289==17.0^2
4 324==18.0^2
5 361==19.0^2
6 529==23.0^2
7 576==24.0^2
8 625==25.0^2
9 729==27.0^2
10 784==28.0^2
11 841==29.0^2
12 961==31.0^2
count=13
a=361 b=529 c=784
C:\bat> |