包含vampirejava的词条

博主:adminadmin 2022-12-14 22:21:06 64

本篇文章给大家谈谈vampirejava,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

什么是吸血鬼数字,由来?

所谓“吸血鬼数字”就是指位数为偶数的数字(我们算得是4位的),可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数字,其中从偶数位数字中选取的数字可以任意排列。以两个0截尾的数字是不允许的。例如:1260=21*60 1827=21*87 2187=27*81

今天在Thinking in JAVA 中做到的练习题。我先自己写了一个算法,但是总感觉太复杂。后来在网上找到一个比较好的算法。如下:

class Eg0410_Vampire{

public static void main(String[] args){

String[] ar_str1,ar_str2;

int sum=0;

for(int i=10;i100;i++){

for(int j=i+1;j100;j++){

int i_val=i*j;

if(i_val1000||i_val9999) 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+"组吸血鬼数");

}

}

这个算法应该是比较简单的了。算法中用到String.valueOf(j)).split("");的方法来把数字转换为字符串,以拆分数字的方法用的很巧妙了。把数字的比较,转换为对字符串的比较,实在高明。

下面是我在《Thinking in Java, 2nd edition, Annotated Solution Guide Revision 1.0》中找到的答案,也就是Thinking in JAVA的官方答案书上的答案。不过个人感觉没有上面的算法好。

//: 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;

}

}

}

}

}

}

} ///:~

Java中计算吸血鬼数字为什么要对9取余

int product = num1 * num2;

product= 1000a + 100b + 10c + d;

因为

num1 = 10a + b

num2 = 10c + d

则 product- num1 - num2

=1000a + 100b + 10c + d - (10a + b)- (10c + d)

=990a + 99b = 9 * (110a + 11b)

所以product- num1 - num2能被9整除。

java程序补全

A:

public void frighten() {

System.out.println("Arrrgh!");

}

B:

public void frighten() {

System.out.println("A bite?");

}

要问为什么,懒得说,仔细想一下类的继承和多态.

java 找出所有4位数的吸血鬼数字,帮下面这个代码,使能编译运行。

public class Vampire {

   public static void main(String[] args) {

      String s="";

      for(int i=1;i10;i++){

          for(int j=0;j10;j++){

              s+=(10*i+j)+" ";  //这里有改动,双引号里面应该有个空格。

          }

      }

      //System.out.println(s);

      String[] arr=s.split(" ");

      System.out.println("两位数的个数:"+arr.length);

      int a,b;

      int count=0;

      for(int j=0;jarr.length;j++){

          for(int k=0;karr.length;k++){

              a=Integer.valueOf(arr[j]).intValue();

              b=Integer.valueOf(arr[k]).intValue();

              if(a*b10000a*b1000(a*b)%100!=0){

                  count++;

                  System.out.println(arr[j]+" "+arr[k]+" ");

              }

          }

          System.out.println();

      }

      System.out.println("The total number is "+count);

  }

}

java 父类引用子类对象时编译器报错

//已为你改好。

//你的问题在于将类Monster、Vampire、Dragon定义成了类MonsterTestDrive的非静态内部类(这样定义也对,

//但使用类时的方法是不同的,下面有一个例子)。

package monsterTestDrive;

public class MonsterTestDrive {

public static void main(String[] args) {

final Monster[] ma = new Monster[3];

ma[0] = new Vampire();

ma[1] = new Dragon();

ma[2] = new Monster();

for(Monster m : ma)

m.frighten();

}

}

class Monster{

boolean frighten() {

System.out.println("arrgh");

return true;

}

}

class Vampire extends Monster {

boolean frighten() {

System.out.println("a bite?");

return true;

}

}

class Dragon extends Monster {

boolean frighten() {

System.out.println("breath fire");

return true;

}

}

//定义成非静态内部类的版本

//package monsterTestDrive;

//public class MonsterTestDrive {

// public static void main(String[] args) {

// MonsterTestDrive mtd=new MonsterTestDrive();

// final Monster[] ma = new Monster[3];

// ma[0] = mtd.new Vampire();

// ma[1] = mtd.new Dragon();

// ma[2] = mtd.new Monster();

// for(Monster m : ma)

// m.frighten();

// }

// class Monster{

// boolean frighten() {

// System.out.println("arrgh");

// return true;

// }

// }

// class Vampire extends Monster {

// boolean frighten() {

// System.out.println("a bite?");

// return true;

// }

// }

// class Dragon extends Monster {

// boolean frighten() {

// System.out.println("breath fire");

// return true;

// }

// }

//}

vampirejava的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、vampirejava的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-14,除非注明,否则均为首码项目网原创文章,转载请注明出处。