点击链接PAT乙级-AC全解汇总
题目:
给定一段一段的绳子,你需要把它们串成一条绳。每次串连的时候,是把两段绳子对折,再如下图所示套接在一起。这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连。每次串连后,原来两段绳子的长度就会减半。
给定 N 段绳子的长度,你需要找出它们能串成的绳子的最大长度。
输入格式:
每个输入包含 1 个测试用例。每个测试用例第 1 行给出正整数 N (2≤N≤104 );第 2 行给出 N 个正整数,即原始绳段的长度,数字间以空格分隔。所有整数都不超过104 。
输出格式:
在一行中输出能够串成的绳子的最大长度。结果向下取整,即取为不超过最大长度的最近整数。
输入样例:
8
10 15 12 3 4 13 1 15
- 1
- 2
输出样例:
14
- 1
我的代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//有的时候题目是一起做的,所以会有不需要的头文件
int main()
{
int N;
cin>>N;
multiset<double>number;
for(int i=0;i<N;i++)
{
int t;
cin>>t;
number.insert(t);
}
while(number.size()>1)
{
int a=*number.begin();
number.erase(number.begin());
int b=*number.begin();
number.erase(number.begin());
number.insert(1.0*a/2+1.0*b/2);
}
int a=*number.begin();
printf("%d",a);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
只要思路对了就会觉得这道题目很简单:每次找最短的两根绳连起来,这样损失的长度就最少
评论记录:
回复评论: