473,395 Members | 1,681 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,395 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 1278

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.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.