打开很慢~请谅解~ package zzq.main; import java.io.BufferedReader; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; /************************************************************************ * C语言趣味程序(不到)百例-之Java实现 * * 0 骨头找了其中比较简单的用Java实现了 * * 1 有的完全可单独为篇,但骨头懒,所有题一并发了。 * * 2 解法并非最优,多数用的穷举法。 * * 3 原题可通过方法注释的题号查询PDF文档。再次为懒道歉 * * 4 只做到83题,原因:1这几天忙2后面的需要思考3晚上需要搞艺术没时间 * * @author lazybone 2010-05-14 * ************************************************************************/ public class JavaFun { public static void main(String[] args) { long start = System.currentTimeMillis(); new JavaFun().KaBuLieKe83(3976, 0); long over = System.currentTimeMillis(); System.out.println("Cost:" + (over - start) + " ms."); } /** * 83。卡布列克常数,挺好玩,实现一下 */ public void KaBuLieKe83(int n, int count) { int cha = getTheMaxOrMin(n, 1) - getTheMaxOrMin(n, 0); System.out.println(count + ":" + getTheMaxOrMin(n, 1) + "-" + getTheMaxOrMin(n, 0) + " = " + cha); count++; if (n == 6174) { System.out.println("I did it 6174"); } else { KaBuLieKe83(cha, count); } } /** * 80.奇数平方和(和后面几个)根本无法得到验证,只能在有限范围内证明正确罢了 * * 没有思考价值纯粹列等式。故跳过。 */ /** * 79。随机数求π圆周率 */ public void getPaiByRandom79() { double all = 10000000; double l = 1000000; double pai = 0; double in = 0; double x = 0, y = 0; for (int i = 0; i < all; i++) { x = (Math.random() * l); y = (Math.random() * l); if (x * x + y * y < l * l) in++; } pai = (in / all * 4); System.out.println("Times/All=" + in + "/" + all + " pai~=" + pai); } /** * 76.小明买书 * * 输出7.9+7.2时会出现小尾巴,原因不明,现在断网暂不深究 */ public void buyBooks76() { double a = 3.1; double b = 1.7; double c = 2; double d = 5.3; double e = 0.9; double f = 7.2; double cha = 100; double s = 0; for (int a1 = 0; a1 <= 1; a1++) for (int b1 = 0; b1 <= 1; b1++) for (int c1 = 0; c1 <= 1; c1++) for (int d1 = 0; d1 <= 1; d1++) for (int e1 = 0; e1 <= 1; e1++) for (int f1 = 0; f1 <= 1; f1++) { s = a1 * a + b1 * b + c1 * c + d1 * d + e1 * e + f1 * f; if (s != 0 && Math.abs(s - 10) < cha) { cha = Math.abs(s - 10); System.out.print("差值:" + cha); System.out.print(" a=" + a1); System.out.print(" b=" + b1); System.out.print(" c=" + c1); System.out.print(" d=" + d1); System.out.print(" e=" + e1); System.out.print(" f=" + f1); System.out.println(); } } } /** * 75.10个小孩分糖果 */ public void shareSweet75() { int[] boy = { 10, 2, 8, 22, 16, 4, 10, 6, 14, 20 }; int[] copy = new int[boy.length]; int times = 0; System.out.print("Times:" + times + " "); show(boy); while (allTheSame(boy) == false) {// 不相等 System.arraycopy(boy, 0, copy, 0, 10); for (int i = 0; i <= 9; i++) { boy[i] = copy[(i - 1 + 10) % 10] / 2 + copy[i] / 2; } times++; System.out.print("Times:" + times + " "); show(boy); for (int i = 0; i <= 9; i++) { if (boy[i] % 2 == 1) boy[i]++; } if (times > 100) break; }// while System.out.print("Times:" + ++times + " "); show(boy); } /** * 74,可以称1到40磅的4块砝码 */ public void fama40() { for (int a = 1; a <= 37; a++) for (int b = a; b <= 37; b++) for (int c = b; c <= 37; c++) for (int d = c; d < 37; d++) { if (a + b + c + d != 40) continue; boolean one = false; boolean all = true; for (int s = 1; s <= 40; s++) { one = false; for (int a1 = -1; a1 <= 1; a1++) for (int b1 = -1; b1 <= 1; b1++) for (int c1 = -1; c1 <= 1; c1++) for (int d1 = -1; d1 <= 1; d1++) { if (one == true)// 有一个成立就可以 continue; if (s == a1 * a + b1 * b + c1 * c + d1 * d) {// 可以组合成为s one = true; } } if (one == false) { all = false; } } if (all == true) { System.out.print(" a=" + a); System.out.print(" b=" + b); System.out.print(" c=" + c); System.out.print(" d=" + d); System.out.println(); } } } /** * 73.和数能表示1-23的5个正整数 */ public void findTheNumber73() { for (int a = 1; a < 23; a++) for (int b = a + 1; b < 23; b++) for (int c = b + 1; c < 23; c++) for (int d = c + 1; d < 23; d++) for (int e = d + 1; e < 23; e++) { if (a + b + c + d + e != 23) // 互不相同 continue; boolean oneIsOK = false; boolean allIsOK = true; int max = 1; for (int s = 1; s <= 23; s++) { oneIsOK = false; for (int a1 = 0; a1 <= max; a1++) for (int b1 = 0; b1 <= max; b1++) for (int c1 = 0; c1 <= max; c1++) for (int d1 = 0; d1 <= max; d1++) for (int e1 = 0; e1 <= max; e1++) { // 只要其中有一组成立即可 if (a1 * a + b1 * b + c1 * c + d1 * d + e1 * e == s) {// 能组成 oneIsOK = true; // System.out.println(a1 // + "X" + a + "+" // + b1 + "X" + b // + "+" + c1 // + "X" + c + "+" // + d1 + "X" + d // + "+" + e1 // + "X" + e + "=" // + s); } } // 如果有一组不成立则本组否定 if (oneIsOK == false) { allIsOK = false; } }// for(s) if (allIsOK == true) { System.out.print(" a=" + a); System.out.print(" b=" + b); System.out.print(" c=" + c); System.out.print(" d=" + d); System.out.print(" e=" + e); System.out.println(); } } } /** * 72.邮票问题 */ public void stamp72() { int[] s = new int[20]; int idx = 0; int value = 0; boolean exist = false; for (int i = 0; i < 5; i++) for (int j = 0; j < 4; j++) { value = 3 * i + 5 * j; for (int k = 0; k < idx + 1; k++) { if (s[k] == value && (i != 0 || j != 0)) { exist = true; break; } } if (exist == false) { s[idx] = value; idx++; } } for (int i = 0; i < s.length; i++) { System.out.print(s[i] + " "); } System.out.println(); } /** * 71.约瑟夫问题 */ public void YueSeFu71() { int max = 30; int[] people = new int[max]; int n = 9; int idx = 0; for (int i = 0; i < max / 2 + 1; i++) { for (int j = 0; j < n;) { if (people[idx % max] == 0) { j++; } idx++;// 若放在for所在行,则不会每次都执行 } idx--; people[(idx) % max] = i + 1; } for (int i = 0; i < max; i++) System.out.print(people[i] + " "); System.out.println(); } /** * 69.魔术师猜牌 */ public void magic69() { int Max = 13; int[] box = new int[Max]; int cur = 0; for (int i = 0; i < Max; i++) { for (int j = 0; j < i + 1;) { if (box[cur % Max] == 0) { j++; } cur++; } box[(cur - 1) % Max] = i + 1; } for (int i = 0; i < box.length; i++) System.out.print(box[i] + " "); System.out.println(); } /** * 62.cube立方体 */ public void cube62() { int n = 8; for (int a = 1; a <= n; a++) for (int b = 1; b <= n; b++) for (int c = 1; c <= n; c++) for (int d = 1; d <= n; d++) for (int e = 1; e <= n; e++) for (int f = 1; f <= n; f++) for (int g = 1; g <= n; g++) for (int h = 1; h <= n; h++) { if (notEquls(new int[] { a, b, c, d, e, f, g, h }) == true) { cube62(a, b, c, d, e, f, g, h); } } } public void cube62(int a, int b, int c, int d, int e, int f, int g, int h) { int s = a + b + c + d; if (s == e + f + g + h) if (s == a + b + e + f) if (s == c + d + g + h) if (s == a + c + e + g) if (s == b + d + f + h) { System.out.print(" a=" + a); System.out.print(" b=" + b); System.out.print(" c=" + c); System.out.print(" d=" + d); System.out.print(" e=" + e); System.out.print(" f=" + f); System.out.print(" g=" + g); System.out.println(" h=" + g); } } /** * 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[b][0] && s[a][0] != s[c][0] && s[b][0] != s[c][0]) { if (notEquls(new int[] { s[a][1], s[a][2], s[a][3], s[b][1], s[b][2], s[b][3], s[c][1], s[c][2], s[c][3], })) System.out.println("a=" + s[a][0] + " b=" + s[b][0] + " c=" + s[c][0]); } } } /** * 60.分成三个数 */ public void devideIntoThree60() { int count = 0; int n1, n2, n3; for (int a = 1; a <= 9; a++) for (int b = 1; b <= 9; b++) for (int c = 1; c <= 9; c++) for (int d = 1; d <= 9; d++) for (int e = 1; e <= 9; e++) for (int f = 1; f <= 9; f++) for (int g = 1; g <= 9; g++) for (int h = 1; h <= 9; h++) for (int i = 1; i <= 9; i++) if (a + b + c + d + e + f + g + h + i == 45 && notEquls(new int[] { a, b, c, d, e, f, g, h, i })) { n1 = a * 100 + b * 10 + c; n2 = d * 100 + e * 10 + f; n3 = g * 100 + h * 10 + i; if (2 * n1 == n2 && 3 * n1 == n3) { count++; System.out.print(" a=" + a); System.out.print(" b=" + b); System.out.print(" c=" + c); System.out.print(" d=" + d); System.out.print(" e=" + e); System.out.print(" f=" + f); System.out.print(" g=" + g); System.out.print(" h=" + h); System.out.println(" i=" + i); System.out.println("n1=" + n1 + " n2=" + n2 + " n3=" + n3); } } System.out.println("count=" + count); } /** * 59.填表格 * * a b c * * d e f */ public void TianTable59() { for (int a = 1; a <= 6; a++) for (int b = 1; b <= 6; b++) for (int c = 1; c <= 6; c++) for (int d = 1; d <= 6; d++) for (int e = 1; e <= 6; e++) for (int f = 1; f <= 6; f++) { if (b > a && c > b && e > d && f > e && d > a && e > b && f > c && notEquls(new int[] { a, b, c, d, e, f })) { System.out.print(" a=" + a); System.out.print(" b=" + b); System.out.print(" c=" + c); System.out.print(" d=" + d); System.out.print(" e=" + e); System.out.println(" f=" + f); } } } /** * 58.拉丁方 */ public void LaDingFang58() { int n = 6; for (int j = 0; j < n; j++) { for (int i = j; i < j + n; i++) { System.out.print(i % n + 1 + " "); } System.out.println(""); } } /** * 57.谁家孩子跑得慢 */ public void whosChildRunSlow57() { int count = 0; for (int a = 1; a <= 9; a++) for (int b = 1; b <= 9; b++) for (int c = 1; c <= 9; c++) for (int d = 1; d <= 9; d++) for (int e = 1; e <= 9; e++) for (int f = 1; f <= 9; f++) for (int g = 1; g <= 9; g++) for (int h = 1; h <= 9; h++) for (int i = 1; i <= 9; i++) if (a + b + c + d + e + f + g + h + i == 45 && (g == 9 || h == 9 || i == 9) && (d == 8 || e == 8 || f == 8) && (d != 3 && e != 3 && f != 3) && (a + b + c == 15) && (d + e + f == 15) && (g + h + i == 15) && (Math.abs(a - b) != 1 && Math.abs(a - c) != 1 && Math .abs(c - b) != 1) && (Math.abs(d - e) != 1 && Math.abs(d - f) != 1 && Math .abs(e - f) != 1) && (Math.abs(g - h) != 1 && Math.abs(g - i) != 1 && Math .abs(h - i) != 1) && notEquls(new int[] { a, b, c, d, e, f, g, h, i }) ) { count++; System.out.print(" a=" + a); System.out.print(" b=" + b); System.out.print(" c=" + c); System.out.print(" d=" + d); System.out.print(" e=" + e); System.out.print(" f=" + f); System.out.print(" g=" + g); System.out.print(" h=" + h); System.out.println(" i=" + i); } System.out.println("Count =" + count); } /** * 56。区分旅客国籍 * * 1美国,2德国,3英国,4法国,5俄罗斯,6意大利 */ public void whereAreYouFrom56() { int count = 0; for (int a = 1; a <= 6; a++) for (int b = 1; b <= 6; b++) for (int c = 1; c <= 6; c++) for (int d = 1; d <= 6; d++) for (int e = 1; e <= 6; e++) for (int f = 1; f <= 6; f++) if (a != 1 && a != 5 && a != 2 && e != 1 && e != 2 && e != 5 && c != 2 && c != 1 && c != 5 && b != 2 && f != 2 && a != 4 && c != 6 && b != 1 && c != 4 && notEquls(new int[] { a, b, c, d, e, f })) { count++; System.out.print(" a=" + a); System.out.print(" b=" + b); System.out.print(" c=" + c); System.out.print(" d=" + d); System.out.print(" e=" + e); System.out.println(" f=" + f); } System.out.println("Count =" + count); } /** * 55。谁值班 */ public void whoOnDuty55() { for (int a = 1; a <= 7; a++) for (int b = 1; b <= 7; b++) for (int c = 1; c <= 7; c++) for (int d = 1; d <= 7; d++) for (int e = 1; e <= 7; e++) for (int f = 1; f <= 7; f++) for (int g = 1; g <= 7; g++) { if ((a - c == 1) && (d - e == 2) && (g - b == 3) && (f == 4) && ((b > 4 && c < 4) || (b < 4 && c > 5)) && (a + b + c + d + e + f + g == (1 + 7) / 2 * 7) && notEquls(new int[] { a, b, c, d, e, f, g }) == true) { System.out.println("a=" + a); System.out.println("b=" + b); System.out.println("c=" + c); System.out.println("d=" + d); System.out.println("e=" + e); System.out.println("f=" + f); System.out.println("g=" + g + "/n/n"); } } } /** * 54.又是无聊的谁在说谎 */ public void whoTellLie54() { for (int a = 1; a <= 3; a++) for (int b = 1; b <= 3; b++) for (int c = 1; c <= 3; c++) if ((a != b && b != c && a != c) && (a == 1 && b == 1 || a == 2 && b != 1 || a == 3) && (b == 1 && b == 3 || b == 2 && b != 3 || b == 3) && (c == 1 && b == 2 || c == 2 && b != 2 || c == 3)) { System.out.println("a=" + a); System.out.println("b=" + b); System.out.println("c=" + c); } } /** * 53. 谁在说谎 */ public void whoTellLie53() { for (int a = 0; a <= 1; a++) for (int b = 0; b <= 1; b++) for (int c = 0; c <= 1; c++) if (((a == 1 && a + b + c == 2) || (a == 0 && a + b + c != 2)) && ((b == 1 && a + b + c == 1) || (b == 0 && a + b + c != 1)) && ((c == 1 && a + b + c == 1) || (c == 0 && a + b + c != 1))) { System.out.println("a=" + a); System.out.println("b=" + b); System.out.println("c=" + c); } } /** * 52.黑0与白1 */ public void BlackAndWrite52() { for (int a = 0; a <= 1; a++) for (int b = 0; b <= 1; b++) for (int c = 0; c <= 1; c++) for (int d = 0; d <= 1; d++) for (int e = 0; e <= 1; e++) { if ((a == 1 && b + c + d + e == 3 || a != 1 && b + c + d + e != 3) && (b == 1 && a + c + d + e == 0 || b != 1 && a + c + d + e != 0) && (c == 1 && a + b + d + e == 1 || c != 1 && a + b + d + e != 1) && (d == 1 && a + b + c + e == 4 || d != 1 && a + b + c + e != 4)) { System.out.println("a=" + a); System.out.println("b=" + b); System.out.println("c=" + c); System.out.println("d=" + d); System.out.println("e=" + e); } } } /** * 51.谁在说谎whoTellLie51 小偷==1 */ public void whoTellLie51() { for (int a = 0; a <= 1; a++) for (int b = 0; b <= 1; b++) for (int c = 0; c <= 1; c++) for (int d = 0; d <= 1; d++) { if (a + b + c + d == 1 && (b + d == 1) && (b + c == 1) && (a + b == 1)) { System.out.println("a=" + a); System.out.println("b=" + b); System.out.println("c=" + c); System.out.println("d=" + d); } } } /** * 50.谁在说谎 */ public void whoTellLie50() { for (int a = 0; a <= 1; a++) for (int b = 0; b <= 1; b++) for (int c = 0; c <= 1; c++) { if (a == 1 && b == 0 && c == 1 && a == 0 && b == 0) { System.out.println("a=" + a); System.out.println("b=" + b); System.out.println("c=" + c); } if (a == 0 && b == 1 && c == 0 && (a == 1 || b == 1)) { System.out.println("a=" + a); System.out.println("b=" + b); System.out.println("c=" + c); } } } /** * 49.委派任务 */ public void mission49() { for (int a = 0; a <= 1; a++) for (int b = 0; b <= 1; b++) for (int c = 0; c <= 1; c++) for (int d = 0; d <= 1; d++) for (int e = 0; e <= 1; e++) for (int f = 0; f <= 1; f++) { if (a + b >= 1 && a + d <= 1 && a + e + f == 2 && (b + c == 0 || b + c == 2) && (c + d == 1) && ((d == 0 && e == 0) || (d == 1 && e == 1))) { System.out.println("a=" + a); System.out.println("b=" + b); System.out.println("c=" + c); System.out.println("d=" + d); System.out.println("e=" + e); System.out.println("f=" + f); } } } /** * 45.埃及分数 */ public void aiJiFenShu45() { } /** * 44.求等式成立 */ public void letItIsRight() { for (int a = 1; a < 10; a++) for (int b = a; b < 10; b++) for (int c = b; c < 10; c++) for (int d = c; d < 10; d++) if (a * b * c + a * b * d + a * c * d + b * c * d == a * b * c * d) System.out.println("a=" + a + " b=" + b + " c=" + c + " d=" + d); } /** * 42.最大公约数&最小公倍数 */ public void GYSGBS42() { int a = 2800; int c = a; int b = 804; int d = b; int temp; if (a > b) { temp = a; a = b; b = temp; } while (b != 0) { temp = a % b; System.out.println("a=" + a + " b=" + b + " temp=" + temp); a = b; b = temp; } System.out.println("公约数:" + a); System.out.println("公倍数:" + c * d / a); System.out.println(12 % 4); } /** * 41.马克思手稿中的数学题 */ public void MaKeSi41() { for (int a = 0; a < 30; a++) for (int b = 0; b < 30; b++) for (int c = 0; c < 30; c++) { if (a * 3 + b * 2 + c == 50 && a + b + c == 30) { System.out.println("a=" + a + " b=" + b + " c=" + c); } } } /** * 40 三色球 */ public void SanSeQiu40() { for (int i = 0; i <= 3; i++) for (int j = 0; j <= 3; j++) { if (8 - i - j <= 6) System.out.println("Res=" + i + " White=" + j + " Black=" + (8 - i - j)); } } /** * 39.年龄几何 */ public void age39() { int a = 0, d = 0; for (d = 1; d <= 6; d++) for (a = 1; a <= 7; a++) { if (a + a + d + a + 2 * d + a + 3 * d == 26 && a * (a + d) * (a + 2 * d) * (a + 3 * d) == 880) { System.out.println("a=" + a + " d=" + d); for (int i = 0; i < 20; i++) { System.out.println("s" + i + "=" + (a + d * i) + " a=" + a + " d=" + d); } return; } } } /** * 38.换硬币问题(PDF的结果貌似有问题少了很多解) */ public void changeCoin38() { int count = 0; for (int i = 0; i <= 100; i++) for (int j = 0; j <= 50; j++) for (int k = 0; k <= 20; k++) { if (i + j * 2 + k * 5 == 100) { System.out.println(count++ + ":" + i + "-" + j + "-" + k); } } } /** * 37 上楼问题 */ public void ShangLou37() { for (int i = 7; i < 10000; i++) { if (i % 2 == 1 && i % 3 == 2 && i % 5 == 4 && i % 6 == 5 && i % 7 == 0) { System.out.println(i); } } } /** * 36.百钱百鸡问题 */ public void BaiQianBaiJi() { for (int a = 0; a < 21; a++) for (int b = 0; b < 34; b++) for (int c = 0; c < 101; c++) { if (5 * a + 3 * b + c / 3 == 100 && c % 3 == 0 && a + b + c == 100) { System.out.println("a=" + a + " b=" + b + " c=" + c); } } } /** * 32.可逆素数,反序数 */ public void KeNiSuShu() { for (int i = 1; i < 10000; i++) { if (isSuShu(i) && isSuShu(FanXuShu(i))) System.out.println(i); } } /** * 31.哥德巴赫猜想 */ public void GeDeBaHe31() { for (int i = 1; i < 2000 / 2 + 1; i++) { if (isSuShu(i) && isSuShu(2000 - i)) { System.out.println("2000=" + i + "+" + (2000 - i)); } } } /** * 30.素数 */ public void SuShu30() { boolean isSuShu = true; int a = 1; for (int n = 2; n < 10000; n++) { isSuShu = true; for (int i = 2; i < n / 2; i++) { if (n % i == 0) { isSuShu = false; continue; } } if (isSuShu == true) System.out.println(a++ + " :" + n); } } /** * 29. abcd=(ab+cd)^2 */ public void NoName29() { for (int a = 0; a < 10; a++) for (int b = 0; b < 10; b++) for (int c = 0; c < 10; c++) for (int d = 0; d < 10; d++) { if ((a * 1000 + b * 100 + c * 10 + d) == (a * 10 + b + c * 10 + d) * (a * 10 + b + c * 10 + d)) { System.out.println(a + "" + b + "" + c + "" + d + "=(" + a + "" + b + "" + c + "" + d + ")^2"); } } for (int n = 1000; n < 10000; n++) { int a = n / 100; int b = n % 100; if ((a + b) * (a + b) == n) { System.out.println("n=" + n); } } } /** * 28.回文数12321 */ public void HuiWenShu28() { int n = 1, m = 1; int count = 0; boolean isSymmetricNumber = true; for (; m < 256; m++) { n = m * m; isSymmetricNumber = true; count = getBitCount(n); for (int i = 0; i < count / 2; i++) { if (getThe(n, i + 1) != getThe(n, count - i)) { isSymmetricNumber = false; break; } } if (isSymmetricNumber == true) { System.out.println("I got one:" + n); } } } /** * 27.自守数() */ public void ZiShouShu27() { for (int i = 1; i < 10000; i++) { int n = getBitCount(i); int s = (int) Math.pow(10, n); if ((i * i - i) % s == 0) System.out.println("i*i=" + i * i + " i=" + i); } } /** * 26.亲密数 */ public void QinMiShu26() { for (int a = 0; a < 3000; a++) { int s = 0; for (int j = 1; j < (a / 2) + 1; j++) { if (a % j == 0) { s += j; } } int s2 = 0; for (int j = 1; j < (s / 2) + 1; j++) { if (s % j == 0) { s2 += j; } } if (a == s2 && s != a) System.out.println("I got one:" + a + " and " + s); } } /** * 25 完全数 */ public void WanQuanShu25() { for (int i = 1; i < 1000; i++) { int s = 0; for (int j = 1; j < (i / 2) + 1; j++) { if (i % j == 0) { s += j; } } if (s == i) { System.out.println("I got one:" + s); } } } /** * 24.阿姆斯特朗数 */ public void AMSTL24() { for (int i = 1; i < 9999; i++) { int s = 0; for (int j = 1; j < getBitCount(i) + 1; j++) { s += (getThe(i, j) * getThe(i, j) * getThe(i, j)); } System.out.println("s=" + s); if (s == i) System.out.println("I got one :" + i); } int n = getBitCount(999); int ss = 0; for (int i = 1; i < n + 1; i++) { ss = getThe(999, i); System.out.println("ss=" + ss); } } /** * 23.两个平方三位数获得是三个平方两位数 */ public void noName23() { int s = 0; double ss = 0.0; for (int a = 1; a < 10; a++) for (int b = 1; b < 10; b++) for (int c = 1; c < 10; c++) for (int x = 1; x < 10; x++) for (int y = 0; y < 10; y++) for (int z = 0; z < 10; z++) { // ------------- s = a * 100 + b * 10 + c; ss = Math.sqrt(s); if (ss % 1 != 0) continue; // ------------- s = x * 100 + y * 10 + z; ss = Math.sqrt(s); if (ss % 1 != 0) continue; // ------------- s = a * 10 + x; ss = Math.sqrt(s); if (ss % 1 != 0) continue; // ------------- s = b * 10 + y; ss = Math.sqrt(s); if (ss % 1 != 0) continue; // ------------- s = c * 10 + z; ss = Math.sqrt(s); if (ss % 1 != 0) continue; System.out.println("abc=" + (a * 100 + b * 10 + c) + " xyz=" + (x * 100 + y * 10 + z) + " ax=" + (a * 10 + x) + " by=" + (b * 10 + y) + " cz=" + (c * 10 + z)); } } /** * 22.对称数 */ public void symmetricNumber() { int n = 1001; int count = 0; boolean isSymmetricNumber = true; for (; n < 1003; n++) { count = getBitCount(n); for (int i = 0; i < count / 2; i++) { System.out.println(i + ":" + getThe(n, i + 1) + "-" + getThe(n, count - i)); if (getThe(n, i + 1) != getThe(n, count - i)) { isSymmetricNumber = false; break; } } if (isSymmetricNumber == true) { System.out.println("I got one:" + n); } } } /** * 20.Strange Number */ public void StrangeNumber() { for (int a = 0; a < 7; a++) for (int b = 0; b < 7; b++) for (int c = 0; c < 7; c++) if (a * 7 * 7 + b * 7 + c == a + b * 9 + c * 9 * 9) { System.out.println("a=" + a + " b=" + b + " c=" + c); } } /** * 18.个位为6,能被3整除的五位数 */ public void DevideBySix() { System.out.println((9999 - 1002) / 3); } /** * 7.阶乘尾数0的个数 .分析:0的个数,2*5得一个零,转换成5的个数 */ public void numberOfZero() { long start, over; int count = 0; start = System.currentTimeMillis(); for (int i = 1; i < 10000000; i++) { if (i % 25 == 0) { count += 2; continue; } if (i % 5 == 0) count++; } over = System.currentTimeMillis(); System.out.println("Method 1:100! contains " + count + " zero cost:" + (over - start)); start = System.currentTimeMillis(); // Method TWO count = 0; for (int i = 5; i < 10000000; i += 5) { if (i % 25 == 0) { count += 2; } else count++; } over = System.currentTimeMillis(); System.out.println("Method 2:100! contains " + count + " zero cost:" + (over - start)); } /** * 6.获取13的13次方的最后三位 */ public void getTheLastThreeNumber() { int n = 13; int result = 13; for (int i = 0; i < 12; i++) {// 循环12次 result = (result * n) % 1000; System.out.println("13的" + (i + 2) + "次方的最后三位是" + result); } } /** * 5.最大公约数 */ public void getMaxYUESHU() { for (int i = 999; i > 99; i--) { if (555555 % i == 0) { System.out.println("max yeushu=" + i); return; } } System.out.println("no one"); } /** * 3.用*画圆 */ public void drawCircle() { double x, y; double r = 5, modify = 5; for (y = 2 * r; y >= 0; y--) { // 调整 x y x = Math.sqrt(r * r - (y - modify) * (y - modify)); for (double i = 0; i < r - x; i++) {// 前面空白 System.out.print(" "); } System.out.print(" *");// 圆上点 for (double i = 0; i < 2 * (r + x - modify); i++) {// 中间空白 System.out.print(" "); } System.out.print(" *");// 圆上点 System.out.println(); } } /** * 1. 0~180余弦函数图 */ public void CosImage1() { double y; double x, m; for (y = 1; y > -1; y -= 0.1) { m = Math.acos(y) * 10; for (x = 1; x < m; x++) System.out.print(" "); System.out.print("*"); for (; x < 62 - m; x++) System.out.print(" "); System.out.println("*"); } } /**************************************************************** * 以下为通用方法 ***************************************************************/ /** * 显示数组的值 */ public void show(int[] a) { if (a == null) System.out.println("Array = null"); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } /** * 最小公倍数 */ public int getZXGBS(int a, int b) { int c = a; int d = b; int temp; if (a > b) { temp = a; a = b; b = temp; } while (b != 0) { temp = a % b; a = b; b = temp; } return c * d / a; } /** * 输入测试 */ public void inputTest() { String s = ""; try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); System.out.println("Input String ,'exit' for exit"); while (!(s = br.readLine()).equals("exit")) { System.out.println("s=" + s); System.out.println("Input String ,'exit' for exit"); } System.out.println("Bye"); } catch (Exception e) { } } /** * 从键盘获取一个String */ public String getAString() { String s = ""; try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); s = br.readLine(); } catch (Exception e) { } return s; } /** * 获取N右数第i位 */ public int getThe(int num, int i) { if (i > getBitCount(num) || i < 1) return -1; return (num % ((int) Math.pow(10, i))) / (int) Math.pow(10, i - 1); } /** * 获取一个数的位数 */ public int getBitCount(int n) { int i = 1; while (n / 10 > 0) { i++; n /= 10; } return i; } /** * 是素数 */ public boolean isSuShu(int n) { boolean isSuShu = true; if (n == 1 || n == 2) return true; for (int i = 2; i < Math.sqrt(n) + 1; i += 1) { if (n % i == 0) { return false; } } if (isSuShu == true) return true; else return false; } /** * 反序数 */ public int FanXuShu(int n) { int len = getBitCount(n); int s = 0; for (int i = 1; i < len + 1; i++) {// 123 s += Math.pow(10, len - i) * getThe(n, i); } return s; } /** * 判断是否两两不相等 */ 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[i] == a[j] && i != j) { // System.out.println("a[" + i + "]" + a[i] + " a[" + j + // "]" // + a[j] + "---"); return false; } } } return true; } /** * 判断是否全相等,只有一个数返回 */ public boolean allTheSame(int[] a) { if (a == null || a.length == 0) { return false; } if (a.length == 1) return true; for (int i = 1; i < a.length; i++) { if (a[0] != a[i]) { return false; } } return true; } /** * 获取重新排列后最大的数,如2310,得到3210,暂时只要四位 */ public int getTheMaxOrMin(int n, int maxOrMin) { if (n < 1000 || n > 9999) return -1; int a = getThe(n, 4); int b = getThe(n, 3); int c = getThe(n, 2); int d = getThe(n, 1); int temp = 0; int[] list = new int[] { a, b, c, d }; for (int i = 0; i < 4; i++) { for (int j = i; j < 4; j++) { if (maxOrMin == 1 ? (list[i] < list[j]) : (list[i] > list[j])) { temp = list[i]; list[i] = list[j]; list[j] = temp; } } } return list[0] * 1000 + list[1] * 100 + list[2] * 10 + list[3]; } }