The Triangle
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
4
-
描述
-
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
(Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.-
输入
- Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99. 输出
- Your program is to write to standard output. The highest sum is written as an integer. 样例输入
-
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
样例输出
-
30
-
题目大意大概是从上往下走,起点是顶点,终点是底边任意一点,每一步只能向下走或者斜着向右下角走
#include<iostream>
using namespace std;
int num[105][105];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
cin>>num[i][j];
for(int i=n-1;i>=1;i--)
for(int j=1;j<=i;j++)
num[i][j]=num[i][j]+max(num[i+1][j],num[i+1][j+1]);
cout<<num[1][1]<<endl;
}
从下往上遍历存储最大值
NYOJ613
免费馅饼
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
3
-
描述
-
都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不 掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只 能在小径上接。由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的 范围内接住坠落的馅饼。现在给这条小径如图标上坐标:
为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在0-10这11个位置。开始时gameboy站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置中其中一个位置上的馅饼。问gameboy最多可能接到多少个馅饼?(假设他的背包可以容纳无穷多个馅饼)-
输入
- 输入数据有多组。每组数据的第一行为以正整数n(0<n<100000),表示有n个馅饼掉在这条小径上。在结下来的n行中,每行有两个整数x,T(0<T<100000),表示在第T秒有一个馅饼掉在x点上。同一秒钟在同一点上可能掉下多个馅饼。n=0时输入结束。 输出
-
每一组输入数据对应一行输出。输出一个整数m,表示gameboy最多可能接到m个馅饼。
提示:本题的输入数据量比较大,建议用scanf读入,用cin可能会超时。
样例输入
-
6 5 1 4 1 6 1 7 2 7 2 8 3 0
样例输出
-
4
#include<iostream>
#include<cstring>
using namespace std;
int num[100010][15];
int max(int x,int y,int z)
{
if(x<y)
x=y;
if(x<z)
x=z;
return x;
}
int main()
{
int n,x,T,l;
while(cin>>n&&n!=0)
{
l=0;
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
{
cin>>x>>T;
num[T][x+1]++;
if(l<T)
l=T;
}
for(int i=l;i>=0;i--)
for(int j=1;j<=11;j++)
num[i][j]+=max(num[i+1][j],num[i+1][j+1],num[i+1][j-1]);
/*boy有三种选择,在原位置,向前或向后,选择那个位置
要看在哪可以接到更多的饼,相当于三路数塔*/
cout<<num[0][6]<<endl;/*从下往上遍历后,回到
boy的起始位置*/
}
}