Saturday, June 27, 2009

59. John and Tiger

Problem - John and the tiger
------------------------------

N is a random number, which for some reason, is at least two digits.
John Doe, a nondescript man, performs an operation on N: he chops off
the last digit to form a new number M, and then finds N-M. This excites
him in a hard-to-justify way. He then tells you N-M. Thrilled by the
fascinating back-story behind this number, you make it your life goal to
figure out what N was.

By the way, John was later eaten by a tiger.

Input
-----

Input consists of multiple lines, one line per case. Each line contains
a single positive integer between 10 and 10^18 inclusive, giving the
value of N-M. Input is terminated by a line containing 0.

Output
------

For each case, print one line containing the possible values of N in
sorted order. Separate consecutive numbers with a single space.

Sample Input
------------

18
0

Output for Sample Input
-----------------------

19 20

2 comments:

Anurag said...

one solution in c++;
#include iostream.h
int main()
{
long long nm, a;
cin >> nm;
while (nm != 0)
{
// We know that nm = n - n/10 + k, where k = i/10, i:0..9. We will
// solve this for n, and keep only integer solutions
for (int i=9;i>=0;i--)
{
a = nm - i;
// For each value of i we need to see whether 10*nm-i is
// divisible by 9. However, for large values of nm, this will
// create an overflow. We can avoid this problem since if
// 10*nm-i is divisible by 9, so is nm-i, and vice versa
if (a % 9 == 0)
{
cout << a / 9 + nm;
// The only case where there are two values to output is when
// nm and nm-9 are divisible by 9. That's why we output a
// space only when just found the first of the two numbers
if (i == 9) cout << " ";
}
}
cout << endl;
cin >> nm;
}
}

Anurag said...

sol in java:
import java.io.*;

class C {

void Begin()
{
String input = readLine();
long nm = Long.parseLong(input);
while (nm != 0)
{
// We know that nm = n - n/10 + k, where k = i/10, i:0..9. We will
// solve this for n, and keep only integer solutions
for (int i=9;i>=0;i--)
{
long a = nm - i;
// For each value of i we need to see whether 10*nm-i is
// divisible by 9. However, for large values of nm, this will
// create an overflow. We can avoid this problem since if
// 10*nm-i is divisible by 9, so is nm-i, and vice versa
if (a % 9 == 0)
{
System.out.print(a / 9 + nm);
// The only case where there are two values to output is when
// nm and nm-9 are divisible by 9. That's why we output a
// space only when just found the first of the two numbers
if (i == 9) System.out.print(" ");
}
}
System.out.println();
input = readLine();
nm = Long.parseLong(input);
}
}

BufferedReader b = new BufferedReader(new InputStreamReader(System.in));;

String readLine()
{
String result = "";
try
{
result = b.readLine();
}
catch (IOException e) {}
return result;
}

public static void main(String args[])
{
C myWork = new C(); // create a dynamic instance
myWork.Begin(); // the true entry point
}
}

Post a Comment