Monday 23 November 2015

SPOJ SOLUTION : Simple Arithmetic 2 (code: arith2)

/*
Problem:Simple Arithmetics II
Source: Spoj
Type: Ad-hoc
Language: C++
*/

#include <iostream>

using namespace std;

int main(void)
{
    int tc;
    int flag=1;
    long long num1,num2,ans,count;
    char op;

    cin>>tc;
   
     while(tc--)
    {
        flag=1;
        count=0;
        while(flag) //flag checks if '=' is encountered
        {
        count++;
        if(count==1) //count determines if the expression is starting or we are in  middle of the expression to be solved
        {
        //get the expression
        cin>>num1;
        }
        else
        num1=ans;
       
        op=' '; //as expression contains many spaces this is where we avoid spaces
        while(op == ' ')
            cin>>op;
        if(op=='=')
        {
            cout<<ans<<"\n";
            flag=0;
        }
        else
            cin>>num2;
       
        switch(op)
        {
            case  '+' : ans=num1+num2;
                    break;
            case  '-' : ans=num1-num2;
                    break;
            case  '*' : ans=num1*num2;
                    break;
            case  '/' : ans=num1/num2;
                    break;
            default : break;
        }
       
       
        }//while
    }
        return 0;
       
}   

Tuesday 4 August 2015

SPOJ SOLUTION - ALIEN AT THE TRAIN

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

typedef unsigned long long ull;

int main()
{
    ull at,bt,tc,sum,i,j,count,max_count=0,cost;

    //get the number of test cases
    cin>>tc;
    while(tc--)
    {
        //get the at and bt
        cin>>at>>bt;
        ull arr[at];
        memset(arr,0,sizeof(arr));
        count=0;
        max_count=0;
        for(i=0;i<at;i++)
            cin>>arr[i];
        cost=100005;
        int start=0;
        sum=0;
        for(i=0;i<at;i++)
        {
           
            sum+=arr[i];
            count++;
            if(max_count<count&&sum<=bt||max_count==count&&sum<cost)
            {
                cost=sum;
                max_count=count;
            }
           
            while(sum>bt)
            {
                sum-=arr[start];
                count--;
                start++;
            }

            if(max_count<count&&sum<=bt||max_count==count&&sum<cost)
            {
                cost=sum;
                max_count=count;
            }
           
        }
        cout<<cost<<" "<<max_count<<"\n";
    }
    return 0;
}

Sunday 22 February 2015

To print all possible binary strings of size 'n'. Where 'n' to be taken as input (recursive approach)

//author: Pranshul Rastogi
//occupation:Student
//nationality:Indian

//Problem:- To print all possible binary strings of size 'n'. Where 'n' to be taken as input

//here maximum value of n can be 1000, one can change according to his/her need.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>


char str[1000];//array that contains string
//binary(int) prints all possible binary strings of size given using recursion

void binary(int n)
{
    if(n<0)
        fprintf(stdout,"%s\n",str);
    else
    {
        str[n]='0';
        binary(n-1);
        str[n]='1';
        binary(n-1);
    }
}
   
int main()
{
    int n;
    fprintf(stdout,"Enter the value of n --> ");//take the value of n
    fscanf(stdin,"%d",&n);
    assert(n<1000);
    binary(n-1);
    return 0;
}



/*
Output:- Enter the value of n--> 2
00
10
01
11
//you can copy and paste it on your respective compiler but try to solve it by //yourself first. For any query, please leave comment!! Thank you .
//Hope you find it useful
*/