POJ1003解题方案

水题,好久不写C++,拿来练手的

题目:

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.
#include
using namespace std;
void OutPut(double input)
{
double sum=0.00;
int i=1.0;
while(sum<input)
{
sum=sum+1.0/(i+1);
i++;
}
cout<<int(i-1)<<"card(s)\n";
}
int main()
{
double input;
cin>>input;
while(input)
{
if(input<0.01||input>5.20)
{
cin>>input;
continue;
}
OutPut(input);
cin>>input;
}
getchar();
return 1;
}
结果如图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ1753的题目描述为:有一个4×4的棋盘,棋盘中有16个棋子,其中有14个棋子是黑色的,用B表示;另外两个棋子是白色的,用W表示。现在要求移动棋子,将两个白色棋子移到一起,移动棋子方式是把与白色棋子相邻(上下左右,而非斜向相邻)的棋子移到空位上。现在,请你求出最少需要移动多少次。 题目看起来很简单,但是要考虑各种情况,一般在处理类似的搜索问题时,我们使用Breath First Search (BFS)来解决问题。 BFS 是一种优秀的遍历搜索算法,广泛应用于许多问题,特别是计算机科学和人工智能。BFS 只需要进行一次完整的搜索即可找到问题的最短路径或解决方案。 在这道题目中,我们可以使用 BFS 来解决问题。 我们首先需要定义状态的表示方式,可以这么表示: 1. 4*4的数组board表示状态。 2. 一个结构体Node,代表搜索树的每个节点。其中状态的表示形式为board。还有一些列信息,包括横,纵坐标,深度depth,以及方向dir。 我们使用 queue 来存储每一层需要遍历的结点,对于每个结点,我们枚举它可以到达的状态,并将这些状态添加到队列中,继续进行下一层的遍历。直到达到目标状态。 因此,我们的搜索过程主要包括以下的步骤: 1. 判断当前状态是否是目标状态 2. 枚举当前状态可能到达的所有状态,并判断是否合法 3. 如果该状态未被访问过,添加该状态,进行遍历。 知道了上面的步骤,我们就可以使用 bfs 来解决问题了。 具体实现可以参考以下代码:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值