吸血鬼数字算法
Posted 王明辉的部落
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了吸血鬼数字算法相关的知识,希望对你有一定的参考价值。
这个算法很巧妙,一定要留下:
class Eg0410_Vampire { public static void main(String[] args) { String[] ar_str1, ar_str2; int sum = 0; for (int i = 10; i < 100; i++) { for (int j = i + 1; j < 100; j++) { int i_val = i * j; if (i_val < 1000 || i_val > 9999) continue; // 积小于1000或大于9999排除,继续下一轮环 ar_str1 = String.valueOf(i_val).split(""); ar_str2 = (String.valueOf(i) + String.valueOf(j)).split(""); java.util.Arrays.sort(ar_str1); java.util.Arrays.sort(ar_str2); if (java.util.Arrays.equals(ar_str1, ar_str2)) { // 排序后比较,为真则找到一组 sum++; System.out.println("第" + sum + "组: " + i + "*" + j + "=" + i_val); } } } System.out.println("共找到" + sum + "组吸血鬼数"); } }
以下是官方答案,比较麻烦,完全没有涉及本节中所讲的中止循环语句:
// : c03:E11_Vampire.java // Solution by Dan Forhan import java.applet.*; import java.awt.*; public class Vampire extends Applet { private int num1, num2, product; private int[] startDigit = new int[4]; private int[] productDigit = new int[4]; private int count = 0; private int vampCount = 0; private int x, y; public void paint(Graphics g) { g.drawString("Vampire Numbers", 10, 20); g.drawLine(10, 22, 150, 22); // Positioning for output to applet: int column = 10, row = 35; for (num1 = 10; num1 <= 99; num1++) for (num2 = 10; num2 <= 99; num2++) { product = num1 * num2; startDigit[0] = num1 / 10; startDigit[1] = num1 % 10; startDigit[2] = num2 / 10; startDigit[3] = num2 % 10; productDigit[0] = product / 1000; productDigit[1] = (product % 1000) / 100; productDigit[2] = product % 1000 % 100 / 10; productDigit[3] = product % 1000 % 100 % 10; count = 0; for (x = 0; x < 4; x++) for (y = 0; y < 4; y++) { if (productDigit[x] == startDigit[y]) { count++; productDigit[x] = -1; startDigit[y] = -2; if (count == 4) { vampCount++; int a = (int) Math.random() * 100; int b = (int) Math.random() * 100; int c = (int) Math.random() * 100; if (vampCount < 10) { g.drawString("Vampire number " + vampCount + " is " + num1 + " * " + num2 + " = " + product, column, row); row += 20; } else { g.drawString("Vampire number " + vampCount + " is " + num1 + " * " + num2 + " = " + product, column, row); row += 20; } } } } } } } /// :~
下面是我写的,比较随意,没有整理,排除法,这个不行,那个不行,剩下的就是行的了。全面地用到了本节的各种中止循环及标签。直接输出是18个数字,去掉乘数互换的一半,4位的应当是9个数字。
其实没太明白“吸血鬼数字”是什么意思,为什么叫这个名字,为什么只能是4位数,6位行不行?8位行不行?16位呢?如果这是个隐喻,那么这个隐喻不是很通用,吸血鬼在西方可能很通用,他们有很多特点,作者也许取其一,西方人一看就知道,但是对于我们不是很了解的人,吸血鬼有什么特点?吸血?面色仓白?尖牙?黑暗?这些都跟这些4位数似乎没什么联系。也没有查到合理的解释。
static void vampire() { int result = -1; String strResult = ""; String strj = "", strj2 = ""; String[] arrayj, arrayj2; String strTotal = ""; String[] arrResult; int n = 0; int limit1 = 1000, limit2 = 1000; ArrayList<String> arrTotal = new ArrayList<String>(); ArrayList<Integer> arrTotalCheck = new ArrayList<Integer>(); System.out.println("吸血鬼数字:"); for (int num1 = 1; num1 < limit1; num1++) { strj = Integer.toString(num1); if (Integer.toString(num1).length() < 2) continue; l2: for (int num2 = 1; num2 < limit2; num2++) { strj2 = Integer.toString(num2); if (Integer.toString(num2).length() < 2) continue; result = num1 * num2; strTotal = strj + strj2; strResult = Integer.toString(result); arrResult = strResult.split(""); arrayj = String.valueOf(num1).split(""); arrayj2 = String.valueOf(num2).split(""); for (String t1 : arrayj) { if (!strResult.contains(t1)) continue l2; } for (String t2 : arrayj2) { if (!strResult.contains(t2)) continue l2; } for (String tTotal : arrResult) { if (!strTotal.contains(tTotal)) continue l2; } arrayj2 = String.valueOf(num2).split(""); if (result >= limit1 * limit2) continue; else if (Integer.toString(result).length() < 4) continue; else if (strResult.length() % 2 != 0) continue; else if (result % 100 == 0) continue; else if (strj.length() != strj2.length()) continue; else { n++; // System.out.println(n + "." + num1 + "*" + num2 + "=" + result + ","); if (result == 102510 || result == 105264 || result == 105750 || result==108135 || result==110758 ) { if(arrTotalCheck.contains(result)) continue; arrTotalCheck.add(result); arrTotal.add(n + "." + num1 + "*" + num2 + "=" + result + ","); }; } } } for (String i : arrTotal) { System.out.println(i); } return; }
根据某百科上的资料可知,有6位的吸血鬼数字,那么有8位的吗?10位的?12位的?理论上应该有,偶数位即可。
那么5775是不是?
4位数的吸血鬼数有7个:
1260, 1395, 1435, 1530, 1827, 2187, 6880
6位数的吸血鬼数字有141个:
102510, 104260, 105210, 105264, 105750, 108135, 110758, 115672, 116725, 117067, 118440, 123354, 124483, 125248, 125433, 125460, 126027, 126846, 129640, 129775, 131242, 132430, 133245, 134725, 135828, 135837, 136525, 136948, 140350, 145314, 146137, 146952, 152608, 152685, 153436, 156240, 156289, 156915, 162976, 163944, 172822, 173250, 174370, 175329, 180225, 180297, 182250, 182650, 186624, 190260, 192150, 193257, 193945, 197725, 201852, 205785, 211896, 213466, 215860, 216733, 217638, 218488, 226498, 226872, 229648, 233896, 241564, 245182, 251896, 253750, 254740, 260338, 262984, 263074, 284598, 284760, 286416, 296320, 304717, 312475, 312975, 315594, 319059, 319536, 326452, 329346, 329656, 336550, 336960, 338296, 341653, 346968, 361989, 362992, 365638, 368550, 369189, 371893, 378418, 378450, 384912, 386415, 392566, 404968, 414895, 416650, 416988, 428980, 429664, 447916, 456840, 458640, 475380, 486720, 489159, 489955, 498550, 516879, 529672, 536539, 538650, 559188, 567648, 568750, 629680, 638950, 673920, 729688, 736695, 738468, 769792, 789250, 789525, 792585, 794088, 809919, 809964, 815958, 829696, 841995, 939658
其中125460比较特殊,因为125640=204*615且125640=246*510.
伪吸血鬼数和一般吸血鬼数不同之处在于其尖牙不强制是n/2个位的数,故伪吸血鬼数的位数可以是奇数。
2002年Carlos Rivera定义了质吸血鬼数:尖牙是质因子的吸血鬼数,例如117067, 124483, 146137, 371893, 536539。
根据“尖牙不强制是n/2个位的数”来推断,尖牙指的是两个乘数,但是,为什么?哪里尖了?莫非是说对称?那也太牵强了。
以上是关于吸血鬼数字算法的主要内容,如果未能解决你的问题,请参考以下文章