Advertisement
icatalin

problema clasa 26.11

Nov 26th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. ifstream f("numere.txt");
  9. ofstream g("numere.out");
  10.  
  11. int prim(int x)
  12. {
  13. int i;
  14. for (i=2;i<=sqrt(x);i++)
  15. if (x%i==0)
  16. return 1; // nu e prim
  17. return 0;
  18. }
  19.  
  20. int palindrom(int x)
  21. {
  22. int nr=0,xx;
  23. xx=x;
  24. while (xx)
  25. {
  26. nr=nr*10+xx%10;
  27. xx/=10;
  28. }
  29. if (nr==x)
  30. return 1; // e ogl
  31. return 0;
  32. }
  33.  
  34. int cmmdc(int a,int b)
  35. {
  36. int r;
  37. r=a%b;
  38. while(r)
  39.  
  40. {
  41. a=b;
  42. b=r;
  43. r=a%b;
  44. }
  45.  
  46. if (prim(b)==0)
  47. return 0;// cmmdc e prim
  48. return 1;// NU e prim cmmdc
  49.  
  50. }
  51.  
  52. int main()
  53. {
  54. int i,n,ok=0,a,b,ok2=0;
  55. f>>n;
  56.  
  57. for (i=1;i<=n;i++)
  58. {
  59. f>>a>>b;
  60.  
  61. if (palindrom(a)==1 && palindrom(b)==1)
  62. ok=1;
  63.  
  64. if (cmmdc(a,b)==0)
  65. ok2=1;
  66.  
  67. if (ok==1 && ok2==1)
  68. g<<a<<" "<<b<<" "<<'\n';
  69. }
  70.  
  71. return 0;
  72. }
  73.  
  74. /* in fisierul txt numere.txt se afla pe prima linie un nr nat "n" si pe urm n linii cate 2 numere nat. despartite prin cate-un spatiu.
  75. scrieti un program care citeste cele n perechi de numere si afiseaza cate dintre acestea (perechi) sunt palindrom si au cmmd nr prim.
  76. se vor folosi minim 3 subprograme pentru nr prim, palindrom, cmmdc
  77.  
  78. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement