this is the question:: Problem 2: Find the Numbers, (K Narayan Kumar, CMI)
This is a rather simple problem to describe. You will be given three numbers S, P and k. Your task is to find if there are integers n1, n2,...,nk such that n1 + n2 +...+ nk = S, n1 * n2 * ... * nk = P. If such integers exist, print them out. If no such sequence of integers exist, then print "NO".
For example if S=11, P=48 and k=3 then 3, 4 and 4 is a solution. On the other hand, if S=11, P=100 and k=3, there is no solution and you should print "NO".
Input format
A single line with three integers S, P and k.
Output format
</>
A single word "NO" or a seqence of k integers n1, n2,..., nk on a single line. (The ni's must add up to S and their product must be P).
Test data
You may assume that 1 = k = 4, 1 = S = 1000 and 1 = P = 1000.
Example
We now illustrate the input and output formats using some examples.
Sample input 1:
11 48 3
Sample output 1:
3 4 4
Sample input 2:
11 100 3
Sample output 2:
NO
here is my program::
/*NOT WORKING----program to find the numbers using recursion*/
This is a rather simple problem to describe. You will be given three numbers S, P and k. Your task is to find if there are integers n1, n2,...,nk such that n1 + n2 +...+ nk = S, n1 * n2 * ... * nk = P. If such integers exist, print them out. If no such sequence of integers exist, then print "NO".
For example if S=11, P=48 and k=3 then 3, 4 and 4 is a solution. On the other hand, if S=11, P=100 and k=3, there is no solution and you should print "NO".
Input format
A single line with three integers S, P and k.
Output format
</>
A single word "NO" or a seqence of k integers n1, n2,..., nk on a single line. (The ni's must add up to S and their product must be P).
Test data
You may assume that 1 = k = 4, 1 = S = 1000 and 1 = P = 1000.
Example
We now illustrate the input and output formats using some examples.
Sample input 1:
11 48 3
Sample output 1:
3 4 4
Sample input 2:
11 100 3
Sample output 2:
NO
here is my program::
/*NOT WORKING----program to find the numbers using recursion*/
Code:
#include<stdio.h> int main() { int s,p,a=0,b=0,c=0,match=0; printf("\nEnter Sum & Product: "); scanf("%d %d",&s,&p); recursive (s,p,&a,&b,&c,1,&match); if (match==1) printf("\nThe numbers are %d %d %d",a,b,c); else printf("\nNumbers are not found."); return 0; } recursive (int s,int p,int *a,int *b,int *c,int level,int *match) { int i=0; printf("\nlevel=%d\t\ta=%d,b=%d,c=%d",level,*a,*b,*c); if (level==3) { if (s==p) { *c=s; *match=1; } return; } if (*match==1) { return; } //printf("\ndebug"); for (i=2;i<p/2;i++) if (p%i==0) { //printf("\ncase level=%d\t\ti=%d,p=%d",level,i,p); if (level==2) *b=i; if (level==1) *a=i; p=p/i; s=s-i; //printf("\nlevel=%d\t\t,p=%d,s=%d",level,p,s); recursive (s,p,a,b,c,level+1,match); } if (i>=s) return; }
Comment