HackerRank - maximum-perimeter-triangle 【水】
题意
给出一系列数字,判断其中哪三个数字可以构成一个三角形,如果有多个,输出周长最大的那个,如果没有输出 - 1
思路
数据较小,所有情况FOR一遍 判断一下
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
using namespace std;
typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 50 + 5;
const int MOD = 1e9 + 7;
LL a[maxn];
bool judge(LL b[])
{
if (b[0] + b[1] > b[2])
return true;
return false;
}
int main()
{
int n;
cin >> n;
LL b[3], ans[3];
LL MAX = MINN;
for (int i = 0; i < n; i++)
scanf("%lld", &a[i]);
sort(a, a + n);
int i, j, k;
for (i = 0; i < n; i++)
{
for(j = i + 1; j < n; j++)
{
for (k = j + 1; k < n; k++)
{
b[0] = a[i];
b[1] = a[j];
b[2] = a[k];
sort(b, b + 3);
if (judge(b))
{
LL temp = b[0] + b[1] + b[2];
if (temp > MAX)
{
for (int l = 0; l < 3; l++)
ans[l] = b[l];
MAX = temp;
}
}
}
}
}
if (MAX != MINN)
{
printf("%lld %lld %lld\n", ans[0], ans[1], ans[2]);
}
else
cout << -1 << endl;
}
HackerRank - maximum-perimeter-triangle 【水】的更多相关文章
- LeetCode 976. 三角形的最大周长(Largest Perimeter Triangle) 33
976. 三角形的最大周长 976. Largest Perimeter Triangle 题目描述 给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的.面积不为零的三角形的最大周长. ...
- 【Leetcode_easy】976. Largest Perimeter Triangle
problem 976. Largest Perimeter Triangle solution: class Solution { public: int largestPerimeter(vect ...
- [Swift]LeetCode976. 三角形的最大周长 | Largest Perimeter Triangle
Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, ...
- LeetCode 976 Largest Perimeter Triangle 解题报告
题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- 119th LeetCode Weekly Contest Largest Perimeter Triangle
Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, ...
- 【leetcode】976. Largest Perimeter Triangle
题目如下: Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- 976. Largest Perimeter Triangle
Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, ...
- 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- uva 11059 maximum product(水题)——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK
- Codeforces Beta Round #6 (Div. 2 Only) A. Triangle 水题
A. Triangle 题目连接: http://codeforces.com/contest/6/problem/A Description Johnny has a younger sister ...
随机推荐
- Thinkphp5 使用命令行模式(cli模式)
Tp5的cli模式跟Tp3.2变化较大,有自己的一套方式,在这里做个搬运工,把Tp文档的东西搬运过来,方便大家. 原出处截图 创建自定义命令行 第一步,配置command.php文件,目录在appli ...
- 桥接模式和NAT模式差别
bridged networking(桥接模式) 在这样的模式下.VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机,它能够訪问网内不论什么一台机器. 在桥接模式下.你须要手工为虚 ...
- java 签名类 Signature
java.security类 Signature java.lang.Object java.security.SignatureSpi java.security.Signature public ...
- WPF-Binding对数据的检验
设置Binding的ValidationRules属性对Binding进行检验 <StackPanel> <TextBox x:Name="txtAge" Fon ...
- ios - 高效,准确的网络检测
//网络检测 // 1.创建网络监听者管理者对象 AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager share ...
- 集合 Vector ArrayList 集合一
集合是存储对象的,与对象数组不同,集合可以自动的扩大自己的容量,像StringBuffer一样,存储的对象类型可以不一致,(object数组). 方法: add();remove();get();se ...
- 5、easyUI-菜单与按钮
列出源码:求解 <html> <head> <meta http-equiv="Content-Type" content="text/ht ...
- shell脚本中出现^M
在Windows中编辑的shell脚本,传到linux系统中,在末尾发现出现了很多^M字符 1.问题分析 在windows下使用notepad++写的脚本上传到Linux下,在使用vim编辑的时候我们 ...
- LeetCode Problem 2:Two Sum
描述: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- 【BZOJ2803】[Poi2012]Prefixuffix 结论题
[BZOJ2803][Poi2012]Prefixuffix Description 对于两个串S1.S2,如果能够将S1的一个后缀移动到开头后变成S2,就称S1和S2循环相同.例如串ababba和串 ...