PAT B 1070 rope

Subject description:

Given a section of the rope, you need to put them strung a rope. Each time series, the two ropes is folded telescoped together again as shown in FIG. The rope thus obtained has been treated as another piece of rope, you can talk to another piece of rope in series folded again. After each series, two of the original length of the rope is cut in half.
N given length of rope segment, you need to find the maximum length they strung a rope.

Input formats:

Each input comprises a test. Each test case is given row of the first positive integer N (2≤N≤10 ^ 4); Line 2 give positive integer N, i.e. separated by a space between the original length of the cord segments, digital. All integer no more than 10 ^ 4.

Output formats:

The maximum length of the output line can be strung rope. The result is rounded down to the nearest integer that is taken not to exceed the maximum length.

Sample input:

8
10 15 12 3 4 13 1 15

Sample output:

14

analysis:

A beginning look at the subject, the subject may not understand the meaning.
Each connection cord, to be two fold and then adding the rope, so the length of the rope will be halved. Fold shorter than the folded length of the rope the ropeLower loss, It should be considered to start from the short rope connection. The rope length in ascending order, and then in succession to make up the rope.
Do not forget to decimals may occur when folded, so to adoptFloatOperated.

Code:

#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    scanf("%d", &n);
    int ropes[n];
    for(int i = 0; i < n; i++)
        scanf("%d", &ropes[i]);
    //绳子长度升序排序
    sort(ropes, ropes + n);
    //用浮点数保存
    float max = ropes[0];
    for(int i = 1; i < n; i++)
        max = max / 2.0 + ropes[i] / 2.0;
    printf("%d", (int)max);
    return 0;
}
Published 10 original articles · won praise 0 · Views 96

Guess you like

Origin blog.csdn.net/qq_41421484/article/details/104077624