解题思路: 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++代码)浏览:817 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:669 |
简单的a+b (C语言代码)浏览:759 |
C语言考试练习题_排列 (C语言代码)浏览:1315 |
C语言程序设计教程(第三版)课后习题10.4 (C语言代码)浏览:665 |
WU-printf基础练习2 (C++代码)浏览:2000 |
C语言考试练习题_一元二次方程 (C语言代码)浏览:575 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:468 |
循环入门练习5 (C语言代码)浏览:829 |
1048题解(读入回车问题)浏览:554 |