Search Here

Friday, September 18, 2015

UVa - 12930 - Bigger Or Smaller Solution [ C & C++ Code with Algorithm ]

=>Try Yourself First. For help Scroll Down .

Hey. How are you . Hope everything Going Good with Problem Solving. Whatever Today I would like to Provide you the Way/Procedure To solve a Very Easy String Manipulation problem .Although My code is in C++ Format its easily Readable & Understandable for C user . 

Clearance

The problem Says to find whether the First Number is Greater/ Less than or Equal to the Second . But the number can be 100 Digit . So, you can understand that you have to take it as a String . and Compare Character by Character.  Many solvers can solve many ways . I will explain mine.
1) What I have done here is I Checked where the Point ( '.' ) is both of the String ( Floating Point Numbers ) ? Let the index of the -> First String's point ('.') is ' first_point' & -> Second String's Point ('.') is 'second_point'. 
2) If ' first_point ' is Greater than ' second_point ' then print " Bigger ". If Smaller then print " Smaller ". 
3) If Equal then Check the Character by Character whether Characters are Equal or Not. If the Check Any One Characters of First String is Greater than Second String . If it is then flag it as ' Bigger '. If not then Flag it 'Smaller' . The Flag variable Must be declared as Equal First .
Way to Get AC Verdict :
1.) Careful about Case Number & Spacing & Spelling . And take your decision Carefully while Comparing Characters .
=========================== Hope You Solved It Already. ===========================
Code :

 #include<bits/stdc++.h>

using namespace std;

int main()
{
    char a[105], b[105];
    int caseno = 0;
    while(scanf("%s %s", a, b)==2)
    {
        int asz = strlen(a), bsz = strlen(b);
        int point_one, point_two;
        bool found_one = false, found_two = false;
        for(int i=0;  ; i++)
        {
            if(found_one && found_two) break;
            if(a[i]=='.' && !found_one)
                { found_one = true; point_one = i+1; }
            if(b[i]=='.' && !found_two)
                { found_two = true; point_two = i+1; }
        }
        printf("Case %d: ", ++caseno);
        if(point_one>point_two) printf("Bigger\n");
        else if(point_one<point_two) printf("Smaller\n");
        else
        {
            char big = 'e';
            if(asz>bsz)
            {
                for(int i=0; i<asz; i++)
                {
                    if(a[i]=='.' && b[i]=='.') continue;
                    if(i>bsz-1)
                    {
                        if(a[i]!='0')
                        {
                            big = 'y'; break;
                        }
                        continue;
                    }
                    if(a[i]!=b[i])
                    {
                        if((a[i]-'0')>(b[i]-'0')) big = 'y';
                        else big = 'n';
                        break;
                    }
                }
            }
            else
            {
                big ='e';
                for(int i=0; i<bsz; i++)
                {
                    if(a[i]=='.' && b[i]=='.') continue;
                    if(i>asz-1)
                    {
                        if(b[i]!='0')
                        {
                            big = 'n'; break;
                        }
                        continue;
                    }
                    if(a[i]!=b[i])
                    {
                        if((a[i]-'0')>(b[i]-'0')) big = 'y';
                        else big = 'n';
                        break;
                    }
                }
            }
            if(big=='y') printf("Bigger\n");
            else if(big=='n') printf("Smaller\n");
            else if(big=='e') printf("Same\n");
        }
    }
    return 0;
}


Critical Cases : 

Try this Cases for your Code's Correctness . Link : http://pastebin.com/8dq4cN9g

=>Question or Need Help ?? Leave a Comment .

No comments:

Post a Comment