0% found this document useful (0 votes)
73 views

PRBLM C++

The document describes two algorithms, greedy and backtracking, for writing a natural number n as the sum of terms equal to a or b, with the minimum or maximum number of terms equal to a. The greedy algorithm starts by subtracting the smaller number a from n until the remainder is divisible by b. It then outputs the terms. The backtracking algorithm initializes variables, defines recursive functions to generate, validate, and find solutions for compositions, and outputs the composition with the minimum or maximum terms equal to a found.

Uploaded by

Aida Voicu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

PRBLM C++

The document describes two algorithms, greedy and backtracking, for writing a natural number n as the sum of terms equal to a or b, with the minimum or maximum number of terms equal to a. The greedy algorithm starts by subtracting the smaller number a from n until the remainder is divisible by b. It then outputs the terms. The backtracking algorithm initializes variables, defines recursive functions to generate, validate, and find solutions for compositions, and outputs the composition with the minimum or maximum terms equal to a found.

Uploaded by

Aida Voicu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

//#4007

/*Se dau trei numere naturale n a b, 1 ≤ a < b < n.

Să se determine o modalitate de a-l scrie pe n ca sumă de termeni egali

cu a sau b în care numărul de termeni egali cu a este minim.*/

//greedy

/*

#include <iostream>

#include <fstream>

using namespace std;

ifstream fin ("part1.in");

ofstream fout ("part1.out");

int main()

int cnta=0,a,b,n;

cin>>n>>a>>b;

while (n%b!=0)

n-=a;

cnta++;

for (int i=1;i<=cnta;++i)

cout<<a<<" ";

for (int i=1;i<=(n/b);++i)

cout<<b<<" ";

return 0;

*/

//backtracking
/*#include <iostream>

#include <fstream>

using namespace std;

ifstream fin ("part1.in");

ofstream fout ("part1.out");

int st[15],v[15],c[15],S,k,n,cnt_a=1e7+1,cnt_b;

void init()

st[k]=0;

int valid()

return 1;

int succesor()

if(st[k]<c[k])

st[k]++;

return 1;

else

return 0;

int solutie()

int sum=0 ;
for(int i=1;i<=k;i++)

sum+=st[i]*v[i];

return sum==S;

void tipar()

if(st[1]<cnt_a)

cnt_a=st[1];

cnt_b=st[2];

void back()

int AS;

k=1;

init();

while(k>0)

do{}while((AS=succesor())&&(!valid())) ;

if(AS)

if(solutie())

tipar();

else

k++;

init();
}

else {k--;}

int main ()

fin>>S;

for(int i=1;i<=2;i++)

fin>>v[i];

c[i]=(int)(S/v[i]);

back();

for(int i=1;i<=cnt_a;i++ )

fout<<v[1]<<' ';

for(int i=1;i<=cnt_b;i++)

fout<<v[2]<<' ';

return 0 ;

*/

//#4006

/*Se dau trei numere naturale n a b, 1 ≤ a < b < n.

Să se determine o modalitate de a-l scrie pe n ca sumă de termeni egali


cu a sau b în care numărul de termeni egali cu a este maxim.*/

//greedy

/*

#include <iostream>

#include <fstream>

using namespace std;

ifstream fin ("part.in");

ofstream fout ("part.out");

int main()

int cntb=0,a,b,n;

cin>>n>>a>>b;

while (n%a!=0)

n-=b;

cntb++;

for (int i=1;i<=(n/a);++i)

cout<<a<<" ";

for (int i=1;i<=cntb;++i)

cout<<b<<" ";

return 0;

*/

//backtracking

/*

#include <iostream>

#include <fstream>

using namespace std;


ifstream fin ("part.in");

ofstream fout ("part.out");

int st[15],v[15],c[15],S,k,n,cnt_a,cnt_b;

void init()

st[k]=0;

int valid()

return1;

int succesor()

if(st[k]<c[k])

st[k]++;

return 1;

else

return 0;

int solutie()

int sum=0;

for(int i=1;i<=k;i++)

sum+=st[i]*v[i];
return sum==S;

void tipar()

if(st[1]>cnt_a)

cnt_a=st[1];

cnt_b=st[2];

void back()

int AS;

k=1;

init();

while(k>0)

do{}while((AS=succesor())&&(!valid()));

if(AS)

if(solutie())

tipar();

else

k++;

init();

else {k--;}
}

int main ()

fin>>S;

for(int i=1;i<=2;i++)

fin>>v[i];

c[i]=(int)(S/v[i]);

back();

for(int i=1;i<=cnt_a;i++)

fout<<v[1]<<' ';

for(int i=1;i<=cnt_b;i++ )

fout<<v[2]<<' ';

return 0 ;

*/

You might also like