360 2018 Autumn Recruitment Written Exam Questions
(I'll update the code later...)
1. Sell chalk
Xiaoming has m colored chalks and n white chalks, of which a colored chalk and b white chalks can be sold for x yuan, c white chalks can be sold for y yuan, d colored pencils can be sold for z yuan, and chalk does not have to be sold. After all, how to maximize the benefits?
Example:
Input:
m=5, n=5, a=1, b=2, c=3, d=3, x=2, y=1, z=3,
output:
7
Parse:
Search recursively . There are three ways to sell:
[a , b] —> x
[0, c] —> y
[d, 0] —> z
Choose the one with the most benefits from the three ways each time, and the rest Chalk continues to search recursively until it is sold out or not.
c++ code implementation:
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int sell(vector<int>& objects,vector<vector<int>>& special,map<vector<int>,int>& answer)
{
if(answer.find(objects)!=answer.end())
return answer[objects];
int result = 0, j = 0;
for(vector<int> s : special) {
vector<int> rest(objects.begin(),objects.end());
for( j=0; j<objects.size(); j++) {
int diff = rest[j]-s[j];
if(diff<0)
break;
rest[j] = diff;
}
if(j==objects.size()) {
int money = s[j] + sell(rest,special,answer);
result = money > result ? money : result;
}
}
answer[objects] = result;
//没卖完的,返回0
return result;
}
int shoppingOffer(vector<int>& objects,vector<vector<int>>& special)
{
map<vector<int>,int> answer;
return sell(objects,special,answer);
}
int main()
{
int m,n,a,b,c,d,x,y,z;
cin>>m>>n>>a>>b>>c>>d>>x>>y>>z;
vector<vector<int>> special = {{a,b,x},{0,c,y},{d,0,z}};
vector<int> objects = {m,n};
cout<<shoppingOffer(objects,special)<<endl;
return 0;
}
2. Array exchange
There are two arrays, namely a[a1, a2, …, an] and b[b1, b2, …, bm], sum represents the sum of all elements of the array, and at most two pairs of numbers in the a and b arrays are exchanged , so that; The absolute value of the difference between the sums of the two swapped arrays is the smallest?
Example:
Input:
n=4, a= [1, 3, 7, 9], m=3, b=[2, 10, 12]
Output:
0
Explanation: Swap (3, 2) and (9, 12) pairs , the sum of the elements of the last two arrays is both 22. The absolute value of the difference is 0.
Parse:
Let’s look at this problem first, exchanging a pair of elements of two arrays so that the absolute value of the elements and the difference between the two arrays is the smallest . This problem has one less restriction than the above problem: the number of exchange of elements is not limited .
Array a, the sum of elements is denoted as sumA; array b, the sum of elements is denoted as sumB.
If sumA==sumB, no swap is required.
Without loss of generality, we assume sumA > sumB and delta = sumA-sumB.
Suppose a[i] is exchanged with b[j], then: sumA'=sumA-a[i]+b[j], sumB'=sumB-b[j]+a[i]
delta' = sumA'-sumB ' = delta - 2*(a[i]-b[j]).
Therefore, to make the sum of two array elements as equal as possible, delta' should be as equal to 0 as possible, and a[i]-b[j] should be as close as possible to delta/2.
However, there is a limit to this question, at most two exchanges. The method has not yet been figured out.
C++ code implementation:
Swap a pair of elements of two arrays so that the absolute value of the elements and the difference between the two arrays is the smallest
#include <iostream>
#include <math.h>
using namespace std;
int change(int a[],int n,int b[],int m,int sumA,int sumB)
{
int delta = 0;
bool bigger = true;
if(sumA>sumB){
delta = sumA - sumB;
bigger = true;
}
else{
delta = sumB - sumA;
bigger = false;
}
int diff = 0, x=0;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
diff = a[i]-b[j];
x = diff > 0 ? diff : (~diff+1); //abs(diff)
if((x*2 <= delta)&&((bigger && diff>0)||(!bigger && diff<0))){
a[i] ^= b[i];
b[i] = a[i]^b[i];
a[i] = a[i]^b[i]; //交换a[i] b[j]
delta -= 2*x;
if(delta==0)
return 0;
}
}
}
return delta;
}
int main()
{
int n,m,sumA=0,sumB=0;
cin>>n;
int a[n];
for(int i=0; i<n; i++){
cin>>a[i];
sumA += a[i];
}
cin>>m;
int b[m];
for(int i=0; i<m; i++){
cin>>b[i];
sumB += b[i];
}
if(sumA==sumB)
cout<<0;
else
cout<<change(a,n,b,m,sumA,sumB);
return 0;
}