原题链接:蓝桥杯2014年第五届真题-排列序数
解题思路: 1.暴力法 2.(大神的) STL组合法
注意事项:
参考代码:先看下我的无脑暴力。。。 直接打表
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
char a[4]={'a','b','c','d'};
int main(){
string s;
while(cin>>s){
if(s=="abcd") cout<<"0"<<endl;
if(s=="abdc") cout<<"1"<<endl;
if(s=="acbd") cout<<"2"<<endl;
if(s=="acdb") cout<<"3"<<endl;
if(s=="adbc") cout<<"4"<<endl;
if(s=="adcb") cout<<"5"<<endl;
if(s=="bacd") cout<<"6"<<endl;
if(s=="badc") cout<<"7"<<endl;
if(s=="bcad") cout<<"8"<<endl;
if(s=="bcda") cout<<"9"<<endl;
if(s=="bdac") cout<<"10"<<endl;
if(s=="bdca") cout<<"11"<<endl;
if(s=="cabd") cout<<"12"<<endl;
if(s=="cadb") cout<<"13"<<endl;
if(s=="cbad") cout<<"14"<<endl;
if(s=="cbda") cout<<"15"<<endl;
if(s=="cdab") cout<<"16"<<endl;
if(s=="cdba") cout<<"17"<<endl;
if(s=="dabc") cout<<"18"<<endl;
if(s=="dacb") cout<<"19"<<endl;
if(s=="dbac") cout<<"20"<<endl;
if(s=="dbca") cout<<"21"<<endl;
if(s=="dcab") cout<<"22"<<endl;
if(s=="dcba") cout<<"23"<<endl;
}
return 0;}
接下来咱们看看一个大神的思路。。 帮他宣传下 STL妙用!#include<bits/stdc++.h>
using namespace std;
int main() {
int count = 0;
char str[12], copy[12];
cin >> str;
/* 复制输入的字符串 */
memcpy(copy, str, sizeof(str) * sizeof(char));
/* 排序回到最初的状态 */
sort(str, str + strlen(str));
/* 如果字符串相等就退出 */
while (strcmp(str, copy)) {
count++;
/* 生成下一个排列 */
next_permutation(str, str + strlen(str));
}
cout << count << endl;
return 0;
}0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复