Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Коробейников Лев
- //лаба-6
- /*Вариант-42.Ввести последовательность натуральных чисел {Aj}j=1...n (n<=1000).
- Упорядочить последовательность по невозрастанию первой цифры числа,
- числа с одинаковыми первыми цифрами дополнительно упорядочить по неубыванию произведения цифр числа,
- числа с одинаковыми первыми цифрами и одинаковыми произведениями цифр дополнительно упорядочить по невозрастанию самого числа. */
- #include <iostream>
- #include <cmath>
- #include <vector>
- using namespace std;
- int FirstNum(int a){
- while(a > 9){
- a /= 10;
- }
- return a;
- }
- int ProductNum(int a){
- int ans = 1;
- while(a > 9){
- ans *= a % 10;
- a /= 10;
- }
- ans *= a;
- return ans;
- }
- int main(){
- int n;
- cin >> n;
- vector<int> mas(n);
- for(int i = 0; i < n; i++){
- cin >> mas[i];
- }
- for(int i = 0; i < n - 1; i++){
- for(int j = i + 1; j < n; j++){
- if(FirstNum(mas[i]) < FirstNum(mas[j])){
- swap(mas[i], mas[j]);
- }
- if(FirstNum(mas[i]) == FirstNum(mas[j])){
- if(ProductNum(mas[i]) > ProductNum(mas[j])){
- swap(mas[i], mas[j]);
- }
- if(ProductNum(mas[i]) == ProductNum(mas[j])){
- if(mas[i] < mas[j]){
- swap(mas[i], mas[j]);
- }
- }
- }
- }
- }
- for(int i = 0; i < n; i++){
- cout << mas[i] << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement