Ganea Mihnea Tema Info Probleme Cu Solutie Eurisitca Etc

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

1.

Plata sumei
a) Cu greedy

#include <iostream>
#include <algorithm>
using namespace std;
int n,v[100],s,nr,x[100];
int main()
{
cin >>n>>s;
for(int i=1;i<=n;i++)
cin>>v[i];
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
if (v[i]>v[j])
swap(v[i],v[j]);
for(int i=n;i>=1;i--)
{
while (s>=v[i])
{
s-=v[i];
x[i]++;
nr++;
}
}
if(s!=0)cout<<"Nu are sol";
for(int i=nr;i>=1;i--)
{
if(x[i])cout<<x[i]<<" " <<v[i]<<endl;
}
return 0;
}

b)Cu bkt
#include <iostream>

using namespace std;

int n , p[20] , x[11] , a[20] , ok , s , c[20];

void afisare()
{
for(int i = 1 ; i <= n ; i++)
cout << x[i] << " ";
ok = 1;
}
int s1=0;
void bkt(int k )
{
for(int i = 0 ; i <= s/a[k] ; i++)
if (!ok)
{
x[k] = i;
s1 += x[k] * a[k];
if(s1 <= s && k <= n)
{
if(k == n && s1 == s) afisare();
else if(k < n)bkt(k + 1 );
}
s1 -= x[k] * a[k];
}

int main()
{
cin >> n >> s;
for(int i = 1 ; i <= n ; i++)
cin >> a[i];

bkt(1 );
}

2.
a)Cu greedy

#include <fstream>
using namespace std;
ifstream f("harta.in");
ofstream g("harta.out");
int a[101][101], n , nr,v[101];
int main()
{
f >> n;
int x,y;
while (f >> x >> y)
a[x][y] = a[y][x] = 1;
for (int i = 1; i <= n; i++)
{
v[i] = 1;
for (int j = 1; j <= n - 1; j++)
if (a[i][j] == 1 && v[j] == v[i])
{
v[i] = v[i] + 1;
if (v[i] > nr) nr = v[i];
}
}
g << nr << '\n';
for (int i = 1; i <= n; i++)
g << i << " " << v[i] << '\n';
}

b) Cu bkt

3.Umplerea tablei de sah prin saritura calului

a)Cu greedy

b)Cu bkt

4)

a)cu greedy

#include <fstream>
using namespace std;
ifstream f("voiajor.in");
ofstream g("voiajor.out");
int n, a[101][101], v[101], sol[101], nr, x, y, z, u = 1;
int vizitat(int v[101], int n)
{
for (int i = 1; i <= n; i++)
if (v[i] == 0) return 1;
return 0;
}
int neviz(int a[101][101], int n, int x)
{
int mn = 100000;
int p = 0;
for (int i = 1; i <= n; i++)
if (a[x][i] && a[x][i] <= mn && v[i] == 0)
{
mn = a[x][i];
p = i;
}
return p;
}
int main()
{
f >> n;
while (f >> x >> y >> z)
a[x][y] = a[y][x] = z;
sol[1] = 1;
v[1] = 1;
while (vizitat(v, n))
{
x = neviz(a, n, sol[u]);
if (x)
{
v[x] = 1;
u++;
sol[u] = x;
nr += a[sol[u - 1]][sol[u]];
}
}
nr += a[u][1];
g << nr << '\n';
for (int i = 1; i <= n; i++)
g << sol[i] << " ";
g << 1;
}

b)cu bkt

You might also like