POJ1942-Paths on a Grid
生活随笔
收集整理的這篇文章主要介紹了
POJ1942-Paths on a Grid
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
給你兩個數n,m,代表n*m的矩陣,讓你求,從左下角走到右上角的方法數;
走法是只能往上走或者往右走。
?
這個題就是求組合數
從左下角走到右上角一共走n+m步,必須得走n步或者m步,所以從n+m中選擇n步或者m步。
所以直接求Cnn+m? 或者Cmn+m 都是答案
?
代碼:
1 #include <iostream> 2 #include <cstdio> 3 #include <set> 4 using namespace std; 5 typedef long long ll; 6 int main() 7 { 8 ll n,m; 9 while(~scanf("%lld%lld",&n,&m)) 10 { 11 ll ans=1; 12 if(n==0&&m==0) 13 break; 14 ll t=m+n; 15 n=max(n,m); 16 for(ll i=n+1,j=1;i<=t;i++,j++) 17 ans=ans*i/j; 18 printf("%lld\n",ans); 19 } 20 21 return 0; 22 }?
轉載于:https://www.cnblogs.com/Cherry93/p/9919826.html
總結
以上是生活随笔為你收集整理的POJ1942-Paths on a Grid的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: History of program (
- 下一篇: noip2010关押罪犯