PTA 乙级 1070 结绳 (25分) C++

 刚开始理解错题意了/捂脸,以为让输出两段折叠后最长的绳子

题的本意是,把n条绳子按照题的规律折叠后如何最长/捂脸

思路:输入后用sort对所有输入的绳子的长度进行排序(从小到大),让长的绳子尽量少的折叠,用double类型的变量存储结果,最后向下取整输出即可

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<cmath>
 5 
 6 using namespace std;
 7 
 8 int main() {
 9     int n = 0;
10     double max;
11     cin >> n;
12     vector<int> rope(n);
13     for (int i = 0; i < n; ++i) cin >> rope[i];
14     sort(rope.begin(), rope.end());                //从小到大排序,让最长的绳子尽量少的折叠
15     max = rope[0];
16     for (int i = 1; i < n; ++i)max = (max + rope[i]) / 2;
17     cout << floor(max);                            //向下取整
18     return 0;
19 }

原文地址:https://www.cnblogs.com/SCP-514/p/13637646.html