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;
}
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.
#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;
}
Related
I'm currently thinking about a code question about the C language, its a game called Blackjack, and here is the original question:
In practice, one need to play the game a large number of times to get an accurate expected
value. Thus, each row of the table should be the results of at least 100,000 experiments. For example, for a particular target points, say 10 points, two cards are drawn first. If the sum of these two cards exceeds 10 points then this experiment is a failure. If the sum is exactly 10 points, then it is a success. If it is less than 10 points, then another card is drawn. If case of neither a failure (more than 10 points) or a success (exactly 10 points), cards are continuously drawn until a conclusive results is obtained. After 100,000 experiments, the probability of getting 10 points should be printed together with the average number of cards of getting 10 points (the third column of the table).
Below is my current code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int r1,r2,count,sum,cardsadd,k;
int aftersum=sum+k;
unsigned int total,cardsum;
float percent,cards;
printf("Points Probability #Cards\n");
for (int points=4; points<=21; points++){
count = 0;
total = 0;
cardsum = 0;
do{
r1 = rand()%13 + 1;
r2 = rand()%13 + 1;
if(r1>10) r1=10;
if(r2>10) r2=10;
sum = r1+r2;
if(r1==1 && r2==1) sum=12;
else if ((r1==1 || r2==1) && r1!=r2) sum+=10;
count++;
cardsadd=0;
if(sum==points){
total++;
cardsum+=2;
}
else if(sum<points){
while(sum<points){
do{
cardsadd+=1;
k = rand()%13 + 1;
if(k>10) k=10;
else if(k==1){
if(sum<=10) k=11;
}
}while(aftersum>points);
sum+=k;
}
total+=1;
cardsum+=aftersum;
}
}while(count<100000);
percent = (float)total/1000;
cards = (float)cardsum/100000;
printf(" %2d %5.2lf%% ",points,percent);
printf("%.2lf\n",cards);
}
return 0;
}
In my code, variable count is the times needed to execute for each cards (4 to 21), total is the correct times when sum of the cards number is successfully equal to the points we want in the beginning (for loop). And cardsum is the total cards we need in 100000 tests, cardsadd is used when the first two cards drawn is less than the point we want, then we will keep drawing until sum of the point is equal to the points in the beginning.
I don't have the correct answer yet but I know my code is surely wrong, as I can clearly see that the average cards we need to get 4 points is not 2.00.
Hope someone can tell me how I should correct my code to get the answer. If anything is not clearly narrated, I will give a more complete explanation of the parts. Thanks for helping.
With an ace you have 2 possibles scores (the soft and the hard);
You cannot compare "points" with only score in case you have an ace because for example with ace and 5 you can have 6 or 16;
You need to modify your program to take this both scores in consideration (in case of an ace);
Problem: Ekka and his friend Dokka decided to buy a cake. They both love cakes and that's why they want to share the cake after buying it. As the name suggested that Ekka is very fond of odd numbers and Dokka is very fond of even numbers, they want to divide the cake such that Ekka gets a share of N square centimeters and Dokka gets a share of M square centimeters where N is odd and M is even. Both N and M are positive integers.
They want to divide the cake such that N * M = W, where W is the dashing factor set by them. Now you know their dashing factor, you have to find whether they can buy the desired cake or not.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer W (2 ≤ W < 2^63). And W will not be a power of 2.
Output
For each case, print the case number first. After that print "Impossible" if they can't buy their desired cake. If they can buy such a cake, you have to print N and M. If there are multiple solutions, then print the result where M is as small as possible
Time Limit: 2 second(s)
I've tried on sample inputs and got same result as the output.
Sample Input
3
10
5
12
Output for Sample Input
Case 1: 5 2
Case 2: Impossible
Case 3: 3 4
My code:
#include<stdio.h>
#include<math.h>
int main()
{
int t,k;
scanf("%d",&t);
for(k=1;k<=t;k++)
{
int a,i,j=1,c;
scanf("%d",&a);
for(i=2;i<sqrt(a)+2;i++)
{
if(a%i==0 && i%2!=0)
c=i;
}
int b=a/c;
if(b%2==0 && b*c==a)
printf("Case %d: %d %d\n",k,c,b);
else
printf("Case %d: Impossible\n",k);
}
return 0;
}
The Light OJ given TLE to my solution.
Your solution is giving TLE.
May be you need to optimise it a little.
One thing I am suggesting is:
If the number W is not divisible by 2, print "Impossible".
If the number W is divisible by 2, keep dividing the number by 2, until it becomes an odd number.
So, the odd number generated will be one share and W/odd_number will be other share.
So I have this problem.
Input # of rooms: 4
room1:6
room2:4
room3:7
room4:3
(if I type 5 in "Input # of rooms" there would also be room5)
Odd: 7 3
Even: 6 4
I have to display the odd and even numbers, so I came up with this code:
System.out.print("Input # of rooms: ");
int rms=Integer.parseInt(io.readLine());
int[] array=new int[rms];
int a=0;
int b=1;
do {
System.out.print("room "+b+":");
array[a] = Integer.parseInt(io.readLine());
a++;
b++;
} while (a<rms);
I don't know how to display which are Odd numbers and which are Even numbers?
you want to find the remainder or modulus when the param is divided by 2.
3 % 2 = 1 so odd
4 % 2 = 2 so even
if(param % 2 == 1){
Print odd number
}else{
Print even number
}
Should get you started
The use of the modulo operator (%) will be invaluable here - it performs integer division and returns the remainder of the quotient - kind of like short division.
The rules for determining the type of number are simple:
If the number is even, it is divisible by 2.
Otherwise, it is odd.
As for the printing part: I would recommend accumulating the values in two separate StringBuffers or Strings if you prefer, adding a space between when we get another of the type of value we want. Then, we can print it out pretty after we're done iterating through the array.
One last thing - you should only need one loop - preferably a for loop, since you know exactly how many elements you're going to iterate over. You can use the above rules for modulus to determine which number gets appended to which variable.
I'm having trouble articulating what the function is supposed to do, so I think I will just show you guys an example. Say my program opens and scans a text file, which contains the following:
"The cat chased after the rooster to no avail."
Basically the function I'm trying to write is supposed to print out how many 1 letter words there are (if any), how many 2 letter words there are, how many 3 letter words, etc.
"
Length Count
2 2
3 3
5 2
6 1
7 1
"
Here's my attempt:
int word_length(FILE *fp, char file[80], int count)//count is how many total words there are; I already found this in main()
{
printf("Length\n");
int i = 0, j = 0;
while(j < count)
{
for(i = 0; i < count; i++)
{
if(strlen(file[i] = i)
printf("%d\n", i);
}//I intended for the for loop to print the lengths
++i;
printf("Count\n");
while()//How do you print the counts in this case?
}
}
I think the way I set up the loops causes words of the same length to be printed twice...so it'd look something like this, which is wrong. So how should I set up the loops?
"Length Count
2 1
2 2
"
This sounds like homework, so I will not write code for you, but will give you some clues.
To hold several values you will need array. Element with index i will contain counter for words with length i.
Find a way to identify boundaries of words (space, period, beginning of line etc.). Then count number of characters between boundaries.
Increase relevant counter (see tip 1). Repeat.
Some details. You actually want to map one thing to another: length of word to number of such words. For mapping there is special data type, called usually hash(table) or dictionary. But in your case array can perfectly work as a map because you keys are uniform and continues (1,2 ... to some maximum word length).
You can't use a single int to count all of that. You need an array and then in it at position 0 you keep track of how many 1 letter words, at position 1 you accumulate 2 letter words and so on.
This was a contest Q:
There are N numbers a[0],a[1]..a[N - 1]. Initally all are 0. You have to perform two types of operations :
Increase the numbers between indices A and B by 1. This is represented by the command "0 A B"
Answer how many numbers between indices A and B are divisible by 3. This is represented by the command "1 A B".
Input : The first line contains two integers, N and Q.
Each of the next Q lines are either of the form "0 A B" or "1 A B" as mentioned above.
Output : Output 1 line for each of the queries of the form "1 A B" containing the required answer for the corresponding query.
Sample Input :
4 7 1 0 3 0 1 2 0 1 3 1
0 0 0 0 3 1 3 3 1 0 3
Sample Output :
4 1 0 2
Constraints :
1 <= N <= 100000 1 <= Q <= 100000 0 <= A <= B <= N - 1
I have no idea how to solve this. can you help please?
The time limit is 1 second. I tried brute force and i also tried saving number of divisors of 3 coming before ith element for each i.
here's my C code:
#include <stdio.h>
int nums[100*1000+20];
int d[100*1000+20];
int e[100*1000+20];
int dah[100*1000+20];
int main()
{
int n,q;
scanf("%d%d",&n,&q);
int h;
for(h=0;h<n;h++)
{d[h/100]++; e[h/1000]++; dah[h/10]++;}
int test;
for(test=0;test<q;test++)
{
int op,start,end;
scanf("%d%d%d",&op,&start,&end);
if(0==op)
{
int x;
for(x=start;x<=end;x++)
{
nums[x]++;
nums[x]%=3;
if(nums[x]==0)
{
d[x/100]++;
e[x/1000]++;
dah[x/10]++;
}
else if(nums[x]==1)
{
d[x/100]--;
e[x/1000]--;
dah[x/10]--;
}
}
}
else if(1==op)
{
int f;
int ans=0;
for(f=start;f<=end;)
{
if(f%1000==0&&f+1000<end)
{
ans+=e[f/1000];
f+=1000;
}
else if(f%100==0&&f+100<end)
{
ans+=d[f/100];
f+=100;
}
else if(f%10==0&&f+10<end)
{
ans+=dah[f/10];
f+=10;
}
else
{
ans+=(nums[f]==0);
f++;
}
}
printf("%d\n",ans);
}
}
return 0;
}
In this approach I save number of multiples of 3 between k*1000 and (k+1)*1000 and also the same thing for k*100 and (k+1)*100 and also for 10. this helps me query faster. but this yet gives me time limit exceed.
Hint #1:
Think about how you might use the MODULUS operator to help you. Initially, you have N numbers, let's say N is 5.
So we can store the remainders for each number (i.e. store 0 MOD 3, 1 MOD 3, 2 MOD 3, and so on):
a[0] = 0
a[1] = 1
a[2] = 2
a[3] = 0
a[4] = 1
a[5] = 2
Each time you increment a range of numbers between A and B, you really only need to store a 0, 1, or 2 in the array. For example, if we are incrementing 2, then the new number will be 3. That is now divisible by 3, so we store 0 in the array. So in cases where we have 0 and we increment, we store 1, if we have 1 we store 2, and if we have 2 we store 0.
This optimization eliminates the need to do any division except for the initial step. Division is a very expensive operation, which is why we want to eliminate it where we can.
So after incrementing 0 through 5, the array would look like this:
a[0] = 1
a[1] = 2
a[2] = 0
a[3] = 1
a[4] = 2
a[5] = 0
The amount of numbers between A and B that are divisible by 3 is just the number of elements that have 0 (2 in this case).
Now you have to think about how to query a range A through B efficiently to find the amount of numbers divisible by 3.
Hint #2:
To find out how many numbers over the interval [A,B] are divisible by 3, one algorithm/data structure you can consider using is a segment tree. Read about it here. What this buys you is that now you can compute the amount of numbers divisible by 3 for any such interval [A,B] very quickly, instead of looping over the array and having to count them.
Hint #3:
Good suggestion by dcp. Though it doesn't reveal how to solve the problem. It's not necessary to store all numbers MOD 3 in the array. If the numbers are updated every time in the array the complexity is O(Q * N) which is obviously too much for given N, Q and 1 sec. in the worst case. That is the point of Ali in the comment to dcp suggestion.
The number of integers with MOD%0, MOD%1, MOD%2 can be stored in each node of the segment tree. Hence the updates can be done in O(log N), which results in O(Q log N) for updates only. For each query the same complexity O(log N) applies. Since you know the number of integers MOD%3 for each residue, it's not necessary to go down to all leaves (each leave in segment tree corresponds to array element) to figure how many numbers are divisible by 3. Once you understand how segment tree works that should be obvious why it is necessary to store residues in each node of the segment tree. Overall complexity of the algorithm is O(Q log N) which will fit nicely in 1 sec. time limit.
When going down the segment tree be sure to accumulate number of integers per residue, for each segment that you visit on the way down the tree.
what's the upper bound for your array? first, figure that out. Then, plan on reading lines of input in one of those two forms. The lines in format 0 A B are easy to handle, can you code at least that much? If so, post it and then worry about the lines in format 1 A B.
If, as your title suggests, you aren't sure how to tell if a number is divisible by three then I suggest you have a look into the modulus operation, in the languages I'm most familiar with it is represented using a %.