Almost every online judge has a “A+B” problem. The first problem (easiest) is often present to allow testing if online judges are working (of course they are!)
This problem can be found at Timus Online Judge.
Of course, almost every registered users will AC this simple problem after immediate registration, to boost their confidence.
This problem, is so simple, but don’t have so much details… for example, the range of inputs are unknown.
The first C (pure C) problem is like this:
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d", a + b);
return 0;
}
This gets Accepted. At least we know the integer can be represented (at least by test cases of this problem) by 32-bit signed integer, and the result won’t overflow.
How about unsigned 32 integer?
#include <stdio.h>
int main() {
unsigned int a, b;
scanf("%u %u", &a, &b);
printf("%u", a + b);
return 0;
}
Yes, it gets AC as well. So the test cases are not strong. Unsigned 32-bit integers seem enough. Let’s try more.
#include <stdio.h>
int main() {
unsigned short a, b;
scanf("%hu %hu", &a, &b);
printf("%hu", a + b);
return 0;
}
Wow, this is also correct… We use unsigned short int, which is 2 byte only (0 to 65535), and even the result is not overflow…. But this does not work for 1-byte char type.
Let’s try if 32-bit x86 Assembly…
#include <stdio.h>
int main() {
unsigned int a, b;
scanf("%u %u", &a, &b);
__asm {
mov eax, a
add eax, b
mov b, eax
}
printf("%u", b);
return 0;
}
Yes, it works! That means the test-machines are Windows (pick ‘Visual Studio C’ or ‘Visual Studio C++’).
Let’s try C++ solution as well.
#include &iostream>
using namespace std;
int main() {
unsigned int a, b;
cin >> a >> b;
__asm {
mov eax, a
add eax, b
mov b, eax
}
cout << b;
return 0;
}
Too simple! but you may try to complicate this using some fun ideas, for example, loop this!
#include &iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
for (; a > 0; a --) b ++;
cout << b;
return 0;
}
Still too easy,, makes it harder using recursion.
#include <iostream>
using namespace std;
int add(int a , int b)
{
if (a <= 1 && b <= 1)
return a + b;
return add(a / 2 , b / 2) + add(a / 2, b / 2) + (b % 2) + (a % 2);
}
int main() {
int a , b;
cin>>a>>b;
cout<<add(a, b)<<endl;
return 0;
}
And this is possible by solving using ‘Dynamic Programming’, ‘Greedy Algorithm’, ‘Network Flow’ …. (just let imaginations fly)
–EOF (The Ultimate Computing & Technology Blog) —
582 wordsLast Post: Coding Exercise - Timus 1001 - Reverse Root - C++ solution - Online Judge
Next Post: Coding Exercise - Timus 1785. Lost in Localization - C++ solution - Online Judge
#include
main ()
{
int a,b;
scanf(“%d%d”,&a,&b);
printf(“%d”,a+b);
return 0;
}