Uva 11878 - Homework Checker

/*
    Uva 11878 - Homework Checker
    Verdict : Accepted
    Time : 0.000
    ==> Take a getline string and count the correct counting of plus and subtraction till the more spaces.
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <sstream>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#define sf scanf
#define pf printf
#define sfint scanf ("%d %d",&a,&b)
#define sfl scanf ("%ld %ld",&a,&b)
#define sfll scanf ("%lld %lld",&a,&b)
#define sfd scanf ("%lf %lf",&a,&b)
#define sff scanf ("%f %f",&a,&b)
#define loop (i,n) for (i=0;i<n;i++)
#define LL long long
#define L long
#define nl puts("")
#define N 10000
#define MOD 10000000007
#define pb push_back
#define pi acos(-1.0)
#define sz size()
#define gc getchar ()
#define ps push
#define clr clear
#define bn begin()
#define ed end()

using namespace std;

int main()
{
    string s;
    int cnt= 0,left,right,equ,d,b;

    while (getline(cin,s))
    {
        bool sp = false , p = false , sub = false;
        int r1=0;

        for (int i=0; i<s.size() ;i++)
        {
            if (s[i] == ' ') sp = true;

            if (s[i] == '=')
            {
                b = i;
                break;
            }

            if (s[i] == '+' or s[i]=='-')
            {
                if (s[i] == '+') p = true;
                if (s[i] == '-') sub = true;

                d = i; //cout << d << " ";
                int r=0;

                for (int j=0;j<d;j++)
                {
                    r = (s[j]-'0') + (r*10);
                }

                left = r; r=0; //cout << left << " ";

                for (int j=d+1; s[j]!= '=' ;j++)
                {
                    r = (s[j]-'0') + (r*10);
                }

                right = r; r=0; //cout << right << " ";
            }
        }

        for (int i=b+1;i<s.size();i++)
        {
            r1 = (s[i]-'0') + (r1*10);
        }

        equ = r1; //cout << equ << " ";

        if (sp) break;
        else
        {
            if (p)
            {
                if (left+right == equ)
                {
                    cnt++;
                }
            }

            else if (sub)
            {
                if (abs(left-right) == equ)
                {
                    cnt++;
                }
            }

            //cout << cnt << " ";

            sp = false;
            p = false;
            sub = false;
            left = 0;
            right=0;
        }
    }

    pf ("%d\n",cnt);

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LeetCode Palindrome Number

Hacker Rank The Power Sum