Article Directory
Question A: Running training
Answer: 65
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
int n = 10000, i;
for (i = 0; n > 0; i++) {
if (i % 2 == 0) {
n -= 600;
} else {
n += 300;
}
}
cout << i << endl;
return 0;
}
Question B: Anniversary
Answer: 52038720
Can be calculated by excel, the difference between the two dates is 36138 days
So the answer is: 36138*24*60=52038720
Test Question C: Combined Test
Answer: 10
Assuming there are n people, the kits that need to be used are
n / k + 0.01 ∗ n ∗ kn/k+0.01*n*kn/k+0.01∗n∗k
Extract n, there are
n ∗ (1 / k + 0.01 ∗ k) n*(1/k+0.01*k)n∗(1/k+0.01∗k)
So, when k=10, there is a minimum
Question D: REPEAT program
This is not QAQ yet
Question F: Dividing sequence
simulation
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void)
{
ll n;
cin >> n;
cout << n;
n /= 2;
while (n > 0) {
cout << " " << n;
n /= 2;
}
cout << endl;
return 0;
}
Question G: Decoding
Simulation, expand the abbreviated string
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void)
{
string s;
cin >> s;
int n = s.size(), num;
for (int i = 0; i < n; i++) {
if (i != n - 1 && s[i + 1] >= '1' && s[i + 1] <= '9') {
num = s[i + 1] - '0';
while (num--) {
cout << s[i];
}
i++;
} else {
cout << s[i];
}
}
cout << endl;
return 0;
}
Question H: Going the square
Simple dynamic programming
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int dp[35][35];
int main(void)
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
dp[i][1] = dp[1][i] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 2; j <= m; j++) {
if (i % 2 == 0 && j % 2 == 0)
dp[i][j] = 0;
else
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
cout << dp[n][m] << endl;
return 0;
}
Test Question I: Integer Concatenation
The complexity of splicing directly is O (n 2) O(n^2)O ( n2 ), needs to be optimized
if the x and y splicing, splicing after the number of bits on the y-number bits
may first calculate the number of bits on k-0--9 times and recorded in the array
if The y-digit number exists in the array, indicating that the spliced number may be a multiple of k, so that the splicing judgment is performed.
If the y-digit number is not in the array, it means that the spliced array cannot be a multiple of k. Just skip
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
ll a[N], num[15];
bool vis[15];
ll connect(ll a, ll b)
{
ll bb = b;
while (bb != 0) {
a *= 10;
bb /= 10;
}
a += b;
return a;
}
int main(void)
{
int n, k, res = 0;
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i <= 9; i++)
vis[(i * k) % 10] = 1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ll x = a[i], y = a[j];
if (vis[y % 10] && connect(x, y) % k == 0)
res++;
if (vis[x % 10] && connect(y, x) % k == 0)
res++;
}
}
cout << res << endl;
return 0;
}
Test Question J: Network Analysis
For this question, I will only use the brute force method of union search, the complexity is O (nm) O(nm)
If O ( n m ) is 1, use union search set to merge. If it is 2, traverse all the elements, if in a set, add the corresponding value.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e4 + 5;
int n, m;
ll fa[N], sum[N];
void init()
{
for (int i = 1; i <= n; i++)
fa[i] = i;
}
int find(int x)
{
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
void unite(int x, int y)
{
x = find(x), y = find(y);
fa[x] = y;
}
int main(void)
{
int t, a, b;
cin >> n >> m;
init();
while (m--) {
cin >> t >> a >> b;
if (t == 1) {
unite(a, b);
} else {
for (int i = 1; i <= n; i++) {
if (find(a) == find(i)) {
sum[i] += b;
}
}
}
}
for (int i = 1; i <= n - 1; i++)
cout << sum[i] << " ";
cout << sum[n] << endl;
return 0;
}