借书 | ||
题目描述小明有n本新书,要借给3位小朋友,假设每人每次只能借一本。小明想知道一共有多少种不同的借法,聪明的你需要写个程序来帮忙小明,输出一共有多少种借法,并输出这些借法,按照字典序排序。 输入格式输入多个case 每个case如下: 输入n 其中 10 > n >= 3 输出格式对于每个case, 第一行,输出借法的个数; 接着每一行输出一种借法。 样例输入3 样例输出6 ABC ACB BAC BCA CAB CBA |
// Problem#: 6271 // Submission#: 1625223 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University //有n本书借给3个人,每人只借一本,求借书的方法数 #include <stdio.h> int main() { int A, B, C,sum,n;//定义变量类型为整型 scanf("%d",&n);//输入书本数量 sum=n*(n-1)*(n-2);//排列组合方式来计算方法数 printf("%d\n", sum );//输出方法数 for ( A = 65; A <= 64+n; A++ )//三个for循环嵌套,输出排列的方式 { for ( B = 65; B <= 64+n; B++ ) { for ( C = 65; C <= 64+n; C++ ) { if ( A != B && B != C && A != C ) printf("%c%c%c\n", A, B, C); } } } return 0; }