Click here to Skip to main content
15,883,841 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
input with this test is true
10 # the number of digits in the bottom
4 2 2 2 2 2 # first digit number of degrees in this row -1
1 2 2
1 3 2
1 2 3
3 2 2 2 2
2 2 2 2
1 3 3
3 3 3 3 3
2 4 3 3
2 2 3 4

output
2 4 3 6 7 5 9 10 1 8

Here the task is to raise to a power and then compare their numbers to derive the index in the order of increasing numbers
exponentiation is from the end
2 2 3 4
is raised as 3 ^ 4 = 81
Now 2 ^ 81 = 2417851639229258349412352

if you submit such numbers, it works incorrectly
input
3
4 5 2 49 4 5
4 3 2 7 2 11
4 2 2 49 32 2

output
3 2 1 should be
my conclusion 3 1 2




C++
#include <fstream>
#include <algorithm>
 
#include <stdio.h>
#include <math.h>
using namespace std;
 
class tower_t {
public:
    int num; 
    int height; 
    double val[11]; // содержимое
    double cache[11]; // кэш для ускорения расчета
 
    // Конструктор
    tower_t() {
        for (int i = 0; i < 11; i++) {
            val[i] = 1;
            cache[i] = 0;
        }
        height = 0;
    }
 
    // Тройной логарифм верхних 3-х уровней
     double head(int level) {
        if(cache[level] == 0) cache[level] = (log2(log2(val[level])) + log2(val[level + 1]) * val[level + 2]);
        return cache[level];
    }
 
    // Вычисление верхушки до тех пока влазит в double
    void normalize() {
        while(height > 1 && (log2(val[height - 2]) * val[height - 1]) < 50) {
            val[height - 2] = pow(val[height - 2], val[height - 1]);
            val[height - 1] = 1;
            height--;
        }
    }
 
    // Вывод для отладки
    void print() {
#ifdef _DEBUG
        printf("%2d: {", num);
        for (int i = 0; i < height; i++) {
            if (i > 0) printf(", ");
            if(val[i] < 1000000000) {
                printf("%0.0f", val[i]);
            } else {
                printf("%0.3e", val[i]);
            }
        }
        printf("}\n");
#endif
    }
};
 
// сравнение двух 
bool compare(tower_t& t1, tower_t& t2) {
    // этаж с которого сравнивать три последних уровня
    int level = ((t1.height > t2.height) ? t1.height : t2.height) - 3;
    if (level < 0) level = 0;
    if(t1.height == t2.height) { // если  одной высоты, сравниваем поэтажно
        for (int i = t1.height - 1; i >= 0; i--) {
            if (abs(t1.val[i] - t2.val[i]) > (t1.val[i] * 1e-14)) {
                if (i < level) { // верхи  совпали ниже level
                    return t1.val[i] < t2.val[i];
                }
                break;
            }
        }
    }
    return t1.head(level) < t2.head(level);
}
 
int main()
{
    // Считывание задания
    ifstream in ("input.txt");
    int cnt;
    in >> cnt;
    tower_t* towers = new tower_t[cnt];
    for (int i = 0; i < cnt; i++) {
        int len;
        in >> len;
        towers[i].num = i + 1;
        bool write = true;
        for (int j = 0; j <= len; j++) {
            int val;
            in >> val;
            if (val <= 1) write = false; // если уровень <= 1 то выше не читать
            if(write) {
                towers[i].val[j] = val;
                towers[i].height = j + 1;
            }
        }
        towers[i].print();
        towers[i].normalize();
    }
    // Сортировка
    sort(towers, towers + cnt, compare);
    // Вывод результата
    ofstream out("output.txt");
    for (int i = 0; i < cnt; i++) {
        out << towers[i].num << " ";
        towers[i].print();
    }
    out << endl;
    out.close();
    delete[] towers;
    return 0;
}


What I have tried:

number theory - Complexity class of comparison of power towers - Mathematics Stack Exchange[^]
Posted
Updated 2-Jun-18 21:45pm
Comments
Patrice T 3-Jun-18 0:56am    
Explaining your problems is also a skill

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900