Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <fstream>
- using namespace std;
- ifstream fi("sortare_divizori.in");
- ofstream g("sortare_divizori.out");
- struct vect
- {
- int nr, poz;
- };
- vect v[1001];
- int f[1001], n;
- bool comp(vect comp1, vect comp2)
- {
- if(comp1.nr == comp2.nr)
- return f[comp1.poz] > f[comp2.poz];
- return comp1.nr < comp2.nr;
- }
- int diviz(int n)
- {
- int d = 2,p,nrdiv=1;
- while(n > 1)
- {
- p = 0;
- while(n % d == 0)
- {
- p++;
- n = n / d;
- }
- if(p)
- nrdiv = nrdiv * (p+1);
- d++;
- if(n > 1 && d * d >n) d = n;
- }
- return nrdiv;
- }
- int main()
- {
- fi >> n;
- for(int i = 1; i <= n; i++)
- {
- fi >> v[i].nr;
- f[i] = v[i].nr;
- v[i].nr = diviz(v[i].nr);
- v[i].poz = i;
- }
- sort(v + 1, v + 1 + n, comp);
- for(int i = n; i >= 1; i--)
- g << f[v[i].poz] <<" ";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement