Tuesday 14 July 2015

SPOJ : CANDY 1 solution

EXPLANATION :

This one has a simple way to solve just by using averages.
Calculate the sum of all candies, now if it can be completely 
divided by total no. of packets , i.e remainder of sum/total no. 
of packets , candies can be equally divided into each packet.
Now get the average , and find out the moves.. 

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

int main()
{

    int n,t,sum=0,rem=0,avg=0,total=0;
   
    while(1)
    {
     scanf("%d",&n); // n is no. of packets
     sum=0;
     if(n==-1)   //to end inputting
        break;
     int candy[n];//array for no. of candy in each packets
     for(t=0;t<n;t++)
     {
         scanf("%d",&candy[t]);
         sum+=candy[t];
     }

     rem=sum%n;
     avg=sum/n;
     if(rem != 0)//means it cannot be divided equally
     {
         printf("-1\n");
     }
     else    // else get the total no. of moves by using averages
        {
          total=0;
          for(t=0;t<n;t++)
          {
              if(candy[t]>avg)
                total=total+(candy[t]-avg);
          }
          printf("%d\n",total);
    }//else closed
    }
    return 0;
}

Saturday 11 July 2015

SPOJ: JULKA solution

#include <iostream>
#include <string.h>
#include <stdio.h>

#define maxi 110
using namespace std;
char total[maxi],  diff[maxi], klaudia[maxi], natalia[maxi];

//function  to divide by 2
void divideByTwo(char tem[],int l,char name[])
{
    int i,j,f,a,b;
    for(i=l-1,a=j=f=0;i>=0;i--)
    {b=(a*10+tem[i]-'0')/2;
     a=(a*10+tem[i]-'0')%2;
     if(b)
            f=1;
     if(f)
        name[j++]=b+'0';
    }
    if(!j)
        j++;
    name[j]=0;
}

void calculate()
{
    //add (total+diff)
    char temp[maxi];
    int lenTotal=strlen(total);
    int lenDiff=strlen(diff);
    int i=0,j=0,k=0,a=0,b=0,c=0;
    for(i=lenTotal-1,j=lenDiff-1; i>=0||j>=0||c ;i--,j--,k++)
    {
        a= i>=0?total[i]-'0' : 0;       //get each number of total
        b= j>=0?diff[j]-'0' : 0;        //get each number of diff
        temp[k]=(a+b+c)%10 +'0';   // add both no.
        c=(a+b+c)/10;                       //carry generated
    }
    temp[k]=0;
    strcpy(klaudia,"0");
    divideByTwo(temp,k,klaudia);          // now divide (total+diff)/2

    ///subtract  (total- diff)

    for(i=lenTotal-1,j=lenDiff-1,k=c=0;i>=0;i--,j--,k++)
    {
        a=total[i]-'0';            //get each no. of total
        b=j>=0?diff[j]-'0':0;  //get each no.of diff
                                            //subtracting
        if(a<b+c)              
        {
           temp[k]=(10+a-b-c)+'0';
           c=1;
        }
        else
        {
            temp[k]=a-b-c + '0';
            c=0;
        }
    }
    temp[k]=0;

    strcpy(natalia,"0");
    divideByTwo(temp,k,natalia);      //   now divide (total-diff)/2
}




int main()
{
    int i=0;
    for(i=0;i<10;i++)
    {
        scanf("%s %s",total,diff);
        calculate();
        printf("%s\n%s\n",klaudia,natalia);
    }

    return 0;
}

SPOJ : MARBLES Solution

Question  MARBLES - Marbles Explaination.... Fill n spaces , with k colors. First select k spaces with k-different color.  Now we are l...