-
题目大意:将一些小绳子系成一条绳子,每次只能系两根,并且每系一次,两根绳子的长度减半。输出最大的绳子长度,最后结果四舍五入成不大于最大长度的整数(即int(result))。
-
思路:从小系到大能保留最长的绳子,然后输出最后的结果。
- 把segment排序
- (rope + segment) / 2
-
知识点:
- sort
- vector
-
代码:
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int chain(float rope, int segment){ return (rope + float(segment)) / 2.0; } int main(){ int n, i; scanf("%d", &n); vector<int> segment(n); for(i = 0; i < n; i++){ scanf("%d", &segment[i]); } sort(segment.begin(), segment.end()); float rope = float(segment[0]); for(i = 1; i < n; i++){ rope = chain(rope, segment[i]); } printf("%d\n", int(rope)); return 0; }
-
总结:
- 注意float变量的定义。
【PAT】1125 Chain the Ropes (25 分)
最新推荐文章于 2023-09-19 12:27:21 发布