Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ghost and Shadow were playing a game. Shadow gives Ghost a number n and Ghost has to give cookies to Shadow. The number of cookies is calculated as the maximum divisor of n (not equal to n, of course). For example, if n = 6 the Ghost has to give 3 cookies, while for n = 25 he needs to give 5 and if n = 2 he only needs to give 1 cookie.

As Ghost is very smart he wants to fool Shadow. He introduces a rule saying, that he can break n into several n1 + n2 + ... + nk = n (here k is arbitrary and k = 1 is also possible) and he will give the cookies according to those split parts. ni 2, for all i from 1 to k.

Help Ghost calculate how many cookies he has to give.

What I have tried:

My code is working for splitting number in two parts
Posted
Updated 29-Jul-18 2:21am
Comments
Kenneth Haugland 29-Jul-18 8:28am    
Use the formula (k^2+k)/2.

1 solution

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
gogo6996 29-Jul-18 8:27am    
//shadow gives ghost a number n
//if n =6 ghost has to give 3 cookies
//if n =2 ghost has to give 1 cookies
//work for splitting in two parts
//it will always break number into two parts
//and the no.of cookie will always 2

#include <iostream>

using namespace std;

typedef struct set{
int x;
int y;
}vector;

vector dataset[100];
int count = 1;

int fact(int n){
int j=1;


for(int i=1;i<n;i++){
if(n%i == 0){
j=i;
}
}
return j;
}

//we have to introduce prime-number concept so that we can minimize the no.of cookies
bool isPrime(int n)
{

if (n <= 1) return false;


for (int i=2; i<n; i++)
if (n%i == 0)
return false;

return true;
}

int split(int n){

int m=0;
for(int i=2;i<n;i++){
for(int j=2;j<n;j++){
if(i+j == n){
count++;
dataset[m].x = i;
dataset[m].y = j;
m = m+1;
}
}
}
}

int findMin(int arr[],int n){
int minthisfunc = arr[0];
int i ;
int minindex = 0;
for( i=1;i<n;i++){
if(minthisfunc > arr[i]){
minthisfunc = arr[i];
}
}

for( i=1;i<n;i++){
if(minthisfunc == arr[i]){
return i;
}
}

}

int main()
{
int n;
cout<<"give number"<<endl;
cin>>n;

if(n>0 && n<10){
if(isPrime(n)){
cout<<n<<endl;
return 0;
}
}

split(n);

int min[1000] ;
int j=0;




for(int i=0;i<count-1;i++){
min[j] = fact(dataset[i].x)+fact(dataset[i].y);
++j;
}

int ans = findMin(min,count-1);

cout<<"no.of cookies->"<<fact(dataset[ans].x)+fact(dataset[ans].y)<<endl;
cout<<"("<<dataset[ans].x<<","<<dataset[ans].y<<")";

return 0;
}
Patrice T 29-Jul-18 9:17am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
And gives examples of test cases that work and that don't.
gogo6996 29-Jul-18 8:28am    
its working for only some test cases .Please help
OriginalGriff 29-Jul-18 8:49am    
Which test cases does it work for, and which doesn't it?
What are the common factors in the test data?

What have you tried to work out what the problem is?

Writing the code is the easy bit: getting it working is the major part of your task!
Nelek 29-Jul-18 13:19pm    
Not agree... the major part of his task is to understand what he is doing / needs to do. So far he is failing :sigh:

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