POJ 1003 Hangover

原创 2018年04月16日 23:06:26

Hangover
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 133565 Accepted: 64951

Description

How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We're assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cards overhang by 1/2 + 1/3 + 1/4 + ... + 1/(n + 1) card lengths, where the top card overhangs the second by 1/2, the second overhangs tha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n + 1). This is illustrated in the figure below.



Input

The input consists of one or more test cases, followed by a line containing the number 0.00 that signals the end of the input. Each test case is a single line containing a positive floating-point number c whose value is at least 0.01 and at most 5.20; c will contain exactly three digits.

Output

For each test case, output the minimum number of cards necessary to achieve an overhang of at least c card lengths. Use the exact output format shown in the examples.

Sample Input

1.00
3.71
0.04
5.19
0.00

Sample Output

3 card(s)
61 card(s)
1 card(s)
273 card(s)

Source



    分析 : 首先输入可以使用动态数组vector, 当输入为0.00的时候就停下, 之后就是一个简单的调和级数计算问题,从1/2+一直到1/n 找到和比输入的数字大的, 输出此时的n值. 代码如下

#include <iostream>
#include <vector>
using namespace std;

int judge(double num);

int main()
{
	vector<double> n;
	double  temp;
	int result;
	
	while(cin >> temp && temp != 0.00)
	{
		n.push_back(temp);
	} 
		
	for(int i = 0; i < n.size(); i++)
	{
		result = judge(n[i]);
		cout << result << " card(s)"<<endl;;
	}


} 

int judge(double num)
{
	double count = 0.00;
	
	for(int i = 2; i < 300; i++)
	{
		count += (double)1/i;
		//cout << "count: " << count << endl;        //test
		if(count >= num)
		return i - 1;
	}
}
此外, 因为此题AC了, 我就没有多管了, 因为嵌套了两层for循环, 当数据量过大的时候, 可以先对输入数据进行快速排序, 然后按照排序的大小, 每当匹配到一个count比输入大的值, 就输出, 这样时间复杂度只有nlogn.

POJ1003-Hangover

转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1309071598 打发时间顺手A的水题= = 没啥好说的。。。算是增强一下做难题前的信心O...
  • lyy289065406
  • lyy289065406
  • 2011-07-29 01:44:14
  • 6190

POJ1003--Hangover 翻译

Hangover Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10659...
  • u013011866
  • u013011866
  • 2015-03-20 08:38:02
  • 853

ACM学习-POJ-1003-Hangover

菜鸟学习ACM,纪录自己成长过程中的点滴。 学习的路上,与君共勉。 ACM学习-POJ-1003-Hangover Hango...
  • hitwhylz
  • hitwhylz
  • 2013-09-20 18:42:50
  • 3380

北大ACM 1003 Hangover

Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 91219   Acce...
  • q745401990
  • q745401990
  • 2013-08-18 10:12:50
  • 1835

北大OJ 1003Hangover && 1001Exponentiation思路

1003 Hangover AC:#include using namespace std; double getSum(int n) { float dRet = 0.00; int...
  • misol
  • misol
  • 2011-05-17 20:29:00
  • 2056

值得做的北大ACM题

首先推荐大家一些非常简单的题,特别适合没有算法基础的新手做(需要C语言基础)。  1000 1001 1002 1003 1004 1005 1006 1007 1008 1012 1013...
  • cctvzxxz1
  • cctvzxxz1
  • 2013-02-27 10:44:10
  • 2110

poj 1003 hangover 水题

这道题目是我现在遇到的最简单的一题,(忽略1000),问题没有任何难度,就是寻找恰好大于问题所需值的卡片个数,我们这里第一反应是给他建立一个表格记录对应n张卡片最大长度,然后通过二分查找,很容易得到所...
  • zhyh1435589631
  • zhyh1435589631
  • 2014-11-30 15:26:52
  • 602

POJ 1003 Hangover GCC编译

#include #include int main() { //float x=1,y=7; //printf("%f",x/y); float a[1000]; memset(a,-...
  • woniupengpeng
  • woniupengpeng
  • 2016-05-15 10:21:29
  • 134

POJ 1003 - Hangover

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 98268   Accepted: 47634 ...
  • u011521976
  • u011521976
  • 2014-04-19 13:27:08
  • 365

poj 1003 Hangover

  • 2009年06月01日 13:29
  • 403B
  • 下载
收藏助手
不良信息举报
您举报文章:POJ 1003 Hangover
举报原因:
原因补充:

(最多只允许输入30个字)