By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,601 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,601 IT Pros & Developers. It's quick & easy.

Finalize the program

P: 3
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


Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <algorithm>
  3.  
  4. #include <stdio.h>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8. class tower_t {
  9. public:
  10.     int num; 
  11.     int height; 
  12.     double val[11]; // содержимое
  13.     double cache[11]; // кэш для ускорения расчета
  14.  
  15.     // Конструктор
  16.     tower_t() {
  17.         for (int i = 0; i < 11; i++) {
  18.             val[i] = 1;
  19.             cache[i] = 0;
  20.         }
  21.         height = 0;
  22.     }
  23.  
  24.     // Тройной логарифм верхних 3-х уровней
  25.      double head(int level) {
  26.         if(cache[level] == 0) cache[level] = (log2(log2(val[level])) + log2(val[level + 1]) * val[level + 2]);
  27.         return cache[level];
  28.     }
  29.  
  30.     // Вычисление верхушки до тех пока влазит в double
  31.     void normalize() {
  32.         while(height > 1 && (log2(val[height - 2]) * val[height - 1]) < 50) {
  33.             val[height - 2] = pow(val[height - 2], val[height - 1]);
  34.             val[height - 1] = 1;
  35.             height--;
  36.         }
  37.     }
  38.  
  39.     // Вывод для отладки
  40.     void print() {
  41. #ifdef _DEBUG
  42.         printf("%2d: {", num);
  43.         for (int i = 0; i < height; i++) {
  44.             if (i > 0) printf(", ");
  45.             if(val[i] < 1000000000) {
  46.                 printf("%0.0f", val[i]);
  47.             } else {
  48.                 printf("%0.3e", val[i]);
  49.             }
  50.         }
  51.         printf("}\n");
  52. #endif
  53.     }
  54. };
  55.  
  56. // сравнение двух 
  57. bool compare(tower_t& t1, tower_t& t2) {
  58.     // этаж с которого сравнивать три последних уровня
  59.     int level = ((t1.height > t2.height) ? t1.height : t2.height) - 3;
  60.     if (level < 0) level = 0;
  61.     if(t1.height == t2.height) { // если  одной высоты, сравниваем поэтажно
  62.         for (int i = t1.height - 1; i >= 0; i--) {
  63.             if (abs(t1.val[i] - t2.val[i]) > (t1.val[i] * 1e-14)) {
  64.                 if (i < level) { // верхи  совпали ниже level
  65.                     return t1.val[i] < t2.val[i];
  66.                 }
  67.                 break;
  68.             }
  69.         }
  70.     }
  71.     return t1.head(level) < t2.head(level);
  72. }
  73.  
  74. int main()
  75. {
  76.     // Считывание задания
  77.     ifstream in ("input.txt");
  78.     int cnt;
  79.     in >> cnt;
  80.     tower_t* towers = new tower_t[cnt];
  81.     for (int i = 0; i < cnt; i++) {
  82.         int len;
  83.         in >> len;
  84.         towers[i].num = i + 1;
  85.         bool write = true;
  86.         for (int j = 0; j <= len; j++) {
  87.             int val;
  88.             in >> val;
  89.             if (val <= 1) write = false; // если уровень <= 1 то выше не читать
  90.             if(write) {
  91.                 towers[i].val[j] = val;
  92.                 towers[i].height = j + 1;
  93.             }
  94.         }
  95.         towers[i].print();
  96.         towers[i].normalize();
  97.     }
  98.     // Сортировка
  99.     sort(towers, towers + cnt, compare);
  100.     // Вывод результата
  101.     ofstream out("output.txt");
  102.     for (int i = 0; i < cnt; i++) {
  103.         out << towers[i].num << " ";
  104.         towers[i].print();
  105.     }
  106.     out << endl;
  107.     out.close();
  108.     delete[] towers;
  109.     return 0;
  110. }
Jun 3 '18 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
What is your question, exactly?
Jun 3 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.