答应的事情要做到名言:c++编程,要源码!

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/28 11:56:29
为果园客户设定密码

虚拟果园一批新品种的芒果就要成熟了。为了让游园的客户都能吃到芒果并且防止有人破坏果园,果园安装了一个新的安全系统,并且要给每一个游园客户分配一个有效的密码。一个有效的密码由L(3 <= L <= 15)个小写字母(来自传统的拉丁字母集'a'...'z')组成,至少有一个元音('a', 'e', 'i', 'o', 或者 'u'),至少两个辅音(除去元音以外的音节),并且按字母表顺序出现(例如,'acd'是有效的,而'dac'不是)。

给定一个期望长度L和C个小写字母,写一个程序,打印出所有的长度为L、能由这些字母组成的有效密码。密码必须按字母表顺序打印出来,一行一个。
题目名称: password
输入格式:
* 第一行: 两个由空格分开的整数,L和C
* 第二行: C个空格分开的小写字母,密码是由这个字母集中的字母来构建的。
输入样例:
4 6
a t c i s w

题目名称: password
输入格式:
* 第一行: 两个由空格分开的整数,L和C
* 第二行: C个空格分开的小写字母,密码是由这个字母集中的字母来构建的。
输入样例:
4 6
a t c i s w
输入详细说明:
由从给定的六个字母中选择的、长度为4的密码。

出格式:
* 第一至?行: 每一个输出行包括一个长度为L个字符的密码(没有空格)。输出行必须按照字母顺序排列。

输出样例 (文件 passwd.out):

输出格式:
* 第一至?行: 每一个输出行包括一个长度为L个字符的密码(没有空格)。输出行必须按照字母顺序排列。

输出样例 (文件 passwd.out):
acis
acit
aciw
acst
acsw
actw
aist
aisw
aitw
astw
cist
cisw
citw
istw

呕心沥血之作:
要细细品味。
编到头都晕了。

#include <iostream>
#include <fstream>
using namespace std;
void setlc(int &, int &);
void setchars(char *, int);
bool sort(char *, int);
char *gennext(char *, int *, int, int, ofstream&);
bool carry(int*, int, int, int);
bool check(char*, int);
void display(char *, int, ofstream&);
int main(int argc, char *argv[]){
int l(0), c(0);
setlc(l, c);
char *chars = new char[c];
setchars(chars, c);
//*********************************
//initialize the position array
int *pos = new int[l];
for(int ix=0; ix<l; ++ix) pos[ix]=ix;
--pos[l-1];
//*********************************
ofstream ofile( "passwd.out" );
while ( gennext(chars, pos, c, l, ofile) ){
}
}
void setlc(int &l, int &c){
cout << "Input L and C" << endl;
cin >> l >> c;
if( l< 3 || l>15 || c<l || c>26 ){
cout << "invalid input! Input again." << endl << endl;
setlc(l, c);
}

}
void setchars(char *chars, int c){
int cnt(0); //count how many vowel
cout << "Input characters" << endl;
for(int ix=0; ix<c; ++ix){
cin >> chars[ix];
if( chars [ix] < 'a' || chars[ix] > 'z' ){
cout << "invalid input! Input again." << endl << endl;
setchars(chars, c);
}
switch ( chars[ix] ){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
++cnt;
}
}
if( !cnt || !sort(chars, c) ){
cout << "invalid input! Input again." << endl << endl;
setchars(chars, c);
}
}
bool sort(char *chars, int c){
for(int ix=0; ix<c-1; ++ix){
for(int jx=ix+1; jx<c; ++jx){
if( chars[ix] == chars[jx] ) return false;

if( chars[ix] > chars[jx] ){
char temp = chars[jx];
chars[jx] = chars[ix];
chars[ix] = temp;
}
}
}
return true;
}

char *gennext(char *chars, int *pos, int c, int l, ofstream &os){
char *res = new char[l];
if( carry(pos, c, l, l-1) ){
for(int ix=0; ix<l; ++ix){
res[ix] = chars[ pos [ix] ];
}
if( check(res, l) ){
display(res, l, os);
return res;
}else return gennext(chars, pos, c, l, os);
}
return 0;
}
bool carry(int *pos, int c, int l, int n){
if ( n < 0 ) return false;
if( ++pos[n] == c ) return carry(pos, c, l, n-1);

for(int ix=n+1; ix<l; ++ix){
pos[ix] = pos[n]+ix-n;
if( pos[ix] >= c ) return carry(pos, c, l, n-1);
}
return true;
}
bool check(char *str, int n){
for(int ix=0; ix<n; ++ix){
switch (str[ix]){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
}
}
return false;
}
void display(char *str, int n, ofstream &os){
if( n>0 ){
for(int ix=0; ix<n; ++ix){
cout << str[ix];
os << str[ix];
}
cout <<endl;
os << endl;
}
}

分太少,500分我就给你做!