zoukankan      html  css  js  c++  java
  • java1234教程系列笔记 S1 Java SE chapter 02 写乘法口诀表

    一、水仙花数

    1、方式一:这是我的思路,取各个位数的方式。我个人习惯于使用取模运算。

    public static List<Integer> dealNarcissiticNumberMethodOne(
                Integer startNum, Integer endNum) {
            List<Integer> resultList = new LinkedList<Integer>();
            for (Integer i = startNum; i <= endNum; i++) {
                Integer unitDigit = i % 10;
           // 该语句不够精炼,有冗余,应该写成 Integer tensDigit=i/10;即可 Integer tensDigit
    = (i / 10) % 10; Integer hundredsDigit = (i / 100) % 10; if (i == (unitDigit * unitDigit * unitDigit + tensDigit * tensDigit * tensDigit + hundredsDigit * hundredsDigit * hundredsDigit)) { resultList.add(i); } } return resultList; }

    2、方式二:视频的主讲人的方式如下:跟小学学习计数的方式一样。此时显示出数学理论的重要。

    public static List<Integer> dealNarcissiticNumberMethodTwo(
                Integer startNum, Integer endNum) {
            List<Integer> resultList = new LinkedList<Integer>();
            for (Integer i = startNum; i <= endNum; i++) {
                Integer hundredsDigit = i / 100;
                Integer tensDigit = (i - hundredsDigit * 100) / 10;
                Integer unitDigit = i - hundredsDigit * 100 - tensDigit * 10;
                if (i == (unitDigit * unitDigit * unitDigit + tensDigit * tensDigit
                        * tensDigit + hundredsDigit * hundredsDigit * hundredsDigit)) {
                    resultList.add(i);
                }
            }
            return resultList;
        }

    二、乘法口诀

    一段代码,调试了四次。需要运行看结果才倒推代码的缺陷。理想情况,应当是,先把逻辑梳理清楚。再梳理。切记

    代码如下:

     1 public static String generateMultiplication(Integer startNum, Integer endNum) {
     2         String result = "";
     3         for (int i = startNum; i <= endNum; i++) {
     4             for (int j = startNum; j <= i; j++) {
     5                 result += j + "*" + i + "=" + i * j + " ";
     6             }
     7             result += "
    ";
     8         }
     9         return result;
    10     }
  • 相关阅读:
    矩阵十题(7)
    线段树成段更新 poj 3468 A Simple Problem with Integers
    线段树单点更新 hdu 2795 Billboard
    线段树成段更新 poj 2528 Mayor's posters
    矩阵十题(10)
    矩阵十题(8)
    矩阵十题(5)
    矩阵十题(6)
    矩阵十题(9)
    矩阵十题(4)
  • 原文地址:https://www.cnblogs.com/siesteven/p/6235362.html
Copyright © 2011-2022 走看看