UVa12024 - Hats(错排问题)
来源:互联网 发布:日本人变态 知乎 编辑:程序博客网 时间:2024/05/16 05:59
Background
John Hatman, the honest cloakroom attendant of the RoyalTheatre of London, would like to know the solution to the followingproblem.
TheProblem
Whenthe show finishes, all spectators in the theatre are in a hurry to see the Final of the UEFAChampionship. So, they run to the cloakroom to take their hats back.
Some of them take a wrong hat. But, how likely is thateveryone take a wrong hat?
TheInput
The first lineof the input contains an integer, t,indicating the number of test cases. For each test case, one lineappears, that contains anumbern, 2<=n<=12,representing the number of people and hats.
TheOutput
For each test case, the output should contain asingle line with the number representing the number of favourable cases(i.e., the number of cases where all people take a wrong hat),followed by a bar, "/", and followed by a number representing thetotal number of possible cases.
SampleInput
3234
SampleOutput
1/22/69/24题意 :求错排个数与排列的个数之比
思路:错排递推关系式为f(n) = (n-1)(f(n - 2) + f(n - 1)),排列的递推式为d(n) = n * d(n -1)
#include <cstdio>using namespace std;const int MAXN = 13;typedef long long LL;LL f[MAXN], fact[MAXN];int n;void init(){ f[0] = 0; f[1] = 0; f[2] = 1; fact[1] = 1; fact[2] = 2; for (int i = 3; i < MAXN; i++) { f[i] = (i - 1) * (f[i - 2] + f[i - 1]); fact[i] = i * fact[i - 1]; }}void input(){ scanf("%d", &n);}void solve(){ printf("%d/%d\n", f[n], fact[n]);}int main(int argc, char **argv) {#ifndef ONLINE_JUDGE freopen("d:\\OJ\\uva_in.txt", "r", stdin);#endif init(); int cas; scanf("%d", &cas); while (cas--) { input(); solve(); } return 0;}
- UVa12024 - Hats(错排问题)
- UVA - 12024 Hats (错排问题)
- 错排问题
- 错排问题
- 错排问题
- 错排问题
- 错排计数问题
- 错排问题
- 错排 问题
- 错排问题:
- 错排问题
- 错排问题
- 错排问题
- 浅析错排问题
- 错排问题
- 错排问题--十二金钗
- 错排问题
- 错排问题
- vim自动补齐插件onmicppcompete
- jsp九大内置对象之response对象
- linux下的java程序脚本启动程序
- 如何有效的减少重复的代码
- 相关度算法BM25
- UVa12024 - Hats(错排问题)
- 思考
- jsp九大内置之request对象
- (cocos2d-js游戏)测试你的反应速度----------基本逻辑(上)
- scrollview嵌套listview,listview下拉冲突
- 2014-10-27Android学习------自定义widget的监听事件的实现-----城市列表应用程序
- Ubuntu12.04LTS安装完后的一些配置
- jsp九大内置对象之session对象
- fghhfshfshfghrtgf