您的位置:首页 > 其它

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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: