Cod sursă (job #111672)

Utilizator avatar dummy contdezactivat dummy IP ascuns
Problemă Sqrt (clasele 9-10) Compilator cpp | 4,21 kb
Rundă Arhiva de probleme Status evaluat
Dată 28 ian. 2015 21:38:51 Scor 100
///Iisuse miluieste - ma
#include <fstream>
#include <cmath>
#define GOD 3000

using namespace std ;

char c ;
short Number [ GOD ] ;
short Res    [ GOD / 2 ] ;
short Double [ GOD ] ;
short Probe  [ GOD ] ;
short k , n ;

short Detectzero( short a[ ] , int poza )
{
    int i = poza ;
    bool gasit = 0 ;
    while( ! gasit )
         {
          if( a[ i ] )
             gasit = 1 ;
          i ++ ;
         }
    return i - 1 ;
}

void Inmultire ( short a[ ] , int poza , int la , int cif , short x[ ] )
{
    int i = la ;
    int j = GOD - 2 ;
    int trans = 0 ;
    int prod ;

    while( i > poza )
         {
           prod = a[ i ] * cif + trans ;
           trans = prod / 10 ;
           x[ j ] = ( prod - 10 * trans ) ;
           i -- ;
           j -- ;
         }
    if( i == poza )
       {
         prod = a[ i ] * cif + trans  ;
         trans = prod / 10 ;
         x[ j ] = ( prod - 10 * trans ) ;
         x[ j - 1 ] = trans ;
       }
}

bool Comparare (  short a[ ] , int poza , int la , short b[ ] , int pozb , int lb )
{
    int i = poza ;
    int j = pozb ;
    bool gasit = 0 ;

    if( la - poza > lb - pozb  )
        return 1 ;
    if( la - poza < lb - pozb  )
        return 0 ;
    if( la - poza == lb - pozb )
      {
        while( !gasit && i <= la && j <= lb )
             {
               if( a[ i ] < b[ j ] )
                  {
                    gasit = 1 ;
                    return 0 ;
                  }
               if( a[ i ] > b[ j ] )
                  {
                   gasit = 1 ;
                   return  1 ;
                  }
               i ++ ;
               j ++ ;
             }
       if( i == la + 1 && j == lb + 1 )
          return  1 ;
       }

}

void Diferenta ( short a[ ] , int poza , int la , short b[ ] , int pozb , int lb )
{
    int diff ;
    int imp = 0 ;
    int i = la ;
    int j = lb ;

    while( j >= pozb )
         {
           diff = a[ i ] - b[ j ] - imp ;
           a[ i ] = diff ;

           if ( diff < 0 )
              {
                imp = 1 ;
                a[ i ] = diff + 10 ;
              }
           if( diff >= 0 )
              imp = 0 ;
           j -- ;
           i -- ;
        }
     if( imp )
        a[ i ] = a[ i ] - imp ;
     ///Slava Tie Dumnezeule!

}

int main( )
{
    int i = 0 ;
    int j ;
    bool gasit ;

    ifstream  fin( "sqrt.in"  ) ;
    ofstream fout( "sqrt.out" ) ;

    fin>> c ;
    while( !fin.eof( ) )
         {
            Number[ i ] = c - '0' ;
            i ++ ;
            fin>>c ;
         }

    k = 0 ;
    if( i % 2 == 1 )
       {
        Res[ k ] = ( int )sqrt( Number[ 0 ] ) ;
        Number[ k ] = Number[ k ] - Res[ k ] * Res[ k ] ;
        j = 1 ;
       }
    if( i % 2 == 0 )
      {
        k = Number[ 0 ] * 10 + Number[ 1 ] ;
        Res[ 0 ] = ( int ) sqrt( k ) ;
        n = Res[ 0 ] * Res[ 0 ] ;
        Number[ 0 ] = ( ( k - n ) / 10 ) ;
        Number[ 1 ] = ( k - n ) - Number[ 0 ] * 10  ;
        j = 2 ;
      }

    fout<< Res[ 0 ] ;
    k = 1 ;

    while( j < i )
         {

           Inmultire( Res , 0 , k - 1  , 2 , Double ) ;

           gasit = 1 ;
           n = 0 ;
           while( n < 10 && gasit )
              {
               n = n + 1 ;
               Double[ GOD - 1 ] = n ;
               Inmultire ( Double , Detectzero( Double , GOD - j - 2 ) , GOD - 1 , n , Probe ) ;
               gasit = Comparare ( Number , Detectzero ( Number , 0 ) , j + 1 , Probe , Detectzero ( Probe , GOD - j - 4 ) , GOD - 2 ) ;
              }
           if( n == 1 )
             {
               fout<< 0 ;
               Res[ k ] = 0 ;
             }
           else
             {
              fout<< n - 1 ;
              Res[ k ] = n - 1 ;
              Double[ GOD - 1 ] = n - 1 ;
              Inmultire ( Double , Detectzero( Double , GOD - j - 2 ) , GOD - 1 , n - 1 , Probe ) ;
              Diferenta ( Number , Detectzero( Number , 0 ) , j + 1 , Probe , Detectzero( Probe , GOD - j - 4 ) , GOD - 2 ) ;
             }
           j = j + 2 ;
           k ++ ;
         }

    ///fout<<"\nIisuse miluieste - ma " ;

    return 0 ;
}