473,287 Members | 3,240 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

compare towers

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
0 1276

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

Similar topics

13
by: MrCoder | last post by:
Hey guys, my first post on here so I'll just say "Hello everbody!" Ok heres my question for you lot. Is there a faster way to compare 1 byte array to another? This is my current code //...
51
by: maloov | last post by:
I have a pretty tough assignment for beginner like me & i'm seeking help please here is the assign In this assignment, you will be guided to complete the program skeleton provided to you in...
2
by: sivamca | last post by:
Hi sir this is siva, i need on coding of towers of honoi problem in c language.The out will be like this.... it will show graphical movement of discs from one tower to another tower according to...
6
by: poopsy | last post by:
hi guys cud some1 explain to me the towers of hanoi code, the source code is available evrywhr. i've been trying to understand it bt cant. can some1 plz help me i hav even tried to "trace" it but i...
26
by: neha_chhatre | last post by:
can anybody tell me how to compare two float values say for example t and check are two variables declared float how to compare t and check please help me as soon as possible
0
by: Brian Whatcott | last post by:
On Thu, 13 Mar 2008 01:17:40 -0700 (PDT), schoenfeld.one@gmail.com wrote: A very fine case for... what ever it is you are making a case for, I'm sure, and as relevant to any of the other...
1
by: schoenfeld.one | last post by:
In this article we show that "top-down" controlled demolition accurately accounts for the collapse times of the World Trade Center towers. A top-down controlled demolition can be simply...
2
by: sunyboy | last post by:
hi I need a programe "Towers of Hanoi without use recessive function" in c++ Please help. Thanks for your time.
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.