一道C語言的問題 – II

來源:互聯網
上載者:User

來源:http://blog.sina.com.cn/s/blog_534c30080100d6vx.html

Fj & haozi

Description

haozi很淘氣,最近FJ帶了很多好吃的,haozi乘FJ不在的時候就吃了很多好吃的,FJ回來發現了,決定一定要抓住haozi。

但是haozi很聰明,假設原來他在16號樓,那麼下一分鐘必定是在15號樓或者17號樓.
如果他一開始在1號樓,那麼下一分鐘只能在2號樓,
如果他一開始在n號樓,那麼下一分鐘一定在n-1號樓。

現在給定一共有n號樓,一開始haozi在p號樓,求過了m分鐘,haozi在t號樓的一共有多少種行走方案數

Input

cas代表測試資料數。(0<cas<=100)
n,p,m,t代表如題意。(0<n,p,m,t<100)

Output

每組測試資料一行,每行一個整數,表示總共的方法數

Sample
Input

2
3 2 4 2
3 2 3 2  

Sample
Output

4
0

output detail:

第一組測試中有以下四種走法:
2->1->2->1->2

2->1->2->3->2

2->3->2->1->2

2->3->2->3->2

 

代碼

#include <stdio.h>int count,t,n;     void main(){int cas(int p,int m);int minute,q,a,i;scanf("%d",&a);for(i=1;i<=a;i++){  count=0;  scanf("%d%d%d%d",&n,&q,&minute,&t);  cas(q,minute);  printf("%d\n",count); }} int cas(int p,int m){if(m<=0){  if(p==t)  {count++; return 0;}    else if(p!=t)  return 0;}if(p==1)  cas(2,m-1);          else{  if(p==n)   cas(n-1,m-1);         else                   {   cas(p+1,m-1);   cas(p-1,m-1);  }}}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.