Advertisement
Nasysysy

Untitled

Oct 17th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. //Коробейников Лев
  2. //лаба-6
  3. /*Вариант-42.Ввести последовательность натуральных чисел {Aj}j=1...n (n<=1000).
  4. Упорядочить последовательность по невозрастанию первой цифры числа,
  5. числа с одинаковыми первыми цифрами дополнительно упорядочить по неубыванию произведения цифр числа,
  6. числа с одинаковыми первыми цифрами и одинаковыми произведениями цифр дополнительно упорядочить по невозрастанию самого числа. */
  7.  
  8. #include <iostream>
  9. #include <cmath>
  10. #include <vector>
  11. using namespace std;
  12.  
  13. int FirstNum(int a){
  14.     while(a > 9){
  15.         a /= 10;
  16.     }
  17.     return a;
  18. }
  19. int ProductNum(int a){
  20.     int ans = 1;
  21.     while(a > 9){
  22.         ans *= a % 10;
  23.         a /= 10;
  24.     }
  25.     ans *= a;
  26.     return ans;
  27. }
  28.  
  29. int main(){
  30.     int n;
  31.     cin >> n;
  32.     int mas[n];
  33.     for(int i = 0; i < n; i++){
  34.         cin >> mas[i];
  35.     }
  36.     for(int i = 0; i < n - 1; i++){
  37.         for(int j = i + 1; j < n; j++){
  38.             if(FirstNum(mas[i]) < FirstNum(mas[j])){
  39.                 int buf = mas[i];
  40.                 mas[i] = mas[j];
  41.                 mas[j] = buf;
  42.             }
  43.             if(FirstNum(mas[i]) == FirstNum(mas[j])){
  44.                 if(ProductNum(mas[i]) > ProductNum(mas[j])){
  45.                     int buf = mas[i];
  46.                     mas[i] = mas[j];
  47.                     mas[j] = buf;
  48.                 }
  49.                 if(ProductNum(mas[i]) == ProductNum(mas[j])){
  50.                     if(mas[i] < mas[j]){
  51.                         int buf = mas[i];
  52.                         mas[i] = mas[j];
  53.                         mas[j] = buf;
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     for(int i = 0; i < n; i++){
  60.         cout << mas[i] << " ";
  61.     }
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement