结营考核
文章目录
- A.门牌制作
- B.既约分数
- C.蛇形填数
- D.跑步锻炼
- E.七段码
- F.成绩统计
- G.回文日期
- H.字串分值和
- I.平面切分
- J.字串排序
A.门牌制作
代码:
num = 0
for i in range(1, 2021):
i = str(i)
num += i.count('2')
print(num)
答案: 624
B.既约分数
代码:
num = 0
# 辗转相除
def func(a, b):
x = a % b
while x != 0:
a = b
b = x
x = a % b
return b
for i in range(1, 2021):
for j in range(1, 2021):
if func(i, j) == 1:
num += 1
print(num)
答案:2481215
C.蛇形填数
代码:
dir = 0 # 0代表向右,1代表向下
a = [[0 for i in range(100)] for j in range(100)]
x, y = 0, 0
num = 1
while True:
if x == 19 and y == 19:
break
while dir == 0:
x, y = x, y+1
num += 1
while y != 0:
x += 1
y -= 1
num += 1
if x == 19 and y == 19:
print(num)
break
dir = 1
while dir == 1:
x, y = x+1, y
num += 1
while x != 0:
x -= 1
y += 1
num += 1
if x == 19 and y == 19:
print(num)
break
dir = 0
答案: 761
D.跑步锻炼
代码:
year = 2000
month = 1
day = 1
week = 6
res = 0
dateday = [0,31,28,31,30,31,30,31,31,30,31,30,31]
# 闰年判断
def run(year):
if year % 4 == 0 and year % 100 != 0:
return True
elif year % 400 == 0:
return True
else:
return False
while year != 2020 or month != 10 or day != 1:
if run(year):
dateday[2] = 29
else:
dateday[2] = 28
day += 1
week = (week+1)%7
if day > dateday[month]:
day = 1
month += 1
if month > 12:
month = 1
year += 1
if day == 1 or week == 1:
res += 1
res += 1
res += 2
print(res)
答案: 8879
E.七段码
代码:
这个可以数发光的个数:
1 : 7种
2: 10种
3: 16种
4: 20种
5: 19种
6: 7种
7: 1种
ans = 7 + 10 + 16 + 20 + 19 + 7 + 1 = 80
一共80种
F.成绩统计
代码:
n = int(input())
num1 = 0
num2 = 0
for i in range(n):
stu = int(input())
if stu >= 60:
num1 += 1
if stu >= 85:
num2 += 1
print('{:d}%'.format(round(num1/n*100)))
print('{:d}%'.format(round(num2/n*100)))
G.回文日期
代码:
from calendar import isleap
n = int(input())
def check(year):
month = int(year[:2])
day = int(year[2:4])
year = int(year)
if 1 <= month <= 12:
if month == 2:
if isleap(year):
if day <= 29:
return True
else:
return False
else:
if day <= 28:
return True
else:
return False
if month in [1,3,5,7,8,10,12]:
if day <= 31:
return True
else:
return False
else:
if day <= 30:
return True
return False
return False
arr1 = []
arr2 = []
for i in range(1000, 9999):
x1 = str(i)[::-1]
x2 = (str(i)[:2]+str(i)[:2])[::-1]
if check(x1) and int(x1[::-1]+x1)>n:
arr1.append(int(x1[::-1]+x1))
if check(x2) and int(x2[::-1]+x2)>n:
arr2.append(int(x2[::-1]+x2))
print(arr1[0])
print(arr2[0])
H.字串分值和
代码:
暴力解法肯定不能ac,大概50%是可以过的
s = input()
n = len(s)
res = 0
left = 0
right = 0
while left < n:
if right == n:
left += 1
right = left
right += 1
s_ = set(s[left:right])
res += len(s_)
print(res)
I.平面切分
代码:
n = int(input())
li = []
for i in range(n):
li.append(list(map(int, input().split())))
res = 2
node = []
for i in range(1, n):
for j in range(i):
if li[i][0] != li[j][0]:
x = (li[i][1] - li[j][1]) / (li[j][0] - li[i][0])
y = li[j][0] * x + li[j][1]
if [x, y] in node:
pass
else:
node.append([x, y])
res += len(node) + 1
node = []
print(res)
J.字串排序
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 135, M = 10010;
int f[N][30][N];
//chcnt[i][j]记录第i个位置取字母j+'a'的逆序对最大值
int chcnt[N][30];
//mlen[i]记录每个位置的最大值
int mlen[N];
void dp()
{
for (int i = 2; i < N; ++i)
{
int m = 0;
for (int j = 1; j <= 'z' - 'a'; ++j)
{
for (int k = 1; k < i; ++k)
{
if (k > 1) f[i][j][k] = f[i - 1][j][k - 1] + i - k;
else f[i][j][k] = chcnt[i - 1][j - 1] + i - 1;
chcnt[i][j] = max(chcnt[i][j], f[i][j][k]);
}
m = max(m, chcnt[i][j]);
}
mlen[i] = m;
}
}
int main()
{
dp();
int score = 0;
cin >> score;
//找出最短长度值
int beg = 0;
for (int i = 1; i < N; ++i)
if (mlen[i] >= score)
{
beg = i;
break;
}
int curr = 0; //用于记录逆序值
int same = 1; //记录后缀中有多少个相同字母
char last = 'z' + 1;//记录上一个字母是什么
for (int i = beg; i > 0; --i)
{
//从a开始枚举
int j = 0;
for (; j <= last - 'a'; ++j)
{
if (j == last - 'a') curr -= same;
if (curr + chcnt[i][j] >= score)
{
curr += i - 1;
break;
}
}
if (j == last - 'a') same++;
else
{
last = j + 'a';
same = 1;
}
cout << last;
}
cout << endl;
return 0;
}