garlic and zombies(模拟法)

http://cstest.scu.edu.cn/soj/contest/problem.action?cid=332&alias=C

garlic and zombies

Description

This problem is recommended for ACM newcomers. huangshenno1 is playing the famous game Plants VS Zombies and he is so boring that he puts garlic onto every grid of the garden. At this time, there happen to be a zombie coming from the middle path. Initially, the zombie is in the nth column but there is a car in the 0th column of every path. A car will beat the zombie as soon as the zombie meets it. Assume that when a zombie meet a garlic, it will either go up-left or go down-left and both possibility is 50%(See the picture, but it will never go out of the garden). huangshenno1 wants to know the possibility of the cars from path 1 to path 5 to beat this zombie if there are n columns.

Input

There are multiple test cases.

For each test case there is only one line with a single integer n indicating the number of columns. (0<=n<=10000)

Output

For each test case output one line five numbers indicating possibilities of the cars from path 1 to path 5 to beat this zombie, each number with 2 digits after the decimal point.

Sample Input

1

9

Sample Output

0.00 0.50 0.00 0.50 0.00

0.00 0.50 0.00 0.50 0.00

思路:这道题有很多人AC,可是我一直都在画图,试图找到一种规律解决该情况

可是还是卡在那了。

比赛后问了同学才知道,直接用模拟法就可以了,通过以前即现在的这道题我总结模拟法如下:

1.题目特征:一般没有什么算法而言,但又找不到合适的规律时,然后题目中在做某一项任务,而且分步进行,前后关联的那种,这时候就可以考虑模拟法了

2.解题步骤:

初始化初值,根据题意模拟步骤行走

#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;
const  int maxn=10000+10;
double  p[maxn][7];
int main()
{int n,i;
memset(p,0,sizeof(p));
p[0][2]=1.00;//设置初始情况,当没有格子的时候直接就在在第三行撞上了
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
 {
   p[i][0]=p[i-1][1]*0.5;
   p[i][1]=p[i-1][2]*0.5+p[i-1][0];
   p[i][2]=p[i-1][3]*0.5+p[i-1][1]*0.5;
   p[i][3]=p[i-1][2]*0.5+p[i-1][4];
   p[i][4]=p[i-1][3]*0.5;
 }
 for(i=0;i<4;i++)
 printf("%.2f ",p[n][i]);
 printf("%.2f\n",p[n][4]);
}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值