HDOJ 1056 HangOver

【题意】:如果你有一张卡,你最多可以让一半悬浮出来。两张卡的话,上边的卡可以悬浮在下边卡,露出一半的长度。下边的卡最桌子来说露出三分之一的长度,总长度1/2+1/3,三张的话就是1/2+1/3+1/4,一次类推。

输入包含多组数据c,0.00表示结束。数据是介于0.01~5.20之间的浮点数。

输出可以实现数据c需要的最小卡片数。

【代码:WA】

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main()
{
    double c = 0.00;
    while (cin >> c && c)
    {
        int i = 0;
        double sum = 0;
        for (i = 2; ; i++)
        {
            sum += 1.0/i;
            if (sum - c >= 1e-10)
            {
                cout << i-1 << " card(s)" << endl;
                break;
            }
        }
    }
    return 0;
}

【代码:AC】 浮点数比较问题,不知道为什么会这样,求大神。

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main()
{
    double c = 0.00;
    while (cin >> c && c)
    {
        int i = 0;
        double sum = 0;
        for (i = 2; ; i++)
        {
            sum += 1.0/i;
            if (sum >= c)
            {
                cout << i-1 << " card(s)" << endl;
                break;
            }
        }
    }
    return 0;
}
时间: 2024-12-14 13:31:26

HDOJ 1056 HangOver的相关文章

HDU 1056.HangOver【水!水!水!】【8月28】

HangOver Problem 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

HDU 1056 - HangOver

递推 1 #include <iostream> 2 using namespace std; 3 double f[10000]; 4 double c; 5 int main() 6 { 7 f[0]=0; 8 for(int i=1;i<=10000;i++) f[i]=1.0/(i+1)+f[i-1]; 9 while(cin>>c,c!=0) 10 { 11 int m=1; 12 while(f[m]<c) m++; 13 cout<<m<

(HDU)1056 --HangOver( 悬住)

题目链接:http://vjudge.net/problem/HDU-1056 第一次写的时候TLE了,原因是处理double型计算的时候,丢失了精度,跳不出循环. 在算式第一个数字后加.0就可以了... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 int main() 7 { 8 9 double l; 10 while(~scanf

HangOver(杭电1056)

HangOver Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9821    Accepted Submission(s): 4137 Problem Description How far can you make a stack of cards overhang a table? If you have one card, y

1056

HangOver Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9009 Accepted Submission(s): 3773 Problem Description How far can you make a stack of cards overhang a table? If you have one card, you can

HDOJ 题目分类

HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:    用STL中的next_permutation()1029:1032:1037:1039:1040:1056:1064:1065:1076:    闰年 1084:1085:1089,1090,1091,1092,1093,1094, 1095, 1096:全是A+B1108:1157:1196:1

hdu 1056

HangOver Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9112    Accepted Submission(s): 3826 Problem Description How far can you make a stack of cards overhang a table? If you have one card, yo

1056. 组合数的和

1056. 组合数的和(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字.要求所有可能组合出来的2位数字的和.例如给定2.5.8,则可以组合出:25.28.52.58.82.85,它们的和为330. 输入格式: 输入在一行中先给出N(1<N<10),随后是N个不同的非0个位数字.数字间以空格分隔. 输出格式: 输出所有可能组合出来的2

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>