# Netease written test 4 questions

1. Divide the goods equally

Now there are n items, and each item has a value. Now I want to distribute these items to two people. It is required that the total value of the items distributed by each of the two people is the same (the number can be different, and the total value can be the same). The remaining items need to be thrown away. Now I want to know how many items of value need to be thrown at least to meet the requirements and distributed to two people.

Enter Description:

In the first line, enter an integer T, representing t groups of test data.

For each set of test data, enter an integer n in one line, representing the number of items.

Next n numbers, a[i] represents the value of each item.

1<= T <= 10

1 <= n <= 15

1 <= a[i] <= 100000

Output Description:

```For each set of test data, output an answer representing the value of the least throw.
```

Input example 1:

```1
5
30 60 5 15 30
```

Output example 1:

```20
```

Example 1:

The example explains that throw away the third and fourth items, then give the first item and the fifth item to the first person, and the second item to the second person. Each person gets a value of 60 and the throw away value is 20.

Solution: violent DFS, actually passed (it's ugly and not elegant enough)

```#include <iostream>
#define MAXINT 0x3f3f3f3f
using namespace std;

int sum1, sum2, all, n, ans;
int a;

void solve(int num)
{
if (sum1 == sum2) {
int tmp = all - (sum1+sum2);
ans = ans < tmp ? ans : tmp;
}
if (num == n) return;

sum1 += a[num];
solve(num+1);
sum1 -= a[num];

sum2 += a[num];
solve(num+1);
sum2 -= a[num];

solve(num+1);
}

int main()
{
int t;
cin >> t;
while (t--) {
cin >> n;
ans = MAXINT;
all = 0;
sum1 = sum2 = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
all += a[i];
}
sum1 += a;
solve(1);
sum1 -= a;

sum2 += a;
solve(1);
sum2 -= a;

solve(1);

cout << ans << endl;
}
return 0;
}```

2. Ticket purchase

Now there are n people queuing up to buy tickets. It is known that they start selling tickets at 8 a.m. these n people can buy tickets in two ways:

The first is that everyone can buy their own tickets alone, and the ith person takes a[i] seconds.

The second is that everyone can choose to buy tickets with the people behind him. The i+1 person and i+1 person spend b[i] seconds in total.

The last person can only buy tickets with the person in front or alone.

Since the ticket selling place wants to close early, he wants to know the earliest time he can close. Please output a time format, such as 08:00:40 am/pm

The number of time should be kept in two digits. If it ends in the morning, it is am, and if it ends in the afternoon, it is pm

Enter Description:

In the first line, enter an integer T, followed by T sets of test data.

For each set of test data: enter a number n, which means that n people buy tickets.

The next n numbers represent the time when each person bought the ticket alone a[i].

The next n-1 number represents the time required for everyone to buy tickets with the person in front of him b[i]

1<= T <=100

1<= n <=2000

1<= a[i] <=50

1<= b[i] <=50

Output Description:

```For each group of data, output a time, representing the closing time.
```

Input example 1:

```2
2
20 25
40
1
8
```

Output example 1:

```08:00:40 am
08:00:08 am```

Problem solution: simple DP. Each time, judge whether the current person is faster to buy tickets by combining with the previous person or by himself. It should be noted that if the current person buys tickets with the previous person, the previous person can't buy tickets with the previous person, so dp[i] = min(dp[i-1]+a[i], dp[i-2]+b[i]). It's hard to change time.

```#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
int T, n;
int a, b;
int dp;
cin >> T;
while (T--) {
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i) cin >> b[i];

if (n == 1) {
int hour = 8;
int mins = 0;
int sec = a;
mins += a/60;
sec = a%60;
hour += mins/60;
mins = mins%60;
bool am = true;
if (hour > 12) {
am = false;
hour = hour%12;
}
if (hour < 10) cout << "0";
cout << hour << ":";
if (mins == 0) cout << "00";
else {
if (mins < 10) cout << "0" << mins;
else cout << mins;
}
cout << ":";
if (sec == 0) cout << "00";
else {
if (sec < 10) cout << "0" << sec;
else cout << sec;
}
if (am) cout << " am\n";
else cout << " pm\n";
continue;
}

dp = a;
dp = b < (a+a) ? b : (a+a);
for (int i = 2; i < n; ++i) {
dp[i] = min(dp[i-1]+a[i], dp[i-2]+b[i]);
}
int time = dp[n-1];
int hour = 8;
int mins = 0;
int sec = time;
mins += time/60;
sec = time%60;
hour += mins/60;
mins = mins%60;
bool am = true;
if (hour > 12) {
am = false;
hour = hour%12;
}
if (hour < 10) cout << "0";
cout << hour << ":";
if (mins == 0) cout << "00";
else {
if (mins < 10) cout << "0" << mins;
else cout << mins;
}
cout << ":";
if (sec == 0) cout << "00";
else {
if (sec < 10) cout << "0" << sec;
else cout << sec;
}
if (am) cout << " am\n";
else cout << " pm\n";
}
return 0;
}```

3. Xiaoyi AI palindrome

Xiaoyi gets a string containing only upper and lower case English characters, which may not be a palindrome string. (a "palindrome string" is a string with the same forward reading and reverse reading. For example, "level" or "noon" is a palindrome string, and "asds" is not a palindrome string.)

Xiaoyi can add any number of arbitrary characters at the end of the string to turn its string into a palindrome string.

Now please write a program that can calculate the shortest palindrome string that Xiaoyi can get.

Enter Description:

```A line includes a string. Length [1, 1000]
```

Output Description:

```A line contains a string representing the answer.
```

Input example 1:

```noon
```

Output example 1:

```noon
```

Input example 2:

```noo
```

Output example 2:

```noon
```

Input example 3:

```helloworld
```

Output example 3:

`helloworldlrowolleh`

Solution: it's still the violence law. First judge whether it's palindrome. If so, output it. If not, check whether the last n characters are palindrome strings. If yes, the generated palindrome string is the reverse order of string ontology + the first len-n characters.

```#include <iostream>
#include <string>
using namespace std;

int main()
{
string str;
cin >> str;
bool flag = true;
int len = str.length();
for (int i = 0; i < len/2; ++i) {
if (str[i] != str[len-i-1]) {
flag = false;
break;
}
}
if (flag) cout << str << endl;
else {
for (int i = 0; i < len; ++i) {
string tmp = str.substr(i, len-i);
bool flagTmp = true;
int lenTmp = tmp.length();
for (int i = 0; i < lenTmp/2; ++i) {
if (tmp[i] != tmp[lenTmp-i-1]) {
flagTmp = false;
break;
}
}
if (flagTmp) {
cout << str;
string ans = str.substr(0, len-lenTmp);
int lenAns = ans.length();
for (int i = lenAns-1; i >= 0; --i) {
cout << ans[i];
}
cout << "\n";
break;
}
}
}
return 0;
}```

4. Prime number

Niuniu now has an array a containing n positive integers. Niuniu can split each number a[i] into several positive integers with a sum of a[i]. Niuniu wants to know how many primes this array can have after splitting (or not splitting at all).

Enter Description:

In the first line, a positive integer n represents the length of the array

The n positive integers in the second line represent the value of a[i]

1<= n <= 1e^6, 1<= a[i] <= 1e^9

Output Description:

```The maximum number of primes in the disassembled array
```

Input example 1:

```3
1 1 1
```

Output example 1:

```0
```

Example 1:

```Since 1 cannot be disassembled and 1 is not a prime number, there are at most 0 prime numbers after disassembly
```

Input example 2:

```3
5 3 7
```

Output example 2:

```6
```

Example 2:

`3. Do not disassemble; 5 can be split into {2,3} and become two prime numbers; 7 can be split into {2,2,3} and become three primes, so the last split array can have up to six primes`

Problem solution: in fact, the number is divided into 2 and 3. This is the Split case with the most primes, so you only need to calculate how many 2's there are, because 3 is also 2 + 1.

I didn't expect the data range to be as large as long long.

```#include <iostream>
using namespace std;

int a[1000000+5];

int main()
{
int n;
long long ans = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (a[i] < 2) continue;
ans += (a[i]/2);
//if (a[i] % 2 == 1) ans++;
}
cout << ans << endl;
return 0;
}
```

Posted by rrhody on Sat, 18 Sep 2021 06:56:33 -0700