FallAndLearn
FallAndLearn

Reputation: 4135

SYNC13C SPOJ Wrong answer

Link to challenge

Ramesh and Suresh get a box full of five stars on lottery each. Since both the boxes need not have the same number of chocolates, they decide to play a game. The winner gets to have both the boxes of chocolates. They play alternatively and Suresh starts the game. Given the number of chocolates in both the boxes, let them be c1 and c2, the player takes either c1 or c2 number of chocolates and divide the remaining box of chocolates to two boxes (these two boxes need not have the same number of chocolates). The player who cannot make such a move loses. Input

First line of input contains a number T(1<=T<=1000), the number of test cases. Then follows T lines each containing two space separated integers c1 and c2

(1<=c1<=c2<=10000).

Output For each test case print "Ramesh" or "Suresh" depending on who is the winner.

Input: 2 3 1 4 5

Output: Ramesh Suresh

Here is my attempt, which is giving me the wrong answer. Give me some more test cases, too.

#include<stdio.h>
int main()
{
    int t,c1,c2,max,count,min;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&c1,&c2);
        min=c1<c2?c1:c2;  
        max=c1>c2?c1:c2;
        if(max%2!=0 && min%2!=0) 
            printf("Ramesh\n");
        else if(min%2==0 && max%2!=0)
            printf("Suresh\n");
        else if(max%2==0 && min%2!=0)
            printf("Ramesh\n");  
        else printf("Suresh\n");
    }
    return 0; 
}

Upvotes: 3

Views: 278

Answers (2)

user3143781
user3143781

Reputation:

#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
       int a,b;
       scanf("%d%d",&a,&b);
       if(a%2==1 && b%2==1)
           printf("Ramesh\n");
       else
           printf("Suresh\n");
    }    
    return 0;
}

Upvotes: 0

nitish712
nitish712

Reputation: 19764

The code is much simpler than that. First, let me explain the algorithm.

Let W be an array where,

W[i] = 1 if the user wins by choosing to split the box of i chocolates and 0 if he looses.

Lets construct this array and we will be getting a pattern.

W[1] = 0, since one can't split the box of one chocolate.

For all i>1, we have:

W[i] = 1 if there exists integers a and b such that a+b=i and W[a]=W[b]=0 , 0 otherwise.

The above statement implies that, for a user to win by choosing the i chocolate box, he needs make sure that, his opponent looses no matter what box he chooses further. His opponent loosing implies that W[a]=W[b]=0 and a+b=i.

If we try to fill up this array we get,

W : 1 2 3 4 5 6 7...

val: 0 1 0 1 0 1 0...

This means if one the given integers is even, then suresh is going to win. If both of them are odd, it means ramesh will win.

Hope I am clear.

Upvotes: 3

Related Questions