UVA Problem No. 10055, Hashmat The Brave Warrior is probably the easiest problem. The input consists of a pair of pairs of unsigned integers ≤ 2 ^ 32 (thus, to use 64-bit integers ...) For each pair, the task is to print the difference between the larger and smaller integer.
According to statistics , the fastest solutions work in less than 0.01 sec. However, all my attempts to solve this are usually carried out after 0.02 s, probably with deviations of ± 0.01 s.
I tried:
#include <cstdint>
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
uint_fast64_t i, j;
while(cin >> i >> j) {
if(i > j)
cout << i-j << '\n';
else
cout << j-i << '\n';
}
}
And:
#include <cstdlib>
#include <cstdint>
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int_fast64_t i, j;
while(cin >> i >> j) {
cout << abs(i-j) << '\n';
}
}
And:
#include <algorithm>
#include <cstdint>
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
uint_fast64_t i, j;
while(cin >> i >> j) {
cout << max(i,j)-min(i,j) << '\n';
}
}
All with the same results.
printf()/scanf() cin/cout, ( , , cin/cout, cin.tie(nullptr), , printf()/scanf() - , - cstdio Im, ).
0,01 ., , , , , , ?
C++11 5.3.0 - GNU C++ Compiler with options: -lm -lcrypt -O2 -std=c++11 -pipe -DONLINE_JUDGE.
EDIT: @Sorin @MSalters:
#include <stdio.h>
#include <stdint.h>
unsigned long long divisors[] = {
1000000000,
1000000000,
1000000000,
1000000000,
100000000,
100000000,
100000000,
10000000,
10000000,
10000000,
1000000,
1000000,
1000000,
1000000,
100000,
100000,
100000,
10000,
10000,
10000,
1000,
1000,
1000,
1000,
100,
100,
100,
10,
10,
10,
1,
1,
1
};
int main()
{
unsigned long long int i, j, res;
unsigned char inbuff[2500000];
unsigned char *in = inbuff;
char outbuff[2500000];
char *out = outbuff;
int c = 0;
while(1) {
i = j = 0;
inbuff[fread(inbuff, 1, 2500000, stdin)] = '\0';
do {
c = *(in++);
} while(c != '\0' && !(c >= '0' && c <= '9'));
if(c == '\0') {
*(--out) = '\0';
puts(outbuff);
return 0;
}
do {
i = 10 * i + (c - '0');
c = *(in++);
} while(c >= '0' && c <= '9');
do {
c = *(in++);
} while(!(c >= '0' && c <= '9'));
do {
j = 10 * j + (c - '0');
c = *(in++);
} while(c >= '0' && c <= '9');
if(i > j)
res = i-j;
else
res = j-i;
if(res == 0) {
*(out++) = '0';
} else {
unsigned long long divisor = divisors[__builtin_clzll(res)-31];
if(res < divisor) {
divisor /= 10;
}
while(divisor != 0) {
unsigned long long digit = res / divisor;
*(out++) = digit + '0';
res -= divisor * digit;
divisor /= 10;
}
}
*(out++) = '\n';
}
}
0.02 .