Editorial for Crooked Digits


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.
by: donchominkov

Sample solutions:

Video in BG

JavaScript

let number = gets();

while (number.length > 1) {
    let newNumber = 0;

    for (let ch of number) {
        if (ch === '.' || ch === '-') {
            continue;
        }

        newNumber += ch.charCodeAt(0) - '0'.charCodeAt(0);
    }

    number = newNumber.toString();
}

print(number);

Java

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String input = in.nextLine();
    int n = 0;
    for(int i = 0; i < input.length(); i ++) {
      char ch = input.charAt(i);
      if(Character.isDigit(ch)) {
        n += ch - '0';
      }
    }

    while(n > 9) {
      int newN = 0;

      while(n > 0) {
        newN += n % 10;
        n /= 10;
      }

      n = newN;
    }

    System.out.println(n);
  }
}

Comments

There are no comments at the moment.