TopcoderARCHIVE
Archive/Problems/MakeItDivisible
SRM · Problem 17370

MakeItDivisible

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a non-negative int N.

You want an integer that is divisible by 7. If N is already divisible by 7, you are happy and you don't need to change it.

If N isn't divisible by 7, you want to change it to a multiple of 7 by changing as few digits of N as possible.

Make those changes and return the new number. Any valid solution will be accepted.

Definition

Class:
MakeItDivisible
Method:
change
Parameters:
int
Returns:
int
Method signature:
int change(int N)
(be sure your method is public)

Notes

  • You are only allowed to change digits, not to add or remove them. Thus, the number you return must always have the same number of digits as the number N you are given.

Constraints

  • N will be between 0 and 999,999,999, inclusive.

Examples

  1. 7028
    Returns: 7028
    This number is divisible by 7, so the optimal course of action is not to change anything.
  2. 1111
    Returns: 1113
    This number is not divisible by 7. There are five ways to turn it into a multiple of 7 by changing just a single digit: you can return any of the numbers 1113, 1141, 1211, 1911, and 6111. The number 1127 is divisible by 7, but this number is not a valid answer: one needs to change two digits to produce it from 1111. The number 41111 is divisible by 7, but it is also not a valid answer: you are not allowed to add digits.
  3. 17
    Returns: 77
    You may change this number either to 77 (changing the first digit from 1 to 7) or to 14 (changing the second digit from 7 to 4). Note that you cannot change the first digit from 1 to 0. The new number cannot have unnecessary leading zeros. You cannot return "07", and you cannot return "7" because it doesn't have the same number of digits as "17".
  4. 1234567
    Returns: 1232567
  5. 3
    Returns: 0
    You may return either 0 or 7.
← All problems