Python偷懒行为图鉴

算法

日期题: datetime

日期差值填空

众所周知,2025 年是完全平方年,因为
定义一个年份 是完全平方年当且仅当存在一个正整数 使得
今天是 2025 年 3 月 29 日,现在我们想知道距离下一个完全平方年的 1 月 1 日还有几天(即从 2025 年 3 月 29 日 0 时到下次完全平方年的 1 月 1 日 0 时,例如:从 3 月 29 日 0 时到 3 月 30 日 0 时算 1 天)。

1
2
3
4
from datetime import datetime as dt
a=dt(2025,3,29,0,0,0)
b=dt(46**2,1,1,0,0,0)
print((b-a).days)

P10385 [蓝桥杯 2024 省 A] 艺术与篮球

小蓝决定根据日期的笔画数来安排自己的练习。首先,他会将当天的日期按照 YYYYMMDD 的格式
转换成一个 位数,然后将这 位数对应到汉字上,计算这些汉字的总笔画数。如果总笔画数超过 ,他就去练习篮球;如果总笔画数不超过 ,他就去练习书法。
例如,在 日这天,日期可表示为一个 位数字 ,其转换为汉字是“二零二四零一零一”。日期的总笔画数为 ,因此在这天,小蓝会去练习书法。
以下是汉字的笔画数对照表:

汉字 笔画数 汉字 笔画数

现在,请你帮助小蓝统计一下,在 日到
这段时间内,小蓝有多少天是在练习篮球?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from datetime import datetime,timedelta
a=datetime(2000,1,1,0,0,0)
b=datetime(2024,4,13,0,0,0)
c=a
d=timedelta(days=1)
bh=[13,1,2,3,5,4,4,2,2,2]
s=0
def cnt(x,n):
ret=0
for i in range(n):
ret+=bh[x%10]
x//=10 # 注意python除法
return ret

while c<=b:
s+=1 if cnt(c.year,4)+cnt(c.month,2)+cnt(c.day,2)>50 else 0
c+=d

print(s)

大数/高精度题: decimal

最高位之和

对于正整数 ,设 在十进制下的最高非零位的值, 在十进制下的最高非零位的值,求所有可能的作为 的值的和。相同的值只计算一次。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from decimal import Decimal
two=Decimal('2')
five=Decimal('5')
s=set()
def high(x):
while x>9:
x//=10
return x

for i in range(50000):
try:
s.add(high(two)*high(five))
two*=2
five*=5
# print(two,five)
except Exception as e:
# print(str(e))
break
res=0
for i in s:
res+=i
print(res)

对拍

待测程序

待测程序test.cpp

1
2
3
4
5
6
7
8
9
#include<bits/stdc++.h>
using namespace std;
int main()
{
freopen("input.txt","r",stdin);
freopen("output1.txt","w",stdout);
//待测程序
return 0;
}

对拍程序

对拍程序bl.cpp

1
2
3
4
5
6
7
8
9
#include<bits/stdc++.h>
using namespace std;
int main()
{
freopen("input.txt","r",stdin);
freopen("output2.txt","w",stdout);
//待测程序
return 0;
}

Python简易评测机

没用过,先放一个在这儿
python简易对拍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
import random
import subprocess
import time
import filecmp

def test():
with open("input.txt", "w") as f:
'''
生成随机数据,例
n = random.randint(1, 100)
f.write(f"{n}\n")
for _ in range(n):
l = random.randint(1, 100)
r = random.randint(1, 100)
f.write(f"{l} {r}\n")
'''
start_time=time.time()
subprocess.run(["test.exe"], shell=True) # 测试
print(f"用时{time.time()-start_time:.3f}s")
subprocess.run(["bl.exe"], shell=True) # 对拍
return filecmp.cmp("output1.txt", "output2.txt")

random.seed(time.time())
for i in range(100): # 测试数量
print(f"运行测试例 {i}...")
if not test():
print("测试例 {i} 失败")
exit(0)
print("通过")

参考:https://blog.csdn.net/Mibbp/article/details/124012372