版权声明:本人ZZU在校学生,文章均为个人心得,有不足之处请不吝赐教! https://blog.csdn.net/whl_program/article/details/82426879
选择题没什么参考价值,都不会,全靠蒙,部分考察软件工程知识,其余的基本没见过
编程题没有测试的地方,只是运行了,不知道能不能AC
思路:
题目求相同字符给定长度的(按字母顺序的)全排列
1. 将userName放入set中去除重复值,顺便将字符转换成小写
2. 将motherName转换成小写,查找是否有和userName相同的字符,顺便放入set去重。
3. 得出来的相同字符放入vector中进行全排列
4. 若vector长度小于给定密码长度,返回空密码
5. 若vector长度与给定密码长度相等,只有一种密码
6. 若给定密码长度为1,vector内所有字符都可以是密码
7. 从vector中选中pwdLen字符,进行全排列,将按照字母顺序排列的字符串加入密码组
代码:
#include <iostream>
#include <vector>
#include <set>
#include <string>
using namespace std;
string userName, motherName;
int pwdLen;
vector<string> perm(vector<char> &sameChar, int start, int end
, vector<string> &res, int pwdLen){
if(start == end){
string str = "";
bool flag = true;//判断是否按照字母顺序排列
for(auto i=0; i<pwdLen; i++){
str += sameChar[i];
if(i>0 && sameChar[i]<sameChar[i-1]){
flag = false;
break;
}
}
if(flag)
res.push_back(str);
}else{
for(auto i=start; i<=end; i++){
char temp = sameChar[start];
sameChar[start] = sameChar[i];
sameChar[i] = temp;
perm(sameChar, start+1, end, res, pwdLen);
//cout << *(sameChar.begin()+start
//sameChar.erase(sameChar.begin()+start);
temp = sameChar[start];
sameChar[start] = sameChar[i];
sameChar[i] = temp;
}
}
return res;
}
vector<string> passwordList(string userName, string motherName, int pwdLen){
vector<string> res;
vector<char> sameChar;
set<char> userNameSet, sameSet;
for(auto i=0; i<userName.size(); i++){
char ctemp = userName[i];
if(ctemp>='A' && ctemp<='Z')
ctemp = tolower(ctemp);
userNameSet.insert(ctemp);
}
for(auto i=0; i<motherName.size(); i++){
char ctemp = motherName[i];
if(ctemp>='A' && ctemp<='Z')
ctemp = tolower(ctemp);
if(userNameSet.find(ctemp) != userNameSet.end())
sameSet.insert(ctemp);//放入set中是排除userName和motherName中相同字符的重复值,比如两个a,留一个
}
for(auto it=sameSet.begin(); it!=sameSet.end(); it++)
sameChar.push_back(*it);
int lenSameChar = sameChar.size();
if(lenSameChar < pwdLen)//相同字符数量小于给定密码长度,返回空密码
return res;
if(lenSameChar == pwdLen){//相同字符数量等于给定密码长度,只有一种情况
string stemp = "";
for(auto it=sameChar.begin(); it!=sameChar.end(); it++)
stemp += *it;
res.push_back(stemp);
return res;
}
if(pwdLen == 1){//给定密码长度为1
for(auto it=sameChar.begin(); it!=sameChar.end(); it++){
string stemp;
stemp += *it;
res.push_back(stemp);
}
return res;
}
return perm(sameChar, 0, sameChar.size()-1, res, pwdLen);
}
int main()
{
cin >> userName >> motherName >> pwdLen;
vector<string> res = passwordList(userName, motherName, pwdLen);
cout << res.size() << endl;
for(auto i=0; i<res.size(); i++)
cout << res[i] << " ";
cout << endl;
return 0;
}
/*
RadheGupta
RADHIKA
3
ABCDRKSe
bckE
1
*/
代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
6
40
14 18 11 14 12 13
将钻石按照大小排序,从大到小得往容器里放
每次放入钻石时,遍历容器,将钻石放入第一个能容纳该钻石的容器里
*/
int numPieces, capacityContainer;
int carats[1000005];
int minContainer(int numPieces, int capacityContainer, int carats[]){
vector<int> arr(numPieces, capacityContainer);
sort(carats, carats+numPieces);
for(int i=numPieces-1; i>=0; --i){
for(int j=0; j<arr.size(); ++j){
if(arr[j] >= carats[i]){
arr[j] = arr[j] - carats[i];
break;
}
}
}
int res = 0;
for(int j=0; j<arr.size(); ++j){
if(arr[j] != capacityContainer)
res++;
}
return res;
}
int main()
{
cin >> numPieces >> capacityContainer;
for(int i=0; i<numPieces; i++)
cin >> carats[i];
cout << minContainer(numPieces, capacityContainer, carats) << endl;
return 0;
}