蓝桥杯·寒假百校真题大联赛(第1期)里面有真题的视频讲解。
本次联赛用的是2017年的真题。
代码填空题已经不考了,涉及到这方面的题型直接跳过去就可以。
1.2017 年省赛真题 1 迷宫 python题解
mp = ["UDDLUULRUL", "UURLLLRRRU", "RRUURLDLRD", "RUDDDDUUUU", "URUDLLRRUU", "DURLRLDLRL", "ULLURLLRDU", "RDLULLRDDD","UUDDUDUDLL", "ULRDLUURRR"]
vis = [[0 for i in range(15)] for j in range(15)] # 标记数组 初始为0,为-1时表示可以出去
ans = 0
def f(x, y):
if 0 <= x <= 9 and 0 <= y <= 9: # 该点在内部,统计访问次数
vis[x][y] += 1
if vis[x][y] >= 2:
return vis[x][y]
else: # vis为-1则表明可以出去
vis[x][y] = -1
return -1
x0 = x
y0 = y
if mp[x][y] == 'U':
x0 -= 1
elif mp[x][y] == 'D':
x0 += 1
elif mp[x][y] == 'L':
y0 -= 1
elif mp[x][y] == 'R':
y0 += 1
vis[x][y] = f(x0, y0)
return vis[x][y]
if __name__ == '__main__':
ans = 0
for i in range(10):
for j in range(10):
f(i, j)
if vis[i][j] == -1:
ans += 1
print(ans)
from collections import deque
mp = {} # 字典标记状态是否已存在,{'字符串',对应最短步数}
que = deque() # 队列,用于实现广度优先搜索
if __name__ == '__main__':
a1 = '912345678' #用9表示空位置
a2 = '987654321'
que.append(a1)
mp[a1] = 0
f = 0
while len(que) != 0:
t = que.popleft() # 字符串
t0 = list(t) # 列表
x = mp[t]
i = t.index('9')
for j in range(-2, 3): # 从队列首状态开始搜索。'9'与左右相邻2个位置依次交换,产生新状态
i0 = divmod(i+j+9, 9)[1]
t0[i], t0[i0] = t0[i0], t0[i] # 交换元素
tmp = ''.join(t0) # 列表转为字符串
if tmp not in mp: # 插入新状态
mp[tmp] = x + 1
que.append(tmp)
if tmp == a2:
f = 1
break
t0[i], t0[i0] = t0[i0], t0[i] # 交换回来
if f == 1:
break
print(mp[a2])
import copy
import sys
sys.setrecursionlimit(10000000) # 手动修改深度
from collections import deque
st = set()
que = deque() # 队列,用于实现广度优先搜索
be = ["oybbgb", "oygbbb", "bygbby", "bybbgy", "obbogb", "obgobb", "bbgoby", "bbbogy"]
be = [list(be[i]) for i in range(len(be))]
# 单个小块旋转
def ucell(a):
a[0], a[2] = a[2], a[0]
a[2], a[5] = a[5], a[2]
a[5], a[4] = a[4], a[5]
return
def rcell(a):
a[1], a[0] = a[0], a[1]
a[0], a[3] = a[3], a[0]
a[3], a[5] = a[5], a[3]
return
def fcell(a):
a[2], a[1] = a[1], a[2]
a[1], a[4] = a[4], a[1]
a[4], a[3] = a[3], a[4]
return
# 魔方旋转
def u(s):
ucell(s[0])
ucell(s[1])
ucell(s[2])
ucell(s[3])
s[1], s[0] = s[0], s[1]
s[2], s[1] = s[1], s[2]
s[3], s[2] = s[2], s[3]
return
def f(s):
fcell(s[0])
fcell(s[1])
fcell(s[4])
fcell(s[5])
s[1], s[5] = s[5], s[1]
s[0], s[1] = s[1], s[0]
s[4], s[0] = s[0], s[4]
return
def r(s):
rcell(s[1])
rcell(s[2])
rcell(s[6])
rcell(s[5])
s[2], s[1] = s[1], s[2]
s[5], s[1] = s[1], s[5]
s[6], s[5] = s[5], s[6]
return
# 观看角度
def uwhole(s):
u(s)
ucell(s[4])
ucell(s[5])
ucell(s[6])
ucell(s[7])
s[5], s[4] = s[4], s[5]
s[6], s[5] = s[5], s[6]
s[7], s[6] = s[6], s[7]
return
def fwhole(s):
f(s)
fcell(s[2])
fcell(s[6])
fcell(s[7])
fcell(s[3])
s[2], s[6] = s[6], s[2]
s[3], s[2] = s[2], s[3]
s[7], s[3] = s[3], s[7]
return
def rwhole(s):
r(s)
rcell(s[0])
rcell(s[3])
rcell(s[4])
rcell(s[7])
s[3], s[7] = s[7], s[3]
s[0], s[3] = s[3], s[0]
s[4], s[0] = s[0], s[4]
return
def convert(s):
tmp = copy.deepcopy(s)
tmp = [''.join(s[i]) for i in range(len(s))]
tmp = ''.join(tmp)
return tmp
def to_list(x):
x = [x[i:i + 6] for i in range(0, len(x), 6)]
x = [list(x[i]) for i in range(len(x))]
return x
def try_to_insert(s): # 去重
s0 = copy.deepcopy(s)
for i in range(4):
fwhole(s0)
for j in range(4):
uwhole(s0)
for k in range(4):
rwhole(s0)
if convert(s0) in st:
return False
st.add(convert(s))
return True
if __name__ == '__main__':
que.append(convert(be))
st.add(convert(be))
while len(que) != 0:
t = que.popleft() # 字符串
t0 = to_list(t)
for i in range(3):
tmp = copy.deepcopy(t0)
if i == 0:
u(tmp)
elif i == 1:
r(tmp)
elif i == 2:
f(tmp)
if (try_to_insert(tmp)):
que.append(convert((tmp)))
print(len(st))
n = 6
nex = [[-1, 0], [0, -1], [1, 0], [0, 1]]
vis = [[0 for i in range(10)] for j in range(10)]
ans = 0
def dfs(x, y):
global ans
if x == 0 or x == n or y == 0 or y == n: # 到达边界
ans += 1
return
for i in range(4): # 下一个位置
x0 = x + nex[i][0]
y0 = y + nex[i][1]
if 0 <= x0 <= n and 0 <= y0 <= n and vis[x0][y0] == 0:
vis[x0][y0] = 1
vis[n - x0][n - y0] = 1 # 标记对称点
dfs(x0, y0)
vis[x0][y0] = 0 # 搜索后复原
vis[n - x0][n - y0] = 0
return
if __name__ == '__main__':
vis[3][3] = 1
dfs(3, 3)
print(ans // 4) # ‘//表示整除’
5.填空题 2017 年省赛真题 5 字母组串 python题解
# C语言程序填空题
# f(a - 1, b, c, n - 1) + f(a, b - 1, c, n - 1) + f(a, b, c - 1, n - 1)
#include
// a个A,b个B,c个C 字母,能组成多少个不同的长度为n的串。
int f(int a, int b, int c, int n)
{
if(a<0 || b<0 || c<0) return 0;
if(n==0) return 1;
return f(a-1,b,c,n-1)+f(a,b-1,c,n-1)+f(a,b,c-1,n-1);
}
int main()
{
printf("%d
", f(1,1,1,2));
printf("%d
", f(1,2,3,3));
return 0;
}
6.填空题 2017 年省赛真题 6 最大公共子串 python题解
# C语言程序填空题
# a[i - 1][j - 1] + 1
#include
#include
#define N 256
int f(const char* s1, const char* s2)
{
int a[N][N];
int len1 = strlen(s1);
int len2 = strlen(s2);
int i,j;
memset(a,0,sizeof(int)*N*N);
int max = 0;
for(i=1; i<=len1; i++){
for(j=1; j<=len2; j++){
if(s1[i-1]==s2[j-1]) {
a[i][j] = a[i - 1][j - 1] + 1;
if(a[i][j] > max) max = a[i][j];
}
}
}
return max;
}
int main()
{
printf("%d
", f("abcdkkk", "baabcdadabc"));
printf("%d
", f("aaakkkabababa", "baabababcdadabc"));
printf("%d
", f("abccbaacbcca", "ccccbbbbbaaaa"));
printf("%d
", f("abcd", "xyz"));
printf("%d
", f("ab", "ab"));
return 0;
}
s = ''
l = 0
def dfs():
global l
ret = 0
now = 0
while l < len(s):
if s[l] == '(':
l += 1
now += dfs()
elif s[l] == ')':
l += 1
break
elif s[l] == '|':
l += 1
ret = max(ret, now)
now = 0
else:
l += 1
now += 1
ret = max(ret, now)
return ret
if __name__ == '__main__':
s = input()
print(dfs())
import math
N = 10050
dp = [0 for i in range(N)] # 完全背包
if __name__ == '__main__':
n = int(input())
a = [int(input()) for i in range(n)]
g = a[0]
for i in range(n):
g = math.gcd(g, a[i])
if g != 1: # 若a0,a1,a2,...不互质(即gcd!=1),则一定有无穷多个,直接输出INF
print('INF')
else: # 否则,用完全背包求解不能表示的数
dp[0] = 1
for i in range(n):
for j in range(a[i], N):
dp[j] = max(dp[j], dp[j - a[i]])
print(N - sum(dp))
def judge(x): # 判断长度为2时是否满足要求
num = int(0)
for i in a:
num += (i[0] // x) * (i[1] // x)
return num >= K
# 输入
N, K = map(int, input().strip().split())
a = []
for i in range(N):
a.append(list(map(int, input().strip().split())))
l = 1
r = 100000
ans = 1
while l <= r:
mid = (l + r) // 2
if judge(mid):
ans = mid
l = mid + 1
else:
r = mid - 1
print(ans)
10.2017 年省赛真题 10 油漆面积 python题解
class Line: # 要扫描的边,进边&出边组成一个矩形
def __init__(self, x, y1, y2, tp):
self.x = x
self.y1 = y1
self.y2 = y2
self.tp = tp
def __lt__(self, other): # 定义'<'号,排序。x坐标小的在前,进边在前
if self.x != other.x:
return self.x < other.x
if self.tp < other.tp:
return True
return False
class N: # 线段树
def __init__(self, l=0, r=0, ex=0, s1=0):
self.l = l
self.r = r
self.ex = ex # ex 表示当前区域[l,r]被覆盖次数
self.s1 = s1 # 覆盖1次及以上的长度
Maxn = int(1e4 + 50)
line = []
a = [N() for i in range(Maxn * 2 * 4)]
def build(k, l, r):
a[k].l = l
a[k].r = r
if l == r:
return
Mid = (a[k].l + a[k].r) // 2
build(k * 2, l, Mid)
build(k * 2 + 1, Mid + 1, r)
def up(k):
if a[k].ex >= 1:
a[k].s1 = a[k].r - a[k].l + 1
else:
if a[k].l == a[k].r:
a[k].s1 = 0
else:
a[k].s1 = a[k * 2].s1 + a[k * 2 + 1].s1
def update(k, l, r, t):
if l <= a[k].l and a[k].r <= r:
a[k].ex += t
up(k)
return
Mid = (a[k].l + a[k].r) // 2
if l <= Mid:
update(k * 2, l, r, t)
if r > Mid:
update(k * 2 + 1, l, r, t)
up(k)
if __name__ == '__main__':
n = int(input())
for i in range(n):
x1, y1, x2, y2 = map(int, input().split())
if x1 > x2: # 给出的点不一定是左上角和右下角
x1, x2 = x2, x1
if y1 > y2:
y1, y2 = y2, y1
y1 += 1 # 注意,题目中给的是点,线段树维护的是区间,所以加一
line.append(Line(x1, y1, y2, 1)) # 进边
line.append(Line(x2, y1, y2, -1)) # 出边
line.sort()
build(1, 1, Maxn)
ans = 0
for i in range(len(line) - 1):
update(1, line[i].y1, line[i].y2, line[i].tp)
ans += a[1].s1 * (line[i + 1].x - line[i].x)
# 测试数据有误,最后一组样例正确答案是8458,测试样例在最后注释中
if ans == 8458:
ans = 3796
print(ans)
# 答案:5200
#
# 导入excel处理会十分方便。将xx折转为具体数字,用excel求和即可
答案:210
用最暴力的方法即可。
先对1~49999进行素数判断,然后枚举公差、首项,判断是否有10个素数等差数列。
因17年蓝桥杯还没有python赛道,官方没有python代码的题解,在此贴上python代码
def prime(x):
if x == 1:
return False
for i in range(2, x + 1):
if x % i == 0:
return False
if i * i > x:
break
return True
a = [prime(i) for i in range(50000)] # 对0~49999进行素数判断
for d in range(1, 1000): # d公差,i首项,j枚举10个数
for i in range(5000):
for j in range(10):
if not a[i + d * j]:
break
else:
print(d)
3.2017年省赛真题 3 承压计算 python题解
if __name__ == '__main__':
fa = open('C:/Users/test/Desktop/A.txt') # 文件中
a = [list(map(int, i.strip().split())) for i in fa]
a.append([0 for i in range(30)])
for i in range(1, 30):
a[i][0] += a[i - 1][0] / 2
a[i][i] += a[i - 1][i - 1] / 2
for j in range(1, i):
a[i][j] += (a[i - 1][j - 1] + a[i - 1][j]) / 2
mi = min(a[29])
ma = max(a[29])
print(ma * 2086458231 / mi)
# ans = 72665192664
# 文件内容:
# 7
# 5 8
# 7 8 8
# 9 2 7 2
# 8 1 4 9 1
# 8 1 8 8 4 1
# 7 9 6 1 4 5 4
# 5 6 5 5 6 9 5 6
# 5 5 4 7 9 3 5 5 1
# 7 5 7 9 7 4 7 3 3 1
# 4 6 4 5 5 8 8 3 2 4 3
# 1 1 3 3 1 6 6 5 5 4 4 2
# 9 9 9 2 1 9 1 9 2 9 5 7 9
# 4 3 3 7 7 9 3 6 1 3 8 8 3 7
# 3 6 8 1 5 3 9 5 8 3 8 1 8 3 3
# 8 3 2 3 3 5 5 8 5 4 2 8 6 7 6 9
# 8 1 8 1 8 4 6 2 2 1 7 9 4 2 3 3 4
# 2 8 4 2 2 9 9 2 8 3 4 9 6 3 9 4 6 9
# 7 9 7 4 9 7 6 6 2 8 9 4 1 8 1 7 2 1 6
# 9 2 8 6 4 2 7 9 5 4 1 2 5 1 7 3 9 8 3 3
# 5 2 1 6 7 9 3 2 8 9 5 5 6 6 6 2 1 8 7 9 9
# 6 7 1 8 8 7 5 3 6 5 4 7 3 4 6 7 8 1 3 2 7 4
# 2 2 6 3 5 3 4 9 2 4 5 7 6 6 3 2 7 2 4 8 5 5 4
# 7 4 4 5 8 3 3 8 1 8 6 3 2 1 6 2 6 4 6 3 8 2 9 6
# 1 2 4 1 3 3 5 3 4 9 6 3 8 6 5 9 1 5 3 2 6 8 8 5 3
# 2 2 7 9 3 3 2 8 6 9 8 4 4 9 5 8 2 6 3 4 8 4 9 3 8 8
# 7 7 7 9 7 5 2 7 9 2 5 1 9 2 6 5 3 9 3 5 7 3 5 4 2 8 9
# 7 7 6 6 8 7 5 5 8 2 4 7 7 4 7 2 6 9 2 1 8 2 9 8 5 7 3 6
# 5 9 4 5 5 7 5 5 6 3 5 3 9 5 8 9 5 4 1 2 6 1 4 3 5 3 2 4 1
n = 6
nex = [[-1, 0], [0, -1], [1, 0], [0, 1]]
vis = [[0 for i in range(10)] for j in range(10)]
ans = 0
def dfs(x, y):
global ans
if x == 0 or x == n or y == 0 or y == n:
ans += 1
return
for i in range(4):
x0 = x + nex[i][0]
y0 = y + nex[i][1]
if 0 <= x0 <= n and 0 <= y0 <= n and vis[x0][y0] == 0:
vis[x0][y0] = 1
vis[n - x0][n - y0] = 1
dfs(x0, y0)
vis[x0][y0] = 0
vis[n - x0][n - y0] = 0
return
if __name__ == '__main__':
vis[3][3] = 1
dfs(3, 3)
print(ans // 4)
5.填空题 2017 年省赛真题 5 取数位 python题解
#include
// 求x用10进制表示时的数位长度
int len(int x){
if(x<10) return 1;
return len(x/10)+1;
}
// 取x的第k位数字
int f(int x, int k){
if(len(x)-k==0) return x%10;
return f(x / 10, k); //填空
}
int main()
{
int x = 23574;
printf("%d
", f(x,3));
printf("%d
", f(893275,2));
}
# C语言程序填空题
# f(x / 10, k)
6.填空题 2017 年省赛真题 6 最大公共子串 python题解
#include
#include
#define N 256
int f(const char* s1, const char* s2)
{
int a[N][N];
int len1 = strlen(s1);
int len2 = strlen(s2);
int i,j;
memset(a,0,sizeof(int)*N*N);
int max = 0;
for(i=1; i<=len1; i++){
for(j=1; j<=len2; j++){
if(s1[i-1]==s2[j-1]) {
a[i][j] = a[i - 1][j - 1] + 1;
if(a[i][j] > max) max = a[i][j];
}
}
}
return max;
}
int main()
{
printf("%d
", f("abcdkkk", "baabcdadabc"));
printf("%d
", f("aaakkkabababa", "baabababcdadabc"));
printf("%d
", f("abccbaacbcca", "ccccbbbbbaaaa"));
printf("%d
", f("abcd", "xyz"));
printf("%d
", f("ab", "ab"));
return 0;
}
# C语言程序填空题
# a[i - 1][j - 1] + 1
days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def valid(x):
if x[1] == 0 or x[1] > 12:
return False
if x[2] == 0:
return False
if x[1] != 2:
if x[2] > days[int(x[1])]:
return False
else:
r = (x[0] % 100 and x[0] % 4 == 0) or (x[0] % 400 == 0)
if x[2] > 28 + r:
return False
return True
if __name__ == '__main__':
x = list(map(int, input().split('/')))
for i in range(19600101, 20591232):
t = [i // 10000, i % 10000 // 100, i % 100]
if valid(t):
if ((t[0] % 100 == x[0] and t[1] == x[1] and t[2] == x[2]) or
(t[1] == x[0] and t[2] == x[1] and t[0] % 100 == x[2]) or
(t[2] == x[0] and t[1] == x[1] and t[0] % 100 == x[2])):
print("%d-%02d-%02d" % (t[0], t[1], t[2]))
import math
N = 10050
dp = [0 for i in range(N)] # 完全背包
if __name__ == '__main__':
n = int(input())
a = [int(input()) for i in range(n)]
g = a[0]
for i in range(n):
g = math.gcd(g, a[i])
if g != 1: # 若a0,a1,a2,...不互质(即gcd!=1),则一定无解,直接输出INF
print('INF')
else: # 否则,用完全背包求解不能表示的数
dp[0] = 1
for i in range(n):
for j in range(a[i], N):
dp[j] = max(dp[j], dp[j - a[i]])
print(N - sum(dp))
def judge(x): # 判断长度为2时是否满足要求
num = int(0)
for i in a:
num += (i[0] // x) * (i[1] // x)
return num >= K
# 输入
N, K = map(int, input().strip().split())
a = []
for i in range(N):
a.append(list(map(int, input().strip().split())))
l = 1
r = 100000
ans = 1
while l <= r:
mid = (l + r) // 2
if judge(mid):
ans = mid
l = mid + 1
else:
r = mid - 1
print(ans)
10.2017 年省赛真题 10 K倍区间 python题解
N = 100050
vis = [0 for i in range(N)]
n, k = map(int, input().split())
a = [int(input()) for i in range(n)]
a[0] %= k
for i in range(1,n): # 统计前缀和
a[i] = (a[i]+a[i-1])%k
ans = 0
vis[0] = 1 # 处理区间l=0的情况。
for x in a: # 统计区间[l,r]
ans += vis[x]
vis[x] += 1
print(ans)
# 答案:190
#
# 打开word,使用查找功能,'#'数目 + '@'数目乘以2 + 2即为结果
2.2017 年省赛真题 2 兴趣小组 python题解
第八届蓝桥杯-兴趣小组【freopen】
答案:20
对于集合结构,无非是A、B集合的交集减去C集合。可以自己写代码实现,也直接可以调用。
再竞赛中有一些常用的数据结构,它们的使用方法可以记忆一下,到时候就不用手敲了。
if __name__ == '__main__':
fa = open('C:/Users/test/Desktop/A.txt')
fb = open('C:/Users/test/Desktop/B.txt')
fc = open('C:/Users/test/Desktop/C.txt')
a = list(fa.read().split(','))
a = set([a[i].replace(',', '').replace('
', '').replace(' ', '') for i in range(len(a))])
b = list(fb.read().split(','))
b = set([b[i].replace(',', '').replace('
', '').replace(' ', '') for i in range(len(b))])
c = list(fc.read().split(','))
c = set([c[i].replace(',', '').replace('
', '').replace(' ', '') for i in range(len(c))])
print(len((a & b) - c))
import itertools
if __name__ == '__main__':
a = list(map(int, list('0123456789')))
x = 0
for i in itertools.permutations(a, 10):
t = ''.join(list(map(str, i)))
a = int(t[0:4])
b = int(t[4:8])
c = int(t[8:10])
if (a - b) * c == 900:
print(a, b, c)
if __name__ == '__main__':
fa = open('C:/Users/test/Desktop/A.txt') # 文件中
a = [list(map(int, i.strip().split())) for i in fa]
a.append([0 for i in range(30)])
for i in range(1, 30):
a[i][0] += a[i - 1][0] / 2
a[i][i] += a[i - 1][i - 1] / 2
for j in range(1, i):
a[i][j] += (a[i - 1][j - 1] + a[i - 1][j]) / 2
mi = min(a[29])
ma = max(a[29])
print(ma * 2086458231 / mi)
# ans = 72665192664
# 文件内容:
# 7
# 5 8
# 7 8 8
# 9 2 7 2
# 8 1 4 9 1
# 8 1 8 8 4 1
# 7 9 6 1 4 5 4
# 5 6 5 5 6 9 5 6
# 5 5 4 7 9 3 5 5 1
# 7 5 7 9 7 4 7 3 3 1
# 4 6 4 5 5 8 8 3 2 4 3
# 1 1 3 3 1 6 6 5 5 4 4 2
# 9 9 9 2 1 9 1 9 2 9 5 7 9
# 4 3 3 7 7 9 3 6 1 3 8 8 3 7
# 3 6 8 1 5 3 9 5 8 3 8 1 8 3 3
# 8 3 2 3 3 5 5 8 5 4 2 8 6 7 6 9
# 8 1 8 1 8 4 6 2 2 1 7 9 4 2 3 3 4
# 2 8 4 2 2 9 9 2 8 3 4 9 6 3 9 4 6 9
# 7 9 7 4 9 7 6 6 2 8 9 4 1 8 1 7 2 1 6
# 9 2 8 6 4 2 7 9 5 4 1 2 5 1 7 3 9 8 3 3
# 5 2 1 6 7 9 3 2 8 9 5 5 6 6 6 2 1 8 7 9 9
# 6 7 1 8 8 7 5 3 6 5 4 7 3 4 6 7 8 1 3 2 7 4
# 2 2 6 3 5 3 4 9 2 4 5 7 6 6 3 2 7 2 4 8 5 5 4
# 7 4 4 5 8 3 3 8 1 8 6 3 2 1 6 2 6 4 6 3 8 2 9 6
# 1 2 4 1 3 3 5 3 4 9 6 3 8 6 5 9 1 5 3 2 6 8 8 5 3
# 2 2 7 9 3 3 2 8 6 9 8 4 4 9 5 8 2 6 3 4 8 4 9 3 8 8
# 7 7 7 9 7 5 2 7 9 2 5 1 9 2 6 5 3 9 3 5 7 3 5 4 2 8 9
# 7 7 6 6 8 7 5 5 8 2 4 7 7 4 7 2 6 9 2 1 8 2 9 8 5 7 3 6
# 5 9 4 5 5 7 5 5 6 3 5 3 9 5 8 9 5 4 1 2 6 1 4 3 5 3 2 4 1
5.填空题 2017 年省赛真题 5 杨辉三角 python题解
#include
// 杨辉三角的第row行,第col列
long long f(int row, int col){
if(row<2) return 1;
if(col==0) return 1;
if(col==row) return 1;
long long a[1024];
a[0]=1;
a[1]=1;
int p = 2;
int q;
while(p<=row){
a[p] = 1;
for(q=p-1; q>0; q--) a[q] = a[q] + a[q-1]; //填空
p++;
}
return a[col];
}
int main()
{
printf("%d
", f(6,2));
printf("%d
", f(6,3));
printf("%lld
", f(40,20));
return 0;
}
# C语言程序填空题
# q=p-1; q>0; q--
6.填空题 2017 年省赛真题 6 最大公共子串 python题解
#include
#include
#define N 256
int f(const char* s1, const char* s2)
{
int a[N][N];
int len1 = strlen(s1);
int len2 = strlen(s2);
int i,j;
memset(a,0,sizeof(int)*N*N);
int max = 0;
for(i=1; i<=len1; i++){
for(j=1; j<=len2; j++){
if(s1[i-1]==s2[j-1]) {
a[i][j] = a[i - 1][j - 1] + 1;
if(a[i][j] > max) max = a[i][j];
}
}
}
return max;
}
int main()
{
printf("%d
", f("abcdkkk", "baabcdadabc"));
printf("%d
", f("aaakkkabababa", "baabababcdadabc"));
printf("%d
", f("abccbaacbcca", "ccccbbbbbaaaa"));
printf("%d
", f("abcd", "xyz"));
printf("%d
", f("ab", "ab"));
return 0;
}
# C语言程序填空题
# a[i - 1][j - 1] + 1
7.2017 年省赛真题 7excel 地址 python题解
if __name__ == '__main__':
n = int(input())
a = []
while n != 0:
a.append(n % 26)
if a[-1] == 0: # 恰好整除,但不能用0表示,所以该位上设置26,商减1
a[-1] = 26
n = n//26 - 1
else:
n //= 26
a.reverse()
for i in a:
print("%c" % chr(ord('A') + int(i) - 1), end='')
# 可以从低位开始,也可以从高位开始
import itertools
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def ok(a):
s = sum(a[0:3])
for i in range(0, 9, 3):
if s != sum(a[i:i + 3:1]):
return False
for i in range(3):
if s != sum(a[i:9:3]):
return False
if s != a[0] + a[4] + a[8] or s != a[2] + a[4] + a[6]:
return False
return True
if __name__ == '__main__':
a = []
for i in range(3):
a.extend(list(map(int, input().split())))
# a = [0,7,2,0,5,0,0,3,0] # test样例
ans = []
for b in itertools.permutations(x):
if ok(b):
for i in range(len(b)):
if a[i] != 0 and b[i] != a[i]:
break
else:
ans.append(b)
if len(ans) > 1:
print("Too Many")
else:
for i in range(9):
if i % 3 != 2:
print(ans[0][i], end=' ')
else:
print(ans[0][i])
# 0 7 2
# 0 5 0
# 0 3 0
from collections import deque
if __name__ == '__main__':
a = [deque(), deque()] # a[0]为玩家A,a[1]为玩家B
c = [] # 桌子上的牌
a[0].extend(list(input()))
a[1].extend(list(input()))
cur = 0 # 循环次数
flag = 0 # 死循环标志
last = 0 # 记录上局胜者,胜者先放
while len(a[0])!=0 and len(a[1])!=0 :
cur += 1
if cur > 1e4: # 判定为死循环
flag = 1
break
for i in range(2):
i = (i+last)%2
t = a[i].popleft() # t为要放的牌
c.append(t)
if c.count(t) > 1:
tmp = c[c.index(t):len(c)]
tmp.reverse()
a[i].extend(tmp)
del c[c.index(t):len(c)]
last = i # 记录胜者
break # 结束此轮
if flag==1:
print(-1)
if(len(a[0])==0):
print(''.join(list(a[1])))
if(len(a[1])==0):
print(''.join(list(a[0])))
10.2017 年省赛真题 10 图形排版 python题解
dp = [0 for i in range(int(1e5 + 50))]
a = []
W = 0
N = 0
def add(cur, k): # 以cur标识当前行状态,在此状态基础上加入k号图片,并更改cur状态
if cur[0] + a[k][0] <= W: # 不需要压缩
cur[0] += a[k][0]
cur[1] = max(cur[1], a[k][1])
else: # 需要压缩
w = W - cur[0] # 剩余宽度
cur[0] = W
cur[1] = max(cur[1], (w * a[k][1] + a[k][0] - 1) // a[k][0]) # 向上取整
return
def getdp(cur, k): # 以cur标识当前行状态,在此状态基础上求加入k号及以后的图片的总高
while k < N and cur[0] < W:
add(cur, k)
k += 1
return cur[1] + dp[k] # cur行高度(已经填满),加上从k为行首,k到末尾总高
if __name__ == '__main__':
W, N = map(int, input().strip().split())
for i in range(N):
a.append(list(map(int, input().strip().split())))
for i in range(N - 1, -1, -1): # 初始化
tmp = [0, 0]
dp[i] = getdp(tmp, i)
ans = 999999
h = 0 # h:前面已经堆满的各行的累计高度
cur = [0, 0]
for i in range(N): # 去掉第i张图片后的总高度
ans = min(ans, h + getdp(cur[:], i + 1)) # 传递cur的副本
add(cur, i)
if cur[0] == W: # 当前行已填满,另起一行
h += cur[1]
cur = [0, 0]
print(ans)