Think in java 答案_Chapter 3_Additional Exercise

阅前声明: http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx

**(From Dan Forhan) Write a java program that will find all the possible “vampire numbers.” A vampire number is defined as a four digit numbers in which the two numbers multiplied are in the product, for example, 15 x 93 = 1395. The program should produce no duplicates, and be less than 50-60 lines of code.

// 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;
                }
              }
            }
          }
      }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值