标签:语句 else contain str number stat ret solution for
这个算法很巧妙,一定要留下:
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是不是?
标签:语句 else contain str number stat ret solution for
原文地址:http://www.cnblogs.com/Sabre/p/7640550.html