423,851 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,851 IT Pros & Developers. It's quick & easy.

compare towers

P: 3
my code fails the tests
Ten towers are given. You need to compare them. exponentiation occurs from right to left a ^ (a ^ (a ^ a)). At the end, print their indexes in ascending order. Here is my code but it is incorrect.
input
10 // number of towers
4 2 2 2 2 2 // 4 The first number in a line is not an element of the tower, it is the //number of elements in it minus one.
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


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; // the tower room
  11.     int height; // the height of the tower
  12.     double val[11]; // content
  13.     double cache[11]; // cache to speed up the calculation
  14.  
  15.     // Designer
  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.     // Triple logarithm of the top 3 levels
  25.     double head(int level) {
  26.         if(cache[level] == 0) cache[level] = log10(log10(val[level])) + log10(val[level + 1]) * val[level + 2];
  27.         return cache[level];
  28.     }
  29.  
  30.     // The calculation of the tops until intermeddle in double
  31.     void normalize() {
  32.         while(height > 1 && (log10(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.     // Output for debugging
  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. // comparison of two towers
  57. bool compare(tower_t& t1, tower_t& t2) {
  58.     // floor with which to compare the last three levels
  59.     int level = ((t1.height > t2.height) ? t1.height : t2.height) - 3;
  60.     if (level < 0) level = 0;
  61.     if(t1.height == t2.height) { // if the towers are of the same height, compare by floor
  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) { // the tops of the towers coincided below 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(int argc, char**argv)
  75. {
  76.     // Reading job
  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; // if level of <= 1 the higher not to read
  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.     // Sort
  99.     sort(towers, towers + cnt, compare);
  100.     // The output
  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 15 '18 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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