Advertisement
DMG

Euler's triangle sum

DMG
Mar 27th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. #define NUM_OF_NODES    120
  7. #define NUM_OF_LEAVES   15
  8. #define NULL        0
  9.  
  10. typedef class Node ChildNode;
  11.  
  12. class Node {
  13. private:
  14.     int data;
  15.     ChildNode *left;
  16.     ChildNode *right;
  17.  
  18. public:
  19.     Node(int node_data, ChildNode *left_node, ChildNode *right_node);
  20.  
  21.     void set_data(int dt);
  22.     int get_data() const;
  23.  
  24.     void set_right_child(ChildNode *node);
  25.     ChildNode* get_right_child() const;
  26.  
  27.     void set_left_child(ChildNode *node);
  28.     ChildNode* get_left_child() const;
  29. };
  30.  
  31. class TriangleGraph {
  32. private:
  33.     Node root;
  34.     int max_sum(Node root);
  35.  
  36. public:
  37.     TriangleGraph(Node graph_root): root(graph_root) {};
  38.     int max_sum();
  39. };
  40.  
  41. Node::Node(int node_data, ChildNode *left_node, ChildNode *right_node):
  42.     data(node_data),
  43.     right(right_node),
  44.     left(left_node)
  45.     {}
  46.  
  47. void Node::set_data(int dt) {
  48.     Node::data = dt;
  49. }
  50.  
  51. int Node::get_data() const {
  52.     return Node::data;
  53. }
  54.  
  55. void Node::set_right_child(ChildNode *node) {
  56.     Node::right = node;
  57. }
  58.  
  59. ChildNode* Node::get_right_child() const {
  60.     return Node::right;
  61. }
  62.  
  63. void Node::set_left_child(ChildNode *node) {
  64.     Node::left = node;
  65. }
  66.  
  67. ChildNode* Node::get_left_child() const {
  68.     return Node::left;
  69. }
  70.  
  71. int TriangleGraph::max_sum (Node node) {
  72.  
  73.     if (node.get_left_child()->get_left_child() == 0) {
  74.         return node.get_data() + max(node.get_left_child()->get_data(), node.get_right_child()->get_data());
  75.     }
  76.  
  77.     return max(node.get_data() + max_sum(*node.get_left_child()), node.get_data() + max_sum(*node.get_right_child()));
  78. }
  79.  
  80. int TriangleGraph::max_sum() {
  81.     return TriangleGraph::max_sum(TriangleGraph::root);
  82. }
  83.  
  84. int max(int a, int b) {
  85.     return (a > b) ? a : b;
  86. }
  87.  
  88. int main() {
  89.  
  90.     vector<Node> triangle_nodes;
  91.     unsigned level = 1, nd_pos = 1;
  92.  
  93.     unsigned triangle[] = {
  94.                   75,
  95.                 95, 64,
  96.                   17, 47, 82,
  97.                        18, 35, 87, 10,
  98.                      20, 4 , 82, 47, 65,
  99.                    19, 1 , 23, 75, 3 , 34,
  100.                  88, 2 , 77, 73, 7 , 63, 67,
  101.                99, 65, 4 , 28, 6 , 16, 70, 92,
  102.              41, 41, 26, 56, 83, 40, 80, 70, 33,
  103.            41, 48, 72, 33, 47, 32, 37, 16, 94, 29,
  104.          53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14,
  105.        70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57,
  106.      91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48,
  107.    63, 66, 4 , 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31,
  108.  4 , 62, 98, 27, 23, 9 , 70, 98, 73, 93, 38, 53, 60, 4 , 23
  109. };
  110.  
  111.     for (int i = 0; i < NUM_OF_NODES; ++i) {
  112.         Node nd = Node(triangle[i], NULL, NULL);
  113.         triangle_nodes.push_back(nd);
  114.     }
  115.  
  116.     for (int i = 0; i < NUM_OF_NODES - NUM_OF_LEAVES; ++i) {
  117.         triangle_nodes[i].set_left_child(&triangle_nodes[nd_pos++]);
  118.         triangle_nodes[i].set_right_child(&triangle_nodes[nd_pos++]);
  119.  
  120.         if (i+1 < level*(level+1)/2)
  121.             --nd_pos;
  122.         else
  123.             ++level;
  124.     }
  125.  
  126.     TriangleGraph pyramid(triangle_nodes[0]);
  127.     cout << pyramid.max_sum() << endl;
  128.  
  129.     system("PAUSE");
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement