题目:三角形
来源:“科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛)
解题思路
题目:给定一个整数 ,
,任选
,其中
,使得这 3 个正整数不能构成三角形,求
的最大值。
构成三角形:任意两条边之和大于第三条边。
假设序列 是已排序的,那么
,
。
如果想这 3 个数不能构成三角形,必须满足条件 。
要想 最大,序列应为
,类似斐波那契数列。序列之和需要满足
。
C++代码
#include<iostream>
using namespace std;
using ULL = unsigned long long;
ULL stickCnt(ULL a){
if(a == 1)
return 1;
if(a == 2 || a == 3)
return 2;
ULL cnt = 2;
ULL i = 1;
ULL j = 1;
ULL k = i + j;
a -= 2;
while(k <= a){
++cnt;
a -= k;
i = j;
j = k;
k = i + j;
}
return cnt;
}
int main(){
int t;
cin >> t;
while(t){
ULL a;
cin >> a;
cout << stickCnt(a) << endl;
--t;
}
return 0;
}
京公网安备 11010502036488号