Du! Number triangle w ww www set !, Ww
Beep! A collection of all number triangles!
Number triangle! To!
Number triangle W! To!
Digital triangle WW! To!
Digital triangle WWW! To!
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------
A digital triangle is a typical exercise of gray and gray water,
1220 digital triangle
Time Limit: 1 s space limit: 128000 KB title level: Gold Title Description
Description
Starting from the top, you can choose to go left or right at each node until it reaches the bottom layer. You need to find a path to maximize the value of the path.
Input description
Input Description
The first line is the number of floors N (1 <= N <= 100 ).
Starting from the second row, there are one or more integers in the number tower chart, indicating the value of the layer node. There are N rows in total.
Output description
Output Description
Maximum output value.
Sample Input
Sample Input
5
13
11 8
12 7 26
6 14 15 8
12 7 13 24 11
Sample output
Sample Output
86
Let's talk about the Code:
# Include <iostream> # include <cstdio> # include <algorithm> # include <cmath> using namespace std; int a [110] [110] = {0}, n; int main () {cin> n; for (int I = 1; I <= n; I ++) for (int j = 1; j <= I; j ++) cin> a [I] [j]; for (int I = n-1; I> = 1; -- I) for (int j = 1; j <= I; j ++) a [I] [j] = max (a [I] [j] + a [I + 1] [j], a [I] [j] + a [I + 1] [j + 1]); cout <a [1] [1]; return 0 ;}
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------
The focus is on the last three conversion questions. Let's skip the number triangle W and first look at the number triangle WW and WWW;
In fact, these two questions are also water problems. first look at the question:
193 digital triangle WW
Time Limit: 1 s space limit: 32000 KB title level: Diamond Title Description
Description
A digital Triangle must go through a certain point to make the distance and maximum
Input description
Input Description
Row n, indicating n
2nd to n + 1 actions each weight
The program must go through the n div 2, n div 2 point
Output description
Output Description
Maximum Value
Sample Input
Sample Input
2
1
1 1
Sample output
Sample Output
2
Data range and prompt
Data Size & Hint
N <= 25
If the request must go through the vertex (n/2, n/2), we only need to add a large number to the value of this vertex,
In the end, you only need to subtract the value you added to the result, and directly add the code to the question:
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>using namespace std;int a[110][110]={0},n;bool f[110][110]={0};int main(){cin>>n;for (int i=1;i<=n;i++)for (int j=1;j<=i;j++){cin>>a[i][j];}a[n/2][n/2]+=5000;for (int i=n-1;i>=1;--i)for (int j=1;j<=i;j++)a[i][j]=max(a[i][j]+a[i+1][j],a[i][j]+a[i+1][j+1]);cout<<a[1][1]-5000;return 0;}
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------
2198 digital triangle WWW
Time Limit: 1 s space limit: 32000 KB title level: Diamond Title Description
Description
A digital Triangle must go through a certain point to make the distance and maximum
Input description
Input Description
Row n, indicating n
2nd to n + 1 actions each weight
Number of n + 2 actions x, y indicates the vertex that must pass
Output description
Output Description
Maximum Value
Sample Input
Sample Input
2
1
1 1
1 1
Sample output
Sample Output
2
Data range and prompt
Data Size & Hint
N <= 25
The idea of this question is the same as that of WW, but the point that must be taken is changed to (x, y ),
Code:
# Include <iostream> # include <cstdio> # include <algorithm> # include <cmath> using namespace std; int a [110] [110] = {0}, n, x, y; bool f [110] [110] = {0}; int main () {cin> n; for (int I = 1; I <= n; I ++) for (int j = 1; j <= I; j ++) cin> a [I] [j]; cin> x> y; a [x] [y] + = 5000; for (int I = n-1; I> = 1; -- I) for (int j = 1; j <= I; j ++) a [I] [j] = max (a [I] [j] + a [I + 1] [j], a [I] [j] + a [I + 1] [j + 1]); cout <a [1] [1]-5000; return 0 ;}
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------
The most important question was: Digital triangle W. At the beginning, I did not come up with a simple practice. Later, my beloved xuejie taught me, because after all, I am also a programmer... explain the specific practices in the code.
2189 digital triangle W
Time Limit: 1 s space limit: 32000 KB title level: Gold Title Description
Description
Digital triangle
The maximum number of mod 100 is required.
Input description
Input Description
Row n, indicating n
2nd to n + 1 actions each weight
Output description
Output Description
Maximum mod 100
Sample Input
Sample Input
2
1
99 98
Sample output
Sample Output
99
Data range and prompt
Data Size & Hint
N <= 25
Code:
// I am using the reverse push idea. The reverse push idea is the same as the actual one. Just change the code here.
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
Using namespace std;
Int n, a [26] [26] = {0 };
Bool f [26] [26] [1, 100] = {0 };
Int main ()
{
Cin> n;
For (int I = 1; I <= n; I ++) // read data
For (int j = 1; j <= I; j ++)
Cin> a [I] [j];
For (int I = 1; I <= n; I ++) // the code in this step is to save all the numbers in the last row.
F [n] [I] [a [n] [I] = true;
For (int I = n-1; I> = 1; -- I) // here is the main process. Thought: Save all possibilities mod100,
For (int j = 1; j <= I; j ++)
For (int k = 0; k <= 99; k ++)
{
If (f [I + 1] [j] [k])
F [I] [j] [(k + a [I] [j]) % 100] = true;
If (f [I + 1] [j + 1] [k])
F [I] [j] [(k + a [I] [j]) % 100] = true;
}
For (int k = 99; k> = 0; -- k) // find out the greatest possibility among all possibilities. for output, this question is AC,
If (f [1] [1] [k])
{
Cout <k;
Return 0;
}
}