Advertisement
Guest User

kk

a guest
Sep 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int n,v[1000],y;
  4. bool prim(int x)
  5. {
  6. if (x<2){
  7. return false;
  8. }
  9. if (x%2==0&&x!=2){
  10. return false;
  11. }
  12. for (int i=3;i*i<=x;i+=2){
  13. if (x%i==0){
  14. return false;
  15. }
  16. }
  17. return true;
  18. }
  19. void cerintaUnu()
  20. {
  21. for (int i=0;i<n;i++){
  22. if (prim(v[i])){
  23. cout<<v[i]<<' '<<i+1<<endl;
  24. }
  25. }
  26. }
  27. int Euclid(int a,int b)
  28. {
  29. int c;
  30. while (b!=0){
  31. c=a%b;
  32. a=b;
  33. b=c;
  34. }
  35. return a;
  36. }
  37. void cerintaDoi()
  38. {
  39. int DC=Euclid(v[0],v[1]);
  40. for (int i=2;i<n;i++){
  41. DC=Euclid(DC,v[i]);
  42. }
  43. cout<<"2. "<<DC<<endl;
  44. }
  45. void afisare()
  46. {
  47. for (int i=0;i<n;i++){
  48. cout<<v[i]<<' ';
  49. }
  50. cout<<endl;
  51. }
  52. void cerintaTrei()
  53. {
  54. int i, j;
  55. for (i = 0; i < n-1; i++){
  56. for (j = 0; j < n-i-1; j++){
  57. if (v[j] > v[j+1]){
  58. swap(v[j] , v[j+1]);
  59. }
  60. }
  61. }
  62. afisare();
  63. }
  64. void cerintaPatru()
  65. {
  66. int st=0,dr=n,poz=-1,m;
  67. while (st<dr&&poz==-1){
  68. m=(st+dr)/2;
  69. if (v[m]==y){
  70. poz=y;
  71. }else{
  72. if (v[m]<y){
  73. st=m;
  74. }else{
  75. dr=m;
  76. }
  77. }
  78. }
  79. if (poz==-1){
  80. cout<<"NU S-A GASIT";
  81. }else{
  82. cout<<"ELEMENT GASIT PE POZITIA A "<<poz<<"-A";
  83. }
  84. }
  85. void citire()
  86. {
  87. cin>>n;
  88. for (int i=0;i<n;i++){
  89. cin>>v[i];
  90. }
  91. }
  92. int main() {
  93. citire();
  94. cerintaUnu();
  95. cerintaDoi();
  96. cerintaTrei();
  97. cout<<"ELEMENTUL CARE TREBUIE CAUTAT ESTE: ";
  98. cin>>y;
  99. cerintaPatru();
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement