Advertisement
Guest User

Untitled

a guest
Jan 10th, 2017
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <cmath>
  2. #include <stdio.h>
  3.  
  4. //Avoid io blocking and 64 bit math
  5. //32 bit mod and div  - OK
  6. inline void printDoubleApprox(double In)
  7. {
  8.     int beforepoint = (int)(In);
  9.     int afterpoint = (int)((In-beforepoint)* 10000);
  10.  
  11.     char buf[10], *p = buf;
  12.     do
  13.     {
  14.         *p++ = '0' + beforepoint % 10;
  15.         beforepoint /= 10;
  16.     }
  17.     while (beforepoint);
  18.  
  19.     do
  20.     {
  21.         _putchar_nolock(*--p);
  22.     }
  23.     while (p > buf);
  24.  
  25.     _putchar_nolock('.');
  26.     _putchar_nolock('0' + (afterpoint%10000)/1000);
  27.     _putchar_nolock('0' + (afterpoint%1000)/100);
  28.     _putchar_nolock('0' + (afterpoint%100)/10);
  29.     _putchar_nolock('0' + (afterpoint%10));
  30.     _putchar_nolock('\n');
  31. }
  32.  
  33. int main()
  34. {
  35.     //char * inpt = new char[256*1024];
  36.     //Stack allock = faster
  37.     char inpt[256*1024];
  38.     int indx=0;
  39.     indx = fread(inpt,1,256*1024,stdin);
  40.     bool valIn = false;
  41.     long long mult = 1;
  42.     long long val = 0;
  43.     while(indx--)
  44.     {
  45.         if(inpt[indx] >= '0' && inpt[indx] <='9') //digit
  46.         {
  47.             if(!valIn)
  48.             {
  49.                 val = 0;
  50.                 mult = 1;
  51.                 valIn=true;
  52.             }
  53.             val = val + mult*(inpt[indx]-'0');
  54.             //mult *= 10;
  55.             mult = (mult<<3) + (mult<<1);
  56.         }
  57.         else //invalid char, outputting
  58.         {
  59.             if(valIn)
  60.             {
  61.                 valIn = false;
  62.                 double v = sqrt((double)val);
  63.                 printDoubleApprox(v);
  64.             }
  65.         }
  66.     }
  67.     if(valIn)
  68.     {
  69.         double v = sqrt((double)val);
  70.         printDoubleApprox(v);
  71.     }
  72.  
  73.     //delete[] inpt;
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement