Candies

Candies

题目描述:We have a 2×N grid. We will denote the square at the i-th row and j-th column (1≤i≤2, 1≤j≤N) as (i,j).
You are initially in the top-left square, (1,1). You will travel to the bottom-right square, (2,N), by repeatedly moving right or down.
The square (i,j) contains Ai,j candies. You will collect all the candies you visit during the travel. The top-left and bottom-right squares also contain candies, and you will also collect them.
At most how many candies can you collect when you choose the best way to travel?
Constraints
1≤N≤100
1≤Ai,j≤100 (1≤i≤2, 1≤j≤N)
输入:
Input is given from Standard Input in the following format:
N
A1,1 A1,2 … A1,N
A2,1 A2,2 … A2,N
输出:
Print the maximum number of candies that can be collected.
样例输入:
5
3 2 2 4 1
1 2 2 2 1
样例输出:
14
题目大意:有两行数,要从左上角走到右下角,只能向右或向下走,每走过一个格子,就加上这个格子上的数,问最多能加到多少。
这两天训练赛难度见长,签到题都这样了(虽然还是很水)。。。应用穷举法,一排有n个数,则一定要走过n+1个格子。其中至少一个在上边,一个在下边,可以从第一个开始转入第二行,也可以从第二个开始……至少要在第n个转入,所以说明有n种情况,穷举出来,取出其中最大的即可。

#include<bits/stdc++.h>
using namespace std;
bool cmp(int x,int y)
{
    return x>y;
}
int main()
{
    int n,sum=0;
    scanf("%d",&n);
    int a[2][101],b[101]={0},i,j;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[0][i]);
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[1][i]);
    }
    sum=sum+a[0][0]+a[1][n-1];//一定经过左上角和右下角,所以可以提前加上
    for(i=0;i<n-1;i++)//从第i个开始转入第二行
    {
        for(j=i;j<n-1;j++)
        {
            b[i]=b[i]+a[1][j];//加第二行数据
        }
        for(j=0;j<i;j++)
        {
            b[i]=b[i]+a[0][j+1];//加第一行数据
        }
    }
    sort(b,b+(n-1),cmp);
    sum=sum+b[0];//加上和最大的那条路的数据
    printf("%d\n",sum);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/soul_mingling/article/details/88378602