2019 GOOGLE CODEJAM qualification round question 1 and solution

                                         FOREGONE SOLUTION:

PROBLEM:
Someone just won the Code Jam lottery, and we owe them N jamcoins! However, when we tried to print out an oversized check, we encountered a problem. The value of N, which is an integer, includes at least one digit that is a 4... and the 4 key on the keyboard of our oversized check printer is broken.
Fortunately, we have a workaround: we will send our winner two checks for positive integer amounts A and B, such that neither A nor B contains any digit that is a 4, and A + B = N. Please help us find any pair of values A and B that satisfy these conditions.

Input

The first line of the input gives the number of test cases, TT test cases follow; each consists of one line with an integer N.

Output

For each test case, output one line containing Case #x: A B, where x is the test case number (starting from 1), and A and B are positive integers as described above.
It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any one of them. (See "What if a test case has multiple correct solutions?" in the Competing section of the FAQ. This information about multiple solutions will not be explicitly stated in the remainder of the 2019 contest.)

Limits

1 ≤ T ≤ 100.
Time limit: 10 seconds per test set.
Memory limit: 1GB.
At least one of the digits of N is a 4.

Test set 1 (Visible)

1 < N < 105.

Test set 2 (Visible)

1 < N < 109.
Solving the first two test sets for this problem should get you a long way toward advancing. The third test set is worth only 1 extra point, for extra fun and bragging rights!

Test set 3 (Hidden)

1 < N < 10100.

Sample


Input

Output
3
4
940
4444

  
Case #1: 2 2
Case #2: 852 88
Case #3: 667 3777

  
In Sample Case #1, notice that A and B can be the same. The only other possible answers are 1 3 and 3 1.

SOLUTION:  PYTHON\

import sys
if __name__ == "__main__":
    f = sys.stdin
    if len(sys.argv) >= 2:
        fn = sys.argv[1]
        if fn != '-':
            f = open(fn)

    T = int(f.readline())
    for _T in xrange(T):
        S = f.readline().strip()

        A = list(S)
        B = ['0'] * len(A)

        for i in xrange(len(A)):
            if A[i] == '4':
                A[i] = '3'
                B[i] = '1'

        A = ''.join(A)
        B = ''.join(B).lstrip('0')
        print "Case #%d: %s %s" % (_T + 1, A, B)

C++:

#include<bits/stdc++.h>
using namespace std;

int main() {
ios_base::sync_with_stdio(0);
int T; cin >> T;

for (int case_num = 1; case_num <= T; case_num ++) {
string N; cin >> N;
string A;
string B;
for (char c : N) {
if (c == '4') {
A += '1';
B += '3';
} else {
if (!A.empty()) A += '0';
B += c;
}
}

cout << "Case #" << case_num << ": " << A << ' ' << B << '\n';
}

return 0;
}

Comments

Popular posts from this blog

Amazon Web Services

Hacker Rank all java and python problem solutions

Testing tools for React Apps