ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1001. Reverse Root

Why wrong answer
Posted by tyrone_tgdriver 18 May 2015 23:41
#include<iostream>
#include<cmath>
#define MAX 32768 //input stream 256KB
#define SWAP( a,b ) { a=a^b; b=a^b; a=a^b; }
using namespace std;

void printSqrt( char * );
int isSpace( int );
void reverseString( char * );
void findSqrt( char *, char * );

int main()
{

    // Reads the input
    char input_stream[MAX];
    for( int i=0; i<MAX; i++)
    {
        std::cin.get( input_stream[i] );
        if( std::cin.eof() )
            break;
    }

    // Find square root for all integers
    printSqrt( input_stream );

    return 0;
}

void printSqrt( char *inp )
{
    // reverse the string
        reverseString( inp );

    // get each string
    char *start=inp;
    char *end=inp;
    for(;*inp!='\0';inp++)
    {
        if(isSpace(*inp))
        {
            findSqrt( start, end );
            start=inp+1;
            end=inp+1;
        }
        else
        {
            end=inp;
        }
    }
    findSqrt( start, end );
}

void findSqrt( char *start, char *end )
{
    if(isSpace(*start) || *start=='\0')
        return;

    int strln = (end-start+1);
    char *p = new char[strln*sizeof(char)];
    int i;
    for(i=0;i<strln;i++)
        *(p+i)=*(start+i);
    *(p+i)='\0';
    reverseString( p );

    // convert char array to number
    unsigned long long num=0;
    for(int j=0;*(p+j)!='\0';j++)
    {
        num*=10;
        num+=*(p+j)-'0';
    }

    std::cout.precision(4);
    std::cout << std::fixed << sqrt(num) << std::endl;
    delete p;
}

int isSpace( int c )
{
    switch ( c )
    {
        case ' ':
        case '\n':
        case '\t':
        case '\v':
        case '\f':
        case '\r':
            return 1;
    }
    return 0;
}

void reverseString( char *strg )
{
    int i = 0;
    while( *(strg+i) != '\0' )
        i++;
    i--;
    for( int j=0,k=i; j<k; j++,k-- )
        SWAP( *(strg+j), *(strg+k) );
}