Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Spongebob Squarepants and Raffle Balls

Spongebob Squarepants was waiting at the Pool Club for his best friend Patrick.
As he got so bored of waiting, he set off for a game with Raffle Balls available inside the Pool Club.
There was a bag filled with N Raffle balls. Each Ball has a distinct number from 1 to N printed on it. All the numbers are distinct. Spongebob withdrew three balls from the bag and took their sum. Now Spongebob aimed to calculate the probability that the sum is not greater than the given number K(<=N). He was particular that the answer should be displayed in the form of p/q. (except when the answer is 0 or 1).
Can you help Spongebob in writing a program that eases the task of finding the probability?

Input Format :
Input consists of two integers, N and K. (0<=K<=N<=10000).

Output Format :
Output the result in the form of p/q (Except when the answer is 0 or 1).
Refer sample input and output for formatting specifications.

Sample Input 1:
5 6
Sample Output 1:
1/10

Sample Input 2:
5 7
Sample Output 2:
1/5

What I have tried:

C
main()
{
 int n,k,b,c,d;
scanf("%d%d",&n,&k);
c=n+k;
b=c-10;
d=c-10;
printf("%d%d",b,d);
return 0;
}
Posted
Updated 5-Aug-19 23:19pm
v2
Comments
Patrice T 15-May-19 4:43am    
Give sample input with wrong output.
phil.o 15-May-19 4:44am    
Your algorithm to compute the probability is wrong. You will need much more that an addition and two subtractions :) Have you written it down on a paper first?

Quote:
I have got answers but some test case where failing and giving as wrong answer when I submit it
C++
main()
{
int n,k,b,c,d;
scanf("%d%d",&n,&k);
c=n+k;
b=c-10;
d=c-10;
printf("%d%d",b,d);
return 0;
} 

No: this program can't get some correct answers because it don't try to handle the problem at all.

Since the problem comes from a challenge site, all the interest is that you solve the problem by yourself.

Advice: Take a sheet of paper, a pencil and solve the problem by hand for every sum with 10 balls. And see how things works, it is the algorithm.
 
Share this answer
 
v2
Comments
Stefan_Lang 6-Aug-19 5:09am    
Are you sure it's from a challenge site? Neither the restriction (K<=N) makes any sense, nor are the samples correct. Moreover, what kind of programming challenge site would use Spongebob??
Patrice T 6-Aug-19 5:26am    
The wording is typical of challenges sites.
I only realize now how is SpongeBob, in French he got a different name that I am used to.
Stefan_Lang 6-Aug-19 5:30am    
I just realized that it might indeed be from a challenge site, because of the odd limitation of K<=N: with that restriction, the number of sequences that add up to no more than K is independent of N! And this greatly simplifies the solution if you know how.
The examples you provided are incorrect. The smallest sum you can generate from three distinct integers between 1 and N is 1+2+3=6, so there can be no sequence with a sum of 5! Also the llimitation K<=N doesn't make a lot of sense! K must be in the range 6 .. 3*N-3, because N+(N-1)+(N-2) = 3*N-3 is the greatest sum you can calculate from these numbers.

Your code has nothing at all to do with the problem! You should show more effort - all you do is ask for others to do your homework.

I suggest that you write a program to generate all valid combinations of balls that could be drawn, count the combinations, calculate the sum, and count the cases where the sum is no greater than K. It won't be fast, but should get you the correct results.

Here's some pseudo code, omitting input and initialization:
for (every valid sequence {first_number, second_number, third_number})
    increment sequence_counter
    sum = first_number + second_number + third_number
    if (sum <= K)
        increment sum_not_greater_counter
print sum_not_greater_counter / sequence_counter

That's already more help than you deserve for showing no effort. But it's so basic that you may find you can apply the same idea (generating all combinations) on other problems as well.
 
Share this answer
 
public static void main(String[] a)
   {
   int n, k, b, c, d, e;
   Scanner sc = new Scanner(System.in);
   System.out.println("Enter value for k");
   k = sc.nextInt();
   System.out.println("Enter value for n");
   n = sc.nextInt();

   c = n + k;
   b = n - k;
   d = c - b;

   System.out.println(b + " " + d);

   }
 
Share this answer
 
Comments
CHill60 6-Aug-19 8:44am    
An unexplained code dump is not a solution!
Stefan_Lang 6-Aug-19 8:56am    
This is far from a correct solution, not the right language, and no help whatsoever.
import java.util.Scanner;

/**
 *
 * @author abhimanyu
 */
public class spongebob {
    public static void main(String[] args)
    {
        int n, k,q,f,a,b,c,i,j,l,r=0,p=0,sum,num=0;
   Scanner sc = new Scanner(System.in);
   System.out.println("Enter value for k");
   k = sc.nextInt();
   System.out.println("Enter value for n");
   n = sc.nextInt();
   
   
   for(i=n;i>=1;i--)
   {   
   a=i-2;
   b=i-3;
   c=b*(b+1)/2;
   f=a+c;
   num=num+f;
   }
         for(i = 1; i <= n; i++){
			for(j = i; j <= n; j++){
				for(l = j; l <= n; l++){
					if(l != i && l != j && i != j)
                                        {
                                           r=i+j+l;
                                           if (r<=k)
            p=p+1;
                                        }
                                }
                        }
         }
        
            System.out.println(p+" "+num);
    }
}
 
Share this answer
 
Comments
CHill60 6-Aug-19 8:44am    
An unexplained code dump is not a solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900