Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1867
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1867
The Greater One-digit Number
By Ricardo Martins, IFSULDEMINAS Brazil
Timelimit: 1
The inhabitants of the planet Uno have a terrible numbers detection problem with more than one digit, so that , for everything that will make transform any integer value in a one-digit number by performing successive sums of the number until it is reduced a digit. For example , the number 999999999991 , the planet Uno , summing up all the numbers, resulting in 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 1 = 100. Since the number 100 has more than one digit, the process repeats , resulting in 0 + 0 + 1 = 1 One of the great difficulties that people have is to compare two numbers and see which one is greater , under the rules of the planet.
Write a program that , given two integers , identify which one is the biggest one-digit number .
Input
There will be several test cases . Each test case begins with two integers N and M ( 0 ≤ N ≤ 10100, 0 ≤ M ≤ 10100 ), indicating the two numbers to be compared. The last test case is given when N = M = 0 , and this case will not be processed.
Output
For each test case , print a line containing an integer, indicating 1 if the first number is the largest of a number , 2 if the second number is the largest of a number or 0 if both numbers have the same value of a number.
Input Sample | Output Sample |
111 2 22 55 123 222 12 4 0 0 |
1 1 0 2 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define true 1
#define false 0
char num1[1010];
char num2[1010];
int solve(char *);
int main(int argc, char **argv)
{
while (true)
{
scanf("%s %s", num1, num2);
if (strcmp(num1, "0") == 0 && strcmp(num2,"0") == 0)
break;
int x = solve(num1);
int y = solve(num2);
if (x > y)
putchar_unlocked('1');
else if (x < y)
putchar_unlocked('2');
else
putchar_unlocked('0');
putchar_unlocked('\n');
}
return 0;
}
int solve(char *str)
{
int i, x;
if (strlen(str) == 1)
return (int)(str[0] - 48);
i = x = 0;
while (str[i])
x += (int)(str[i++] - 48);
i = 0;
while (x)
str[i++] = (char)((x % 10) + 48), x /= 10;
str[i] = 0;
return solve(str);
}
Copy The Code &
Try With Live Editor
Input
22 55
123 222
12 4
0 0
Output
1
0
2
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int val(long long x)
{
while (x >= 10)
{
long long aux = x;
long long s = 0;
while (aux)
{
s += aux % 10;
aux /= 10;
}
x = s;
}
return x;
}
int compare(long long a, long long b)
{
return (a > b ? 1 : a < b ? 2 : 0);
}
int main()
{
string a, b;
long long a1, b1;
while (1)
{
cin >> a >> b;
if (a == "0" && b == "0") return 0;
a1 = b1 = 0;
for (int i = 0 ; i < a.size(); ++i) a1 += a[i] - '0';
for (int i = 0 ; i < b.size(); ++i) b1 += b[i] - '0';
cout << compare(val(a1), val(b1)) << '\n';
}
}
Copy The Code &
Try With Live Editor
Input
22 55
123 222
12 4
0 0
Output
1
0
2