中興 2019校園招聘編程題-9.03

選擇題沒什麼參考價值,都不會,全靠蒙,部分考察軟件工程知識,其餘的基本沒見過
編程題沒有測試的地方,只是運行了,不知道能不能AC
1-1
1-2.png
思路:
題目求相同字符給定長度的(按字母順序的)全排列
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
*/

2-1.png
2-2.png
代碼:

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章