HDU 5914 Triangle(水题)

Triangle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 199    Accepted Submission(s): 141


Problem Description
Mr. Frog has n sticks, whose lengths are 1,2, 3 n respectively. Wallice is a bad man, so he does not want Mr. Frog to form a triangle with three of the sticks here. He decides to steal some sticks! Output the minimal number of sticks he should steal so that Mr. Frog cannot form a triangle with
any three of the remaining sticks.
 

Input
The first line contains only one integer T ( T20 ), which indicates the number of test cases.

For each test case, there is only one line describing the given integer n ( 1n20 ).
 

Output
For each test case, output one line “Case #x: y”, where x is the case number (starting from 1), y is the minimal number of sticks Wallice should steal.
 

Sample Input
  
  
3 4 5 6
 

Sample Output
  
  
Case #1: 1 Case #2: 1 Case #3: 2
 

Source

题意:给出一个数字n,问1-n序列中最少去掉几个数字可使得剩下的序列中任意三个数字均不能组成三角形三边。
思路:一开始发现数据规模很小,想找规律,但发现前面样例去掉的数字不会影响后面样例去掉的数字。之后又想打表,每次暴力求出可以构成三角形的三边长度,统计出现频率最多的数字去掉之后再暴力求剩下的再统计再去掉,但这样很难处理当多个数字出现频率一样时的情况。其实中间通过1123这样的数字联想到了斐波那契数列,但没有继续想下去。最后发现真的与斐波那契数列有关,在此引用一下骆骆的证明:
充分性:斐波拉契数列的任意项li,lj,lk均不构成三角形。
由于任意三角形三边a、b、c(a < b < c)均需要满足a + b < c。而对于斐波拉契数列中任意的边lk - 2 + lk - 1 = max(li + lj, i,j < k) = lk,因此不可能存在构成三角形。
必要性:在斐波拉契数列中增加任意一项,则可以构成至少一个三角形。
假设添加边长m在li和li + 1之间,由于li + li + 1 = li + 2,因此 li + m > li + 2,因此至少构成一个三角形。

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
int a[25]={0,0,0,0,1,1,2,3,3,4,5,6,7,7,8,9,10,11,12,13,14};


int main(){
	
	int t;
	scanf("%d",&t);
	for(int cas=1;cas<=t;cas++){
		int i;
		scanf("%d",&i);
		printf("Case #%d: %d\n",cas,a[i]);
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值