PAT B1070 rope (25 minutes)

Topic links : https://pintia.cn/problem-sets/994805260223102976/problems/994805264706813952

Title 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.
Here Insert Picture Description
N given length of rope segment, you need to find the maximum length they strung a rope.

Input
Each input comprises a test. Each test case is given row of the first positive integer N (≦ n ≦ 10 . 4 ); the N line 2 gives a positive integer, i.e., separated by spaces between the original length of the cord segments, digital. All integer not exceed 10 4 .

Output
outputs the maximum length of the rope can be strung together in a row. The result is rounded down to the nearest integer that is taken not to exceed the maximum length.

Sample input
. 8
10. 4 15. 3 12 is 13 is 15. 1

Sample output
14

Code

#include <cstdio>
#include <algorithm>
using namespace std;

int main() {
	int n, a[100010];
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	double sum = a[0];		//当只有一条绳子的时候不需要对折!(一个case)
	for(int i = 1; i < n; i++)
		sum = sum / 2 + a[i] * 1.0 / 2;
	printf("%d\n", (int)sum);
	return 0;	
}
Published 327 original articles · won praise 12 · views 20000 +

Guess you like

Origin blog.csdn.net/Rhao999/article/details/105182231