Advertisement
a53

Sortare Divizori

a53
Jun 5th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <algorithm>
  3. using namespace std;
  4. FILE *f,*g;
  5. struct sir
  6. {
  7. int nr;
  8. int nd;
  9. };
  10. sir a[1000];
  11. int n;
  12.  
  13. inline bool cmp(sir a,sir b)
  14. {
  15. if(a.nd==b.nd)
  16. return a.nr<b.nr;
  17. else
  18. return a.nd>b.nd;
  19. }
  20.  
  21. int nrdiv(int n)
  22. {
  23. int p=1,d,exp=0; // p retine numarul divizorilor
  24. if(n%2==0)
  25. {
  26. while(n%2==0)
  27. {
  28. exp++;
  29. n=n/2;
  30. }
  31. p*=(exp+1);
  32. }
  33. d=3;
  34. while(n>1)
  35. {
  36. if(n%d==0)
  37. {
  38. exp=0;
  39. while(n%d==0)
  40. {
  41. exp++;
  42. n=n/d;
  43. }
  44. p*=(exp+1);
  45. }
  46. else
  47. d+=2;
  48. if(n>1&&d*d>n)
  49. {
  50. p*=2; // aici exp=1
  51. break;
  52. }
  53. }
  54. return p;
  55. }
  56.  
  57. int main()
  58. {
  59. f=fopen("sortare_divizori.in","rt");
  60. g=fopen("sortare_divizori.out","wt");
  61. fscanf(f,"%d",&n);
  62. for(int i=0;i<n;i++)
  63. {
  64. fscanf(f,"%d ",&a[i].nr);
  65. a[i].nd=nrdiv(a[i].nr);
  66. }
  67. sort(a,a+n,cmp);
  68. for(int i=0;i<n;i++)
  69. fprintf(g,"%d ",a[i].nr);
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement