POJ 1496 + 1850 (数论-排列组合) (?)
2010-12-10 21:39
337 查看
a -> 1 b -> 2 . . z -> 26 ab -> 27 ac -> 28 . . az -> 51 bc -> 52 . . vwxyz -> 83681
/*理解是关键,数论题*/ #include <iostream> #include <cstring> using namespace std; const int maxn = 11; char str[maxn]; int nResult; int judgeStr(); void funcSolve(); void funcOutput(); int funcCom(int x, int y); int main() { while (1 == scanf(" %s", str)) { funcSolve(); funcOutput(); } return 0; } int judgeStr() { int len = strlen(str); int flag = 1; for(int i = 1; i < len - 1; i ++) { if(str[i] < str[i-1]) { flag = false; break; } } return flag; } void funcSolve() { nResult = 0; if(judgeStr()) { int temp, t; int len = strlen(str); for(int i = 0; i < len; i ++) { temp = 0; t = str[i] - 'a' + 1;//因为是字典序 故后面的字母只能是在当前字母的后面 /*比如vwxyz 第一位是v那么此时就要先算在v之前的有少种组合 *for(j=1;j<=t;j++)既是算v之前的字母 若这一位已经确定 *那么后面剩下的数的组合也就确定了为Com(len-i-1,26-j) */ /*for循环 算出后面字母组合数*/ for(int j = 1; j <= t; j ++) { temp += funcCom(len-i-1, 26-j); } nResult += temp; } } } void funcOutput() { printf("%d/n", nResult); } /*(y>x)组合数, 关键*/ int funcCom(int x, int y) { int ret = 1; int temp1 = x, temp2 = 1; for(int i = 0; i < x; i ++) { ret *= y --; while ((temp1 > temp2) && (ret % temp1 ==0)) { ret /= temp1; temp1 --; } while ((temp1 > temp2 - 1) && (ret % temp2 == 0)) { ret /= temp2; temp2 ++; } } return ret; }
[/code]
相关文章推荐
- 排列组合:poj 1496 Word Index+poj 1850 Code
- poj 1496(1850)/2245 全组合打表(一种字符编码/Lotto)
- POJ1850 Code 【排列组合】
- POJ 1850 Code/POJ 1496 Word Index(组合数学-字母串序号)
- POJ 1850/ 1496 组合数学
- poj1496(组合数学)-与1850几乎一样
- POJ【数论/组合/博弈论】题目列表
- POJ【数论/组合/博弈论】题目列表
- hdoj 2048 神、上帝以及老天爷【全错位排列】【组合数论】
- POJ:2719 Faulty Odometer(数学,排列组合)
- POJ1850 组合数学
- poj 1850 code(组合数学)
- 字典序生成下一排列组合 poj 1146水
- Poj 3187 Backward Digit Sums【暴力+排列组合】
- 排列组合:poj 3252 Round Numbers
- poj 1850 Code (组合数学)
- 【POJ 3187 Backward Digit Sums】 + DFS + 排列组合
- poj 1496 poj 1850
- (组合数学3.1.2.1)POJ 2249 Binomial Showdown(排列组合公式的实现)
- POJ 3252 Round Numbers (数论&&组合)