从n+m步中挑选min(n,m)步向上走,剩下的就是向下走。
求解n+mCmin(n,m)时,要一边计算一边约分。
#include <cstdio> #include <algorithm> #include <iostream> using namespace std; unsigned int n,m; unsigned int Com(unsigned int m,unsigned int n) { double cnm = 1.0; while(n > 0) cnm *= (double)(m--)/(double)(n--); cnm += 0.5; return (unsigned int)cnm; } int main() { while(cin>>n>>m && (n||m) ) { cout << Com(n+m,min(n,m))<<endl; } }