Web Toolbar by Wibiya

Pages

Tuesday, October 25, 2011

Write an algorithm to find the number of ways of placing 3 balls in 3 buckets. Bucket 1 can hold 2 balls, bucket 2 can hold 3 balls and bucket 3 can hold 2 balls..

Solution# 1: We can try all the permutations using the following code,
#include 
#include 

int main()
{
    int A;
    int B;
    int C;
    int count=0;

    for(A=0;A<=2;A++)
    {
        for(B=0;B<=3;B++)
        {
            for(C=0;C<=2;C++)
            {
                    if((A+B+C)==3)
                    {
                        printf("%d%d%d\n", A, B, C);
                        count++;
                    }
            }
        }
    }
    printf("count = %d\n", count);
    return 0;
}

No comments: