# Recursive Football Problem

By: a guest on May 17th, 2012  |  syntax: C++  |  size: 2.15 KB  |  hits: 56  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. #include "stdafx.h"
2. #include <iostream>
3. #include <vector>
4. #include <stdio.h>
5.
6. using namespace std;
7.
8. struct Node
9. {
10.         vector<Node> nodes;
11.         string score;
12. };
13.
14. struct Score
15. {
16.         int value;
17.         string str;
18. };
19.
20. bool getAllScores(int score, Node *top);
21. void printScores(Node *top, string builder);
22.
23. Score allScores[] =
24. {
25.         { 2, "Safety" },
26.         { 3, "FieldGoal" },
27.         { 6, "Touchdown" },
28.         { 7, "Touchdown w/ Extra Point" },
29.         { 8, "Touchdown w/ 2 Point Conversion" }
30. };
31.
32.
33. int main()
34. {
35.         Node top;
36.
37.         // show the welcome message, and ask for input
38.         printf("Welcome to Adam's sick football program!\n\n" \
39.                 "This program will display all the possible ways to get a certain football score.\n\n" \
40.                 "What score would you like to see? ");
41.
42.         // read the score input
43.         int score;
44.         scanf("%d", &score);
45.
46.         if (score < 2)
47.         {
48.                 printf("\nScore must be at least 2!\n");
49.
50.                 // freeze the program, so it doesn't close
51.                 scanf("%c", &score);
52.                 scanf("%c", &score);
53.                 return 0;
54.         }
55.
56.         printf("\n");
57.
58.         // get all of the scores, and then print them out
59.         getAllScores(score, &top);
60.         printScores(&top, "");
61.
62.         // freeze the program, so it doesn't close
63.         scanf("%c", &score);
64.         scanf("%c", &score);
65.
66.         return 0;
67. }
68.
69. bool getAllScores(int score, Node *top)
70. {
71.         if (score == 0)
72.                 return true;
73.         else if (score <= 1)
74.                 return false;
75.
76.         // loop through all the possible football scores
77.         for (int i = 0; i < 5; i++)
78.         {
79.                 // create a new node with the string being the score
80.                 Node n;
81.                 n.score = allScores[i].str;
82.
83.                 // if the score will work, then add the node into the tree
84.                 if (getAllScores(score - allScores[i].value, &n))
85.                         top->nodes.push_back(n);
86.         }
87.
88.         return true;
89. }
90.
91. void printScores(Node *top, string builder)
92. {
93.         // for some reason VS was saying that you can't use the '+' operator
94.         // to concatenate strings so I had to use append instead, it LIES
95.
96.         if (top->nodes.size() == 0)
97.         {
98.                 string temp = builder;
99.                 temp.append(top->score);
100.
101.                 printf("%s\n", temp.c_str());
102.         }
103.
104.         for (int i = 0; i < top->nodes.size(); i++)
105.         {
106.                 string temp = builder;
107.                 temp.append(top->score);
108.                 temp.append(" ");
109.                 printScores(&top->nodes.at(i), temp);
110.         }
111. }