写在前面:第一次做公司面试题大题,能力有限语法和代码有待优化,欢迎批评指正:
解:
#include<iostream>
#include<vector>
std::vector<int> fun(const std::vector<int> & _leastPlayValue, const std::vector<int> & _lastValue);
int main()
{
std::cout << "请输入测试数组数:";
int T;
std::cin >> T;
std::cin.get();
std::vector<int>testArr(T);
int size = testArr.size();
int n, m;
for (int i = 0; i < testArr.size(); i++)
{
std::cout << "请输入小伙伴的人数:";
std::cin >> n;
std::cin.get();
std::cout << "请输入角色数:";
std::cin >> m;
std::cin.get();
std::vector<int>leastPlayValue;
for (int j = 0; j < n; j++)
{
std::cout << "请输入第" << j + 1 << "个人心中最少拥有的戏份值:";
int tempPlayValue;
std::cin >> tempPlayValue;
std::cin.get();
while (tempPlayValue<1 || tempPlayValue > 100)
{
std::cout <<"请重新输入第" << j + 1 << "个人心中最少拥有的戏份值:";
std::cin >> tempPlayValue;
std::cin.get();
}
leastPlayValue.push_back(tempPlayValue);
}
//
std::vector<int>lastValue;
for (int j = 0; j <m; j++)
{
std::cout << "请输入剧组剩余的第"<<j + 1<<"个角色的戏份值:";
int tempLastValue;
std::cin >> tempLastValue;
std::cin.get();
while (tempLastValue < 1 || tempLastValue > 100)
{
std::cout << "请重新输入剧组剩余的第" << j + 1 << "个角色的戏份值:";
std::cin >> tempLastValue;
std::cin.get();
}
lastValue.push_back(tempLastValue);
}
//
std::vector<int>RoleNumber = fun(leastPlayValue, lastValue);
for (int j = 0; j < RoleNumber.size(); j++)
std::cout << RoleNumber[j] << " ";
std::cout << std::endl;
}
return 0;
}
std::vector<int> fun(const std::vector<int> & _leastPlayValue, const std::vector<int> & _lastValue) {
std::vector<int>tempRoleNumber;
int a = _leastPlayValue.size();
int b = _lastValue.size();
for (int i = 0; i < _leastPlayValue.size(); i++)
{
int j;
for (j = 0; j < _lastValue.size(); j++)
{
if (_leastPlayValue[i] < _lastValue[j])
{
tempRoleNumber.push_back(j + 1);
break;
}
}
if (j == _lastValue.size())
tempRoleNumber.push_back(-1);
}
return tempRoleNumber;
}