【杭电oj】5620 - KK's Steel(递推)

KK's Steel

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


Problem Description
Our lovely KK has a difficult mathematical problem:he has a  N(1N1018)  meters steel,he will cut it into steels as many as possible,and he doesn't want any two of them be the same length or any three of them can form a triangle.
 

Input
The first line of the input file contains an integer  T(1T10) , which indicates the number of test cases.

Each test case contains one line including a integer  N(1N1018) ,indicating the length of the steel.
 

Output
For each test case, output one line, an integer represent the maxiumum number of steels he can cut it into.
 

Sample Input
   
   
   
   
1 6
 

Sample Output
   
   
   
   
3
Hint
1+2+3=6 but 1+2=3 They are all different and cannot make a triangle.
 

Source
BestCoder Round #71 (div.2)


类似斐波那契数列,递推过去就行了,时间上足够,注意数据类型就行了。

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;
long long f[1000000];
int main()
{
	f[1] = 1;
	f[2] = 2;
	for (int i = 3 ; i <= 1000 ; i++)
		f[i] = f[i-1] + f[i-2];
	int u;
	long long n;
	long long sum;
	scanf ("%d",&u);
	while (u--)
	{
		scanf ("%lld",&n);
		sum = 0;
		for (int i = 1 ;; i++)
		{
			sum += f[i];
			if (sum == n)
			{
				printf ("%d\n",i);
				break;
			}
			else if (sum > n)
			{
				printf ("%d\n",--i);
				break;
			}
		}
	}
	return 0;
}


你可能感兴趣的:(【杭电oj】5620 - KK's Steel(递推))