【问题标题】:Step by step long division in Java?在Java中一步一步长除法?
【发布时间】:2021-09-06 01:46:01
【问题描述】:

我正在尝试编写一个函数,在给定除数和除数的情况下输出逐步长除法问题。它应该看起来像这样:

     25 r 2                            
5 | 125                     
   -10                                 
     27                          
    -25                          
      2

我可以很容易地用垂直线写位,但我不知道如何用余数或减法循环格式化顶部。非常感谢任何帮助

【问题讨论】:

  • 在您的示例中,125 是除数,5 是除数吗?如果是这样,应该没有余数。
  • 是的,我认为 OP 错误地将第一个减法中的 2 添加到第二行的两个数字中。
  • 你应该向我们展示你到目前为止所做的代码。还有一些问题是,您的规范中有多少摆动空间,例如,如果数字的某些相对位置没有变化,可以添加多少空白。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: java division


【解决方案1】:

这是我对这个问题的解决方案,您可能需要执行一些错误检查以查看被除数是否大于除数。间距可能并不总是有效,但我尝试了几个数字,看起来还不错:

int divisor = 5;
int dividend = 127;
int answer = dividend / divisor;

// Set a space constant for formatting
int spaces = (int) Math.log10(dividend) + (int) Math.log10(divisor) + 4;

// Print the initial bracket
for(int i = 0; i < spaces - (int) Math.log10(answer); i ++)  {
    System.out.print(" ");
}
System.out.println(Integer.toString(answer) + " r " + Integer.toString(dividend % divisor));
System.out.println(Integer.toString(divisor) + " | " + Integer.toString(dividend));

// Do a while loop to do the subtraction
int remainder = dividend;
while(remainder != dividend % divisor) {
    
    // Find how much of the start of the remainder can be subtracted by making it into a string
    String test = Integer.toString(remainder);
    int sub = Integer.valueOf(test.substring(0, 1));
    test = test.substring(1);
    int exp = (int) Math.log10(remainder);
    while(sub < divisor) {
        sub = sub * 10 + Integer.valueOf(test.substring(0, 1));
        test = test.substring(1);
        exp--;
    }
    
    int multiple = sub - (sub % divisor);
    
    //Print the subtraction and remainder lines
    for(int i = 0; i < spaces - 1 - exp - (int) Math.log10(multiple); i++) {
        System.out.print(" ");
    }
    System.out.println("-" + Integer.valueOf(multiple));
    
    remainder -= multiple * Math.pow(10, exp);
    
    for(int i = 0; i < spaces - (int) Math.log10(remainder); i++) {
        System.out.print(" ");
    }
    System.out.println(Integer.valueOf(remainder));

}

棘手的部分是计算出需要隔离多少余数(例如,对于 127 和 5,1 不能除以 5,所以我需要使用 12),这是通过将余数变成字符串来实现的一次解释一个字符(这可以在数学上完成,但是当我第一次尝试时它不起作用,所以我放弃了)。

除数 = 12,除数 = 5 的样本输出:

     25 r 2
5 | 127
   -10
     27
    -25
      2

【讨论】:

    【解决方案2】:

    填充一个对象,然后调用打印格式。

    LongDivision.java

    public class LongDivision {
        /**
         * The Number.
         */
        String number;
        /**
         * The Divider.
         */
        String divider;
        /**
         * The Result.
         */
        String result;
        /**
         * The Lines.
         */
        List<String> lines;
    
        /**
         * Instantiates a new Long divider.
         *
         * @param number  the number
         * @param divider the divider
         * @param result  the result
         * @param lines   the lines
         */
        public LongDivision(String number, String divider, String result, List<String> lines) {
            this.number = number;
            this.divider = divider;
            this.result = result;
            this.lines = lines;
        }
    
        /**
         * Gets number.
         *
         * @return the number
         */
        public String getNumber() {
            return number;
        }
    
        /**
         * Sets number.
         *
         * @param number the number
         */
        public void setNumber(String number) {
            this.number = number;
        }
    
        /**
         * Gets divider.
         *
         * @return the divider
         */
        public String getDivider() {
            return divider;
        }
    
        /**
         * Sets divider.
         *
         * @param divider the divider
         */
        public void setDivider(String divider) {
            this.divider = divider;
        }
    
        /**
         * Gets result.
         *
         * @return the result
         */
        public String getResult() {
            return result;
        }
    
        /**
         * Sets result.
         *
         * @param result the result
         */
        public void setResult(String result) {
            this.result = result;
        }
    
        /**
         * Gets lines.
         *
         * @return the lines
         */
        public List<String> getLines() {
            return lines;
        }
    
        /**
         * Sets lines.
         *
         * @param lines the lines
         */
        public void setLines(List<String> lines) {
            this.lines = lines;
        }
    
        @Override
        public String toString() {
            return String.format(
                    "LongDivider (number=%s, divider=%s, result=%s, lines=%s)", this.number, this.divider, this.result, this.lines);
        }
    
        /**
         * print format.
         *
         * @return the string
         */
        public String printFormat() {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(result+"\n");
            stringBuilder.append(repeat("__", 2));
            stringBuilder.append("\n");
            stringBuilder.append(number + "| " + divider);
            stringBuilder.append("\n");
            lines.stream().forEach(s -> stringBuilder.append(s+"\n"));
            return stringBuilder.toString();
        }
    
        public static String repeat(String s, int n) {
            if(s == null) {
                return null;
            }
            final StringBuilder sb = new StringBuilder(s.length() * n);
            for(int i = 0; i < n; i++) {
                sb.append(s);
            }
            return sb.toString();
        }
    }
    

    主类

    public class LongDividerSolution {
        /**
         * The entry point of application.
         *
         * @param args the input arguments
         */
        public static void main(String[] args) {
            String number = "945";
            String divider = "4";
            String result = "236 (1)";
            String lineSeparator = "_";
            int max = Math.max(number.length(), result.length());
            // TODO Implement the calculation itself.
            List<String> strings = new ArrayList<>();
            strings.add("8");
            strings.add(LongDivision.repeat(lineSeparator, max));
            strings.add("14");
            strings.add(" 12");
            strings.add(LongDivision.repeat(lineSeparator,max));
            strings.add("  25");
            strings.add("   24");
            strings.add(LongDivision.repeat(lineSeparator,max));
            strings.add("( 1 )");
            LongDivision longDivision = new LongDivision(number, divider, result,strings);
            System.out.println(longDivision.printFormat());
        }
    }
    

    输出:

    236 (1)
    ___
    945| 4
    8
    _____
    14
     12
    _____
      25
       24
    _____
    ( 1 )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-25
      • 2015-05-25
      • 2012-08-29
      • 1970-01-01
      • 2017-08-06
      • 2011-01-25
      • 2011-12-15
      • 2012-05-21
      相关资源
      最近更新 更多