Advertisement
a53

BiMinPrim

a53
Dec 28th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. ifstream fin("biminprim.in");
  6. ofstream fout("biminprim.out");
  7. int nt[5001],X=1000000000,Y=1000000000;
  8. struct nod
  9. {
  10. int info;
  11. nod * st, *dr;
  12. };
  13.  
  14. void citire(nod * & p)
  15. {
  16. int x;
  17. fin>>x;
  18. if(x==0)
  19. p=NULL;
  20. else
  21. {
  22. p=new nod;
  23. p->info=x;
  24. citire(p->st);
  25. citire(p->dr);
  26. }
  27. }
  28.  
  29.  
  30. int prim(int n)
  31. {
  32. if(n==0 || n==1)
  33. return 0;
  34. else
  35. if(n%2==0&&n!=2)
  36. return 0;
  37. else
  38. for(int d=3;d*d<=n;d=d+2)
  39. if(n%d==0)
  40. return 0;
  41. return 1;
  42. }
  43.  
  44. void preordine(nod * p)
  45. {
  46. if(p)
  47. {
  48. if(p->info<Y&&prim(p->info))
  49. Y=p->info;
  50. preordine(p->st);
  51. preordine(p->dr);
  52. }
  53. }
  54.  
  55. void stergeTot(nod * & p)
  56. {
  57. if(p!=NULL)
  58. {
  59. stergeTot(p->st);
  60. stergeTot(p->dr);
  61. delete p;
  62. p=NULL;
  63. }
  64. }
  65.  
  66. int main()
  67. {
  68. nod * p = NULL;
  69. citire(p);
  70. preordine(p->st);
  71. if(Y==1000000000)
  72. X=-1;
  73. else
  74. X=Y;
  75. Y=1000000000;
  76. preordine(p->dr);
  77. if(Y==1000000000)
  78. Y=-1;
  79. fout<<X<<' '<<Y;
  80. stergeTot(p);
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement