Jump to content
TCD

Problema C.

Recommended Posts

Posted

Am si eu o problema de facultate de anul I. Cine ma poate ajuta, va rog?

Prime

Se consider? un num?r natural nenul n.

Cerin??

Scrie?i o func?ie care genereaz? toate posibilit??ile de scriere a lui n ca sum? de numere prime.

Exemplu

intrare |: 8

ie?ire : 2+2+2+2;2+3+3; 3+5

Posted

#include <stdio.h>
#include <conio.h>
int n,x[100],v[100],m=0;

int prim(int n){
int i;
if(n==0 || n==1) return 0;
else if(n==2 || n==3) return 1;
else if(n%2==0) return 0;
for(i=3;i*i<=n;i+=2)
if(n%i==0) return 0;
return 1;
}
void afis(int n){
int i;
for(i=1;i<=n;i++)
printf("%d ",x[i]);
putchar('\n');
}

void back(int k,int sp){
int i;
for(i=1;i<=m;i++){
x[k]=v[i];
sp=sp+x[k];
if(sp<=n && x[k]>x[k-1]) if(sp==n) afis(k);
else back(k+1,sp);
sp=sp-x[k];
}
}

main(){
int i;
puts("\nDati n: "); scanf("%d",&n);
for(i=2;i<=n;i++) if(prim(i)) { m++;
v[m]=i;
}
back(1,0);
getch();
}

cam asta ar fi .. trebuie sa stapanesti conceptul de backtracking prima data , si abia apoi poti jongla cu asa ceva.

Posted

Banuiesc ca esti la FII si-ti trebuia pentru tema5, aici gasesti problema. Alte surse mai poti gasi in box.com/cmiN -> FII/L1/AP, oricum s-a terminat semestrul, da' cine stie, pe viitor poate o sa gasesti chestii interesante.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...