元旦欢乐赛#37题解
2025-01-13 13:22:25
发布于:北京
前言:
这次题难度相比欢乐赛#35和#36略有提升,但还是要比#34及以前的欢乐赛简单。
互动跳转[1]
正式题解:
T1:
这是一道很水的题,只要用C++中的cout
语句或Python中的print()
函数即可。
C++代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
long long a=114514,b=1919810;
cout << a*b;
}
Python代码:
print(114514*1919810)
T2:
这道题我们第一行先获取到一个测试用例数目T
,然后循环T
次,之后数位用索引依次累加,最后用
即可确定数位之和是奇数还是偶数。
C++代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
for(int i = 1;i <= t;i++){
int s = 0;
string n; //存储为string类型,方便去得某位数字
cin >> n;
for(int j = 0;j <= n.size()-1;j++){
int k = n[j] - '0'; //不减去'0'就会取到其数的ASCII码
s += k;
}
s % 2 == 0 ? cout << "NO" : cout << "YES"; //三目运算符
cout << endl;
}
}
Python代码:
t = int(input())
for i in range(t):
s = 0
n = input()
for i in n:
s += int(i)
if s % 2 == 0:
print("NO")
else:
print("YES")
T3:
因为,所以这题我们可以使用C++的<algorithm>
头文件中的__gcd()
函数或Python的math
库中的gcd()
函数。
C++代码:
#include<bits/stdc++.h> //里面直接包含<algorithm>头文件
using namespace std;
int main(){
int a,b;
cin >> a >> b;
cout << a*b/__gcd(a,b);
}
Python代码:
import math
a,b = map(int,input().split())
print(int(a*b/math.gcd(a,b)))
T4:
这题我们可以用C++的for循环去拼接字符,或者用Pythona[-1:0:-1] + a[0]
的索引方式将字符串反转字符串
C++代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
string t1,t2;
cin >> t1;
for(int i=t1.size();i>=0;i--){
t2 += t1[i];
}
cout << t1 << t2;
}
Python代码:
t1 = input()
t2 = t1[-1:0:-1] + t1[0]
print(f"{t1}{t2}")
T5:
首先我们用sort()
函数对数组进行排序,用 m = (n - 1) // 2
找到中间值的位置。
用 target = a[m] + 1
将中间值提升到比其当前值大的下一个数。
对中间值及其右侧的所有元素进行操作,使这些元素不小于 target
,并统计所需的操作次数。
C++代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
sort(a, a + n);
int m = (n - 1) / 2; // 找到中间值的位置
int target = a[m] + 1; // 计算中间值
int c = 0;
for(int i = m; i < n; i++){
if (a[i] < target){
c += target - a[i];
}
}
cout << c << endl;
}
}
Python代码:
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
a.sort() #对列表排序
m = (n - 1) // 2 #找到中间值的位置
c = 0
target = a[m] + 1
for i in range(m, n):
if a[i] < target:
c += target - a[i] #累加需要增加的次数
print(c)
T6:
首先我们要定义 3 × 3 的矩阵 g
,表示每个灯的按压次数。
然后创建一个 3 × 3 的数组 r
,初始时每个灯的状态为 1
(即灯是开启的)。
对于每个灯,计算它和其上下左右相邻灯的按压次数之和,若该和为奇数,则灯的状态变为关闭(0);若该和为偶数,则灯的状态保持开启(1)。
C++代码:
#include<bits/stdc++.h>
using namespace std;
int main() {
int g[3][3];
int r[3][3] = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}};
// 读取每个灯的按下次数
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
cin >> g[i][j];
}
}
// 遍历每个灯的位置
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
int p = g[i][j];
// 相加与当前位置相邻的灯的按压次数
if (i > 0) p += g[i - 1][j];
if (i < 2) p += g[i + 1][j];
if (j > 0) p += g[i][j - 1];
if (j < 2) p += g[i][j + 1];
r[i][j] = (p % 2 == 0) ? 1 : 0; // 三目运算符,偶数次按下为开,奇数次按下为关闭
}
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
cout << r[i][j];
}
cout << endl;
}
}
Python代码:
g = []
for i in range(3):
t = list(map(int, input().split()))
g.append(t)
# 相加与当前位置相邻的灯的按压次数
r = [[1,1,1],[1,1,1],[1,1,1]]
for i in range(3):
for j in range(3):
p = g[i][j]
if i > 0:
p += g[i - 1][j]
if i < 2:
p += g[i + 1][j]
if j > 0:
p += g[i][j - 1]
if j < 2:
p += g[i][j + 1]
if p % 2:
r[i][j] = 0
else:
r[i][j] = 1
for i in range(3):
for j in range(3):
print(r[i][j], end='')
print()
结语:
上次题解里没写C++代码,这次为了写题解我还特意写了非常难写的C++语言代码......
请给我点个赞吧!
互动(来试试能不能看到“XXXX”四个字和“!”符号,能看到的把字发在评论区):
看上面 ↩︎
全部评论 25
2025-01-11 来自 北京
02025-01-11 来自 北京
02025-01-11 来自 北京
0e
2025-01-12 来自 北京
0
666
2025-01-11 来自 江苏
0?
2025-01-11 来自 北京
0
顶
2025-01-10 来自 北京
0顶
2025-01-10 来自 北京
0顶
2025-01-10 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0顶
2025-01-09 来自 北京
0
有帮助,赞一个