Advertisement
alexon5519

29-programare

Apr 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3.  
  4. int c1(int x){
  5. while(x >= 10)
  6. x/=10;
  7. return x;
  8. }
  9. int t(int x){
  10. int c=0;
  11. while(x != 0){
  12. if(x % 2 == 1)
  13. c++;
  14. x/=2;
  15. }
  16. return c;
  17. }
  18.  
  19.  
  20. using namespace std;
  21.  
  22.  
  23. int main(){
  24. struct {
  25. int x,c,nr;
  26. }
  27. V[100],aux;
  28. ifstream f1("atestat.in");
  29. ofstream f2("atestat.out");
  30. int n,i,j;
  31. f1>>n;
  32. for(i=1;i <= n;i++){
  33. f1>>V[i].x;
  34. V[i].c=c1(V[i].x);
  35. V[i].nr=t(V[i].x);
  36. }
  37. for(i=1;i<n;i++)
  38. for(j=i+1;j<=n;j++)
  39. if(V[i].c < V[j].c){
  40. aux=V[i];
  41. V[i]=V[j];
  42. V[j]=aux;
  43. }
  44. for(i=1;i <= n; i++)
  45. f2<<V[i].x<<" ";
  46. f2<<endl;
  47. for(i=1;i<n;i++)
  48. for(j=i+1;j<=n;j++)
  49. if(V[i].nr > V[j].nr){
  50. aux=V[i];
  51. V[i]=V[j];
  52. V[j]=aux;
  53. }
  54. else
  55. if(V[i].nr == V[j].nr)
  56. if(V[i].x < V[j].x){
  57. aux=V[i];
  58. V[i]=V[j];
  59. V[j]=aux;
  60. }
  61. for(i=1;i<=n;i++)
  62. f2<<V[i].x<<" ";
  63. f1.close();
  64. f2.close();
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement