Div. 7 (Codeforces 1633A)
Tags: Others, brute force

You are given an integer $$$n$$$. You have to change the minimum number of digits in it in such a way that the resulting number does not have any leading zeroes and is divisible by $$$7$$$.If there are multiple ways to do it, print any of them. If the given number is already divisible by $$$7$$$, leave it unchanged.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 990$$$) — the number of test cases.Then the test cases follow, each test case consists of one line containing one integer $$$n$$$ ($$$10 \le n \le 999$$$).

Output: For each test case, print one integer without any leading zeroes — the result of your changes (i. e. the integer that is divisible by $$$7$$$ and can be obtained by changing the minimum possible number of digits in $$$n$$$).If there are multiple ways to apply changes, print any resulting number. If the given number is already divisible by $$$7$$$, just print it.

Note: In the first test case of the example, $$$42$$$ is already divisible by $$$7$$$, so there's no need to change it.In the second test case of the example, there are multiple answers — $$$28$$$, $$$21$$$ or $$$63$$$.In the third test case of the example, other possible answers are $$$357$$$, $$$371$$$ and $$$378$$$. Note that you cannot print $$$077$$$ or $$$77$$$.

Sample Inputs:
3
42
23
377

Sample Outputs:
42
28
777

Solution (Python):
def d(n):
    l = '0123456789'
    for i in l:
        a = n[:-1] + i
        if int(a) % 7 == 0:
            return a

def f():
    n = input()
    x = int(n)
    if x % 7 != 0:
        x = d(n)
    print(x)
t = int(input())
for i in range(t):
    f()

Solution (Python):
a = int(input().strip())
str_list = []
for _ in range(a):
    str_list.append(str(input().strip()))
for num in str_list:
    if int(num) % 7 == 0:
        print(int(num))
    else:
        parcial_num = num
        for i in range(len(num)):
            if int(parcial_num) % 7 == 0:
                break
            parcial_num = num
            j = 1
            while j < 10 and int(parcial_num) % 7 != 0:
                parcial_num = parcial_num[:i] + str(j) + parcial_num[i + 1:]
                j += 1
        print(parcial_num)

Solution (Python):
def processing(n):
    if n % 7 == 0:
        return n
    else:
        k = n % 7
        temp_data = 7 - k
        if n % 10 + temp_data < 10:
            return n + temp_data
        else:
            return n - k
t = int(input())
while t > 0:
    n = int(input())
    print(processing(n))
    t -= 1