a very easy bag problem
Mar 10, 2013 at 6:48pm UTC
I am trying to output the partition of the integer n;such as n=2+3+5=10;My code actually works but I don't know how to output all the solves.
for example ,you input n=10, it shows ,2,3,4; but when you input 10,it dosen't work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
//package problem
#include <iostream>
using namespace std;
bool done(int n)
{
return (n==0);
};
void finalarray(int *b,int &t)
{
cout<<"the solve is :" <<endl;
for (int i=0;i<t;i++)
cout<<b[i];
};
int main()
{
bool rec[8];
int b[10];
int n;
int a[8]={1,2,3,4,5,6,7,8};
cout<<"please input a number" <<endl;
cin>>n;
if (n==0)
cout<<"put a bigger number" <<endl;
for (int i=0;i<8;i++)
{
rec[i]=false ;
}
rec[0]=true ;
int t=0;
for (int i=0;i<8;i++)
{
if (rec[i])
continue ;
n=n-a[i];
rec[i]=true ;
b[t]=a[i];
t++;
if (!done(n))
continue ;
else
finalarray(b,t);
}
system("pause" );
return 0;
}
Last edited on Mar 10, 2013 at 6:50pm UTC
Topic archived. No new replies allowed.