poj1850

本文深入探讨了一段用于解决组合数学问题的C++代码实现,详细解释了代码逻辑、核心算法及其背后的数学原理,同时提供了实例分析和代码优化建议。

组合数学

ContractedBlock.gifExpandedBlockStart.gifView Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
using namespace std;

char st[15];

int com(int n, int r)
{
if (n - r < r)
r
= n - r;
int i, j, s = 1;
for (i = 0, j = 1; i < r; ++i)
{
s
*= (n - i);
for (; j <= r && s % j == 0; ++j)
s
/= j;
}
return s;
}

int main()
{
//freopen("t.txt", "r", stdin);
gets(st);
int len = strlen(st);
if (len == 1)
{
printf(
"%d\n", st[0] - 'a' + 1);
return 0;
}
bool ok = true;
if (st[0] <= 'z' && st[0] >= 'a')
ok
= true;
else
ok
= false;
for (int i = 1; i < len; i++)
if (!(st[i] <= 'z' && st[i] >= 'a' && st[i] > st[i - 1]))
ok
= false;
if (!ok)
{
printf(
"0\n");
return 0;
}
int ans = 26;
for (int i = 2; i < len; i++)
ans
+= com(26, i);
ans
+= com(26, len) - com(26 - (st[0] - 'a'), len);
for (int i = 1; i < len; i++)
ans
+= com(26 - (st[i - 1] - 'a' + 1), len - i) - com(26 - (st[i] - 'a'
), len
- i);
printf(
"%d\n", ans + 1);
return 0;
}

转载于:https://www.cnblogs.com/rainydays/archive/2011/06/11/2078187.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值