[Solution] title battery life

Title Description

        Small S bought a new handheld game consoles, the consoles by the two AA batteries. To ensure that a long time to play games, he bought a lot of No. 5 batteries, these batteries of different manufacturers, there are differences in quality, so life is also different, and some can use five hours, and some might only use 3 Hours. Obviously if he is only a two batteries can be used 5 hours a usable three hours, then he can only play three hours of the game, there is a remaining battery power is not available, but if he had more batteries, you can more fully utilize them, he has three such batteries can be used are 3,3,5 hour, he can use two to three hours of energy with a battery, wherein the use of half an hour and then replaced by a 5 hours can be used battery, two and a half hours after the rest of a battery replaced just replaced the battery (the battery can use 2.5 hours), so you can use a total of 5.5 hours, with no waste.

        Now time and the number of batteries that can be used known battery, you find a solution makes use of time as long as possible.

 

Input Format

        Comprising a plurality of sets of input data. Each set of data consists of two lines, the first line is an integer N (2≤N≤1000), represents the number of batteries, the next line is the positive integer N represents the time of the battery can be used.

 

Output Format

        Each output line of the data indicating the time the battery can be used, reserved to a decimal.

 

SAMPLE INPUT

2

3 5

3

3 3 5

 

 

Sample Output

3.0

5.5

 

answer

        Readily occur, when we have the total life of the battery $ $ SUM, time can be used for $ \ frac {sum} {2} $.

        But there is a special case. We set up this pile of battery life the longest battery life of $ maxa $, if $ maxa> sum - maxa $, in fact, that time can be used is less than $ \ frac {sum} {2} $, in fact, is only use ($ sum - maxa $).

#include <iostream>
#include <cstdio>

using namespace std;

int n;
int a[1005];

int main()
{
    int maxa, sum;
    while(~scanf("%d", &n))
    {
        maxa = sum = 0;
        for(register int i = 1; i <= n; ++i)
        {
            scanf("%d", a + i);
            maxa = max(maxa, a[i]);
            sum += a[i];
        }
        if(maxa > sum - maxa) printf("%.1lf\n", (double)sum - maxa);
        else printf("%.1lf\n", (double)sum / 2);;
    }
    return 0;
}
Reference program

 

Guess you like

Origin www.cnblogs.com/kcn999/p/10959073.html