POJ - 1862 贪心
来源:互联网 发布:python开源小软件 编辑:程序博客网 时间:2024/06/05 14:10
一道水题,贪心
给你n个数字,每两个数字以2sqrt(a*b)的方式合并成一个数,最后n个数字全部合并成一个数字,问最小的结果。
思路:四个数字abcd,按abcd 的顺序合并,最后的结果等于2sqrt(d*2sqrt(c*2sqrt(b*a))),可以看出,最先合并的数字被开根号的次数最多,所以越大的数字应该越靠里面,也就是越大的数字越先合并。
所以先排序,从大到小一个个合并
#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 105;double a[MAXN];int n;int main(){ while(scanf("%d", &n) == 1) { for(int i=0; i<n; i++) scanf("%lf", a+i); sort(a, a+n); double ans = a[n-1]; for(int i=n-2; i>=0; --i) ans = 2 * sqrt(ans*a[i]); printf("%.3f\n", ans); } return 0;}
0 0
- POJ 1862 Stripies (贪心)
- poj 1862 贪心
- POJ 1862 Stripies 【贪心】
- 贪心 POJ 1862
- POJ 1862 (贪心)
- poj 1862 贪心
- POJ - 1862 贪心
- POJ 1862 Stripies 贪心
- 贪心 + 数学 (POJ 1862)
- 【贪心】Stripies POJ 1862
- POJ-1862--贪心---Stripies
- POJ 1862 - Stripies(贪心)
- POJ 1862 Stripies(数学贪心)
- Poj 1862 Stripies【贪心+数学】
- [POJ 1862] Stripies (贪心)
- poj 1862 Stripies简单贪心
- Poj 1862 Stripies【贪心+数学】
- Stripies(POJ 1862, 贪心)
- 面试的一些内容(2)
- Linux Log分析高频命令速查
- Android 获取联系人列表
- 什么是SysWow64
- 让 Web 设计师高效学习代码的 6 个方法
- POJ - 1862 贪心
- 数据库:DDL/DML/DCL/TCL基本概念
- 彩色图像分割方法汇总
- 面试题7:用两个栈实现一个队列
- 2017.2.7 Java有感8
- JDBC(1):基本概念
- 浅识C#
- [POJ3321] Apple Tree 苹果树 - 树状数组
- 【Python】学习笔记——-2.1、Python的数据类型与变量