首页 > ACM题库 > HDU-杭电 > HDU 2803-HOJ-The MAX-数学相关-[解题报告]
2014
02-17

HDU 2803-HOJ-The MAX-数学相关-[解题报告]

The MAX

问题描述 :

Giving N integers, V1, V2,,,,Vn, you should find the biggest value of F.

输入:

Each test case contains a single integer N (1<=N<=100). The next line contains N integers, meaning the value of V1, V2….Vn.(1<= Vi <=10^8).The input is terminated by a set starting with N = 0. This set should not be processed.

输出:

Each test case contains a single integer N (1<=N<=100). The next line contains N integers, meaning the value of V1, V2….Vn.(1<= Vi <=10^8).The input is terminated by a set starting with N = 0. This set should not be processed.

样例输入:

2
1 2
0

样例输出:

4017

 

http://acm.hdu.edu.cn/showproblem.php?pid=2803

 

解题思路:一道数学题啦,就是要让你求最大值,我们只要排好V的值,让最后一个V值乘以尽可能最大的Y,剩下的V都乘以1就能最大。

 

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    int a[101];
    __int64 n,i,sum;
    while(scanf("%I64d",&n)!=EOF&&n)
    {
		for(i=0;i<n;i++)
			scanf("%d",&a[i]);
		sort(a,a+n);
		sum=0;
		for(i=0;i<n-1;i++)
			sum+=a[i];
		printf("%I64d/n",sum+a[n-1]*(2010-n));
    }
	return 0;
}

解题参考:http://blog.csdn.net/q3498233/article/details/5369410


  1. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。

  2. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的

  3. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }