第 8 场 小白入门赛
第一题:
解题思路:
乘一下。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;
void solve()
{
int cur = 2024;
cur *= 2.5;
cout << (int)cur << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}
第二题:
解题思路:
自定义排序:比较\(a + b\)和\(b + a\)的字符串大小,按升序排序。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;
bool cmp(string a, string b)
{
string t1 = a + b;
string t2 = b + a;
return t1 < t2;
}
void solve()
{
int n;
cin >> n;
vector<string> a;
for (int i = 1; i <= n; i++)
{
string s;
cin >> s;
a.emplace_back(s);
}
sort(a.begin(), a.end(), cmp);
for (auto s : a)
{
cout << s;
}
cout << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}
第三题:
正确解题思路:
模\(10\)就是求最后个位数是多少,对于这点,我们发现只要考虑\(0\sim9\)的高次幂的规律即可。打一下表能发现周期性。
赛时解法:
快速幂拆着做。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;
ll qmi(ll a, ll b)
{
ll res = 1;
while (b)
{
if (b & 1)
{
res = res * a % 10;
}
a = a * a % 10;
b >>= 1;
}
return res;
}
void solve()
{
ll x;
cin >> x;
string p;
cin >> p;
reverse(p.begin(), p.end());
ll a = x;
ll ans = 1;
for (auto c : p)
{
int b = c - '0';
if (b > 0)
{
ans = ans * qmi(a, b) % 10;
}
a = qmi(a, 10);
}
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
第四题:
解题思路:
递推。
\(a_1 = 0\)和\(a_1 = 2\)时序列唯一。
对于\(a_1 = 1\),对\(x_1 = 1和x_2 = 1\)分类讨论。
注意:判掉\(x_i\)不为\(0或1\)的解法。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;
void solve()
{
int n;
cin >> n;
vector<int> a(n + 1), x(n + 1, 0);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
if (a[1] == 0)
{
for (int i = 3; i <= n; i++)
{
x[i] = a[i - 1] - (x[i - 1] + x[i - 2]);
}
}
else if (a[1] == 1)
{
x[2] = 1;
bool f = true;
for (int i = 3; i <= n; i++)
{
x[i] = a[i - 1] - (x[i - 1] + x[i - 2]);
if (x[i] < 0 || x[i] > 1)
{
f = false;
}
}
if (x[n - 1] + x[n] != a[n] || !f)
{
x[1] = 1;
x[2] = 0;
for (int i = 3; i <= n; i++)
{
x[i] = a[i - 1] - (x[i - 1] + x[i - 2]);
}
}
}
else if (a[1] == 2)
{
x[1] = 1;
x[2] = 1;
for (int i = 3; i <= n; i++)
{
x[i] = a[i - 1] - (x[i - 1] + x[i - 2]);
}
}
for (int i = 1; i <= n; i++)
{
cout << x[i] << " \n"[i == n];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}
第五题:
解题思路:
对于\(n = 2和n = 3\)的情况特殊讨论。所有元素都要变得相同。
整个序列只要按升序排序,头两个数字和尾两个数字相同即合法。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;
void solve()
{
int n;
cin >> n;
vector<int> a(n + 1), cnt(110, 0);
int mx = -1;
int sx = -1;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
cnt[a[i]]++;
}
for (int i = 1; i <= n; i++)
{
if (cnt[i] == 0)
{
continue;
}
if (cnt[i] > mx)
{
sx = mx;
mx = cnt[i];
}
else if (cnt[i] == mx)
{
sx = mx;
}
else if (cnt[i] > sx)
{
sx = cnt[i];
}
}
sort(a.begin() + 1, a.end());
if (n & 1)
{
if (n == 3)
{
cout << n - mx << "\n";
}
else
{
if (a[n - 1] != a[n] && a[1] != a[2])
{
if (a[n - 1] == a[n - 2] || a[2] == a[3])
{
cout << 1 << "\n";
}
else
{
cout << 2 << "\n";
}
}
else if (a[n - 1] == a[n] && a[1] == a[2])
{
cout << 0 << "\n";
}
else
{
cout << 1 << "\n";
}
}
}
else
{
if (n == 2)
{
cout << (a[1] != a[2]) << "\n";
}
else
{
if (a[n - 1] != a[n] && a[1] != a[2])
{
if (a[n - 1] == a[n - 2] || a[2] == a[3])
{
cout << 1 << "\n";
}
else
{
cout << 2 << "\n";
}
}
else if (a[n - 1] == a[n] && a[1] == a[2])
{
cout << 0 << "\n";
}
else
{
cout << 1 << "\n";
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
第六题:
解题思路:
可达的最小值到最大值之间的所有数都一定可以得到。
只要\(x\)在可达的最大值和最小值之间,那么就是可以恰好得到的。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using piii = pair<ll, pair<ll, ll>>;
const ll inf = 1ll << 60;
using ull = unsigned long long;
// using i128 = __int128_t;
const int N = 210 + 10;
void solve()
{
int n, x;
cin >> n >> x;
vector<int> a(n + 2);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
vector<vector<int>> dp(n + 2, vector<int>(2));
dp[1][0] = dp[0][0] + a[1];
dp[1][1] = dp[0][1] + a[1];
for (int i = 2; i <= n + 1; i++)
{
dp[i][0] = min(dp[i - 1][0], dp[i - 2][0]) + a[i];
}
for (int i = 2; i <= n + 1; i++)
{
dp[i][1] = max(dp[i - 1][1], dp[i - 2][1]) + a[i];
}
if (dp[n + 1][0] <= x && x <= dp[n + 1][1])
{
cout << "Yes\n";
}
else
{
cout << "No\n";
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
标签:入门,int,ll,long,小白,pair,using,define
From: https://www.cnblogs.com/value0/p/18091749