V
V
Vitaly Ilnitsky2017-10-17 23:25:05
Java
Vitaly Ilnitsky, 2017-10-17 23:25:05

Column division java?

It is necessary to implement division into a column
. I don’t know which side to approach, can you describe the algorithm of actions?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Romanov, 2017-10-17
@Djaler

1. Take 2 numbers.
2. Divide one by the other in a column on a leaf.
3. Write down actions
4. Think about them

B
bedward70, 2017-10-26
@bedward70

Good problem. Here's what happened:

public class AppDivide {
    public static void main(String[] args) throws Exception {

        /** Делимое */
        int dividend = 554223;
        /** Делитель */
        int divider = 11;

        divide(dividend, divider);
    }

    private static void divide(int dividend, int divider) throws Exception {
        validate(dividend);
        validate(divider);

        // Находим мин длину для отображения (+1 - это для знака минус)
        int maxLen = 1 + Math.max(Integer.valueOf(dividend).toString().length(), Integer.valueOf(divider).toString().length());
        String intFormat = "%" + maxLen + "d";
        System.out.println(generateTab(maxLen + maxLen + 2, '='));
        System.out.println(" " + String.format(intFormat, dividend) + "|" + String.format(intFormat, divider));

        int result = 0;
        while (true) {

            // Найти следующую цифру
            Num num = divideNext(dividend, divider);
            if (num.getDigit() == 0) {
                break;
            }

            // Вывод вычитаемого значения и полученной цифры
            String numFormat = "%" + (maxLen - num.getPointPosition()) + "d";
            System.out.println(" " + String.format(numFormat, -divider * num.getDigit()) + generateTab(num.getPointPosition(), ' ') + "|" + String.format(numFormat, num.getDigit()));

            // Модифицирование остатка и сохранение в результат
            result += num.getValue();
            dividend -= num.getValue() * divider;

            // Вывод линии и остатка
            System.out.println(generateTab(maxLen + maxLen + 2, '-'));
            System.out.println(" " + String.format(intFormat, dividend) + "|" );

        }

        // Вывод остатка и результата
        System.out.println(generateTab(maxLen + maxLen + 2, '='));
        System.out.println(" " + String.format(intFormat, dividend) + "|" + String.format(intFormat, result));
    }

    private static Num divideNext(int dividend, int divider) {
        int pointPosition = 0;
        if (dividend < divider) {
            return new Num(0,0);
        }
        while(dividend > divider * 10) {
            pointPosition ++;
            divider *= 10;
        }
        int count = 1;
        while(dividend > divider * (count + 1)) {
            count ++;
        }
        return new Num(count, pointPosition);
    }

    private static String generateTab(int length, char ch) {
        StringBuilder result = new StringBuilder();
        for (int i = 0 ; i < length; i++) {
            result.append(ch);
        }
        return result.toString();
    }

    private static void validate(int value) throws Exception {
        if (value < 0) {
            throw new Exception("Validation error: " + value);
        }
    }

     private static class Num {

         private final int digit;
         private final int pointPosition;

         public Num(int value, int pointPosition) {
             this.digit = value;
             this.pointPosition = pointPosition;
         }

         public int getDigit() {
             return digit;
         }

         public int getPointPosition() {
             return pointPosition;
         }

         public int getValue() {
             int result = digit;
             for (int i = 0; i < pointPosition; i++) {
                 result *= 10;
             }
             return result;
         }
     };
}

Here's what the result looks like:
================
  554223|     11
 -55    |  5
----------------
    4223|
   -33  |    3
----------------
     923|
    -88 |     8
----------------
      43|
     -33|      3
----------------
      10|
================
      10|  50383

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question