How to optimize the printing of the difference between the larger and smaller of two integers?

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]; /* To be certain there no overflow here */
  unsigned char *in = inbuff;
  char outbuff[2500000]; /* To be certain there no overflow here */
  char *out = outbuff;

  int c = 0;

  while(1) {
    i = j = 0;

    inbuff[fread(inbuff, 1, 2500000, stdin)] = '\0';

    /* Skip whitespace before first number and check if end of input */
    do {
      c = *(in++);
    } while(c != '\0' && !(c >= '0' && c <= '9'));

    /* If end of input, print answer and return */
    if(c == '\0') {
      *(--out) = '\0';
      puts(outbuff);
      return 0;
    }

    /* Read first integer */
    do {
      i = 10 * i + (c - '0');
      c = *(in++);
    } while(c >= '0' && c <= '9');

    /* Skip whitespace between first and second integer */
    do {
      c = *(in++);
    } while(!(c >= '0' && c <= '9'));

    /* Read second integer */
    do {
      j = 10 * j + (c - '0');
      c = *(in++);
    } while(c >= '0' && c <= '9');

    if(i > j)
      res = i-j;
    else
      res = j-i;



    /* Buffer answer */
    if(res == 0) {
      *(out++) = '0';
    } else {
      unsigned long long divisor = divisors[__builtin_clzll(res)-31];
      /* Skip trailing 0 */
      if(res < divisor) {
        divisor /= 10;
      }
      /* Buffer digits */
      while(divisor != 0) {
        unsigned long long digit = res / divisor;
        *(out++) = digit + '0';
        res -= divisor * digit;
        divisor /= 10;
      }
    }
    *(out++) = '\n';
  }
}   

0.02 .

+4
4

-. ( ). , , .

sscanf / .

IO , , . (). , 10 .

+2

.

#include <iostream>
#include <string>

using namespace std;

int main()
{
   unsigned long long i, j;
   string outv;   
   while(cin >> i >> j) {
     asm("movq %0, %%rax;"
         "movq %1, %%rdx;"  
         "subq %%rax, %%rdx;"
         "jns .L10;"        
         "notq %%rdx;"      
         "addq $0b1, %%rdx;"
         ".L10: movq %%rdx, %0": : "g"(i), "g"(j) );       
     string str = to_string(i);
     outv += str + "\n";     
    }
    cout << outv;   
 }
+1

printf - . , . , %d. (BTW, std::cout << - ).

, , char[] puts. puts , , printf.

+1

:

This solution, which runs in less than 0.001 seconds, is based on the UVa Online Judge submission http://ideone.com/ca8sDu which was resolved at http://uhunt.felix-halim.net/id/779215 ; However, this solution is shortened and modified by #include

#define pll(n) printf("%lld ",(n))
#define plln(n) printf("%lld\n",(n))
typedef long long ll;

#if defined(_WINDOWS) // On Windows GCC, use the slow thread safe version
inline int getchar_unlocked() {
    return getchar();
}
#elif defined  (_MSC_VER)// On   Visual Studio
inline int getchar_unlocked(){
    return _getchar_nolock(); // use Microsoft Thread unsafe version
}
#endif 

inline int  scn( ll & n){
     n = 0;
     int  c = getchar_unlocked(),t=0;
    if (c == EOF) 
        return 0;
    while(c < '0' || c > '9') {
        if(c==45)
            t=1;
        c = getchar_unlocked(); 
    }
    while(c >= '0' && c <= '9'){
        n = n *10+ c - '0';       
        c = getchar_unlocked();
    }
    if(t!=0)
        n *=-1;
    return 1;
}

int main(){
    ll n, m;
    while(scn(n)+scn(m)==2){
        if (n>m)
            plln(n - m);
        else
            plln(m - n);
    }
    return 0;
}
0
source

Source: https://habr.com/ru/post/1677677/


All Articles