算法是什么
问题:
吸血贵数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排序.
以两个0结尾的数字是不允许的,例如,下列数字都是"吸血鬼"数字;
1260=21*60
1827=21*87
2187=27*81
求算法.
问题:
吸血贵数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排序.
以两个0结尾的数字是不允许的,例如,下列数字都是"吸血鬼"数字;
1260=21*60
1827=21*87
2187=27*81
求算法.
/**
* 吸血鬼数字,高效率版本.<br>
* 一个4位数字,可以拆分2个2位数数字的乘积,顺序不限。<br>
* 比如 1395 =15 * 93
*
* @author 老紫竹(laozizhu.com)
*/
public class Vampire {
public static void main(String[] arg) {
String[] ar_str1, ar_str2;
int sum = 0;
int from;
int to;
int i_val;
int count = 0;
// 双重循环穷举
for (int i = 10; i < 100; i++) {
// j=i+1避免重复
from = Math.max(1000 / i, i + 1);
to = Math.min(10000 / i, 100);
for (int j = from; j < to; j++) {
i_val = i * j;
if (i_val % 100 == 0 || (i_val - i - j) % 9 != 0) {
continue;
}
count++;
ar_str1 = String.valueOf(i_val).split("");
ar_str2 = (String.valueOf(i) + String.valueOf(j)).split("");
Arrays.sort(ar_str1);
Arrays.sort(ar_str2);
if (Arrays.equals(ar_str1, ar_str2)) {// 排序后比较,为真则找到一组
sum++;
System.out.println("第" + sum + "组: " + i + "*" + j + "=" + i_val);
}
}
}
System.out.println("共找到" + sum + "组吸血鬼数");
System.out.println(count);
}
}
thank you!!!
public class XNumber {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个整数:");
String s=sc.next();
boolean flag=false;
int number1 = 0;
int number2 = 0;
int number = 0;
if(s.length()%2!=0){
System.out.println("你输入的数不是吸血鬼数");//位数为奇数个肯定不是吸血鬼数
}
else{
number=Integer.parseInt(s);
int no=s.length()/2;
for(int i=(int)Math.pow(10,no-1);i<Math.pow(10,no);i++){
for(int j=(int)Math.pow(10,no-1);j<Math.pow(10,no);j++){
if(i*j==number&&(i%10!=0||j%10!=0)){
flag=true;
number1=i;
number2=j;
break;
}
}
}
}
if(flag)
System.out.println(number+ "="+ number1 +"*"+number2);
else
if(s.length()%2==0)
System.out.println("你输入的数不是吸血鬼数");//为避免和上面重复打印。
}
}
if (i_val % 100 == 0 || (i_val - i - j) % 9 != 0) {
continue;
}
count++;
----------------------------------------------------------------------
(i_val - i - j) % 9 != 0) 是干什么用的啊
还有count 是用来做什么的啊,
我调试了好久都没看不来是什么意思,
希望大虾能不帮帮忙,
这里三楼的更容易理解一些http://topic.csdn.net/u/20071031/12/2ea68c43-8267-426f-b180-fce95b23e100.html
static int a(int i) {
return i/1000;
}
static int b(int i) {
return (i%1000)/100;
}
static int c(int i) {
return ((i%1000)%100)/10;
}
static int d(int i) {
return ((i%1000)%100)%10;
}
static int com(int i, int j) {
return (i * 10) + j;
}
static void productTest (int i, int m, int n) {
if(m * n == i) System.out.println(i + " = " + m + " * " + n);
}
public static void main(String[] args) {
for(int i = 1001; i < 9999; i++) {
productTest(i, com(a(i), b(i)), com(c(i), d(i)));
productTest(i, com(a(i), b(i)), com(d(i), c(i)));
productTest(i, com(a(i), c(i)), com(b(i), d(i)));
productTest(i, com(a(i), c(i)), com(d(i), b(i)));
productTest(i, com(a(i), d(i)), com(b(i), c(i)));
productTest(i, com(a(i), d(i)), com(c(i), b(i)));
productTest(i, com(b(i), a(i)), com(c(i), d(i)));
productTest(i, com(b(i), a(i)), com(d(i), c(i)));
productTest(i, com(b(i), c(i)), com(d(i), a(i)));
productTest(i, com(b(i), d(i)), com(c(i), a(i)));
productTest(i, com(c(i), a(i)), com(d(i), b(i)));
productTest(i, com(c(i), b(i)), com(d(i), a(i)));
}
}
}