1.门牌制作
答案:624
2.既约分数
答案:248125
3.蛇形填数
答案:761
4.跑步锻炼
答案:8879
5.七段码
答案:80
import itertools
class UnionFindSet():
def __init__(self,n):
self.setSize = n #不连通区域
self.father = {i:-1 for i in range(7)}
def find(self,x):
root = x
while self.father[root] != -1: #找根节点
root = self.father[root]
while (x != root): #路径压缩
o = self.father[x] #找x的父节点
self.father[x] = root #把x的父节点设置成刚才找到的根
x = o #往上一层
return root
def merge(self, x, y):
root_x = self.find(x)
root_y = self.find(y)
if root_x != root_y:
self.father[root_x] = root_y #合并
self.setSize -= 1
dic = {
0: [1, 5], 1: [0, 2, 6], 2: [1, 3, 6], 3: [2, 4], 4: [3, 5, 6], 5: [0, 4, 6], 6: [1, 2, 4, 5]
}
ans, nums = 7, [x for x in range(7)]
for i in range(2,8):
for j in (itertools.combinations(nums, i)):
uf = UnionFindSet(len(j))
for k in range(len(j)):
for m in range(k + 1, len(j)):
if j[m] in dic[j[k]]:
uf.merge(j[m], j[k])
if uf.setSize == 1:
ans += 1
print(ans)
6.成绩统计
n = int(input())
you = 0
jige = 0
for i in range(n):
a = int(input())
if a >= 60:
jige += 1
if a >= 85:
you += 1
jige = format(jige / n, '.2f') # format方法可以四舍五入,但返回的是字符串形式
you = format(you / n, '.2f')
print(f'{jige[2:]}%')
print(f'{you[2:]}%')
7.回文日期
from calendar import isleap
gap_year = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
normal_year = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def judge(date, is_gap):
month = int(date[:2])
day = int(date[2:])
if month > 12:
return False
if is_gap:
return day < gap_year[month]
else:
return day < normal_year[month]
def judge_next_year(year: str):
res_year = year[::-1]
new_year = year + res_year
revers_str = new_year[::-1]
if new_year == revers_str:
is_gap = isleap(int(year))
flag = judge(res_year, is_gap)
if flag:
return [True, revers_str]
else:
return [False, revers_str]
else:
return [False, revers_str]
def get_next(data: str):
year = data[:4]
res = list()
while True:
year = str(int(year) + 1)
ans = judge_next_year(year)
flag, tmp = ans[0], ans[1]
if flag:
res.append(tmp)
break
num = int(data[0:2])
while True:
num += 1
year = str(num) + str(num)
ans = judge_next_year(year)
flag, tmp = ans[0], ans[1]
if flag:
res.append(tmp)
break
return res
if __name__ == '__main__':
data = input()
res = get_next(data)
for i in res:
print(i)
8.子串分值和
# 100分
list1=list(input())
list2=[-1 for i in range(26)]
count=0
for i in range(len(list1)):
index=ord(list1[i])-ord('a')
count+=(len(list1)-i)*(i-list2[index])
list2[index]=i
print(count)
9.平面切分
N=int(input())
input_lis=[]
crosspoint_x_set=set()
for i in range(N):
input_lis.append(list(map(int,input().split())))
input_lis=list(set([tuple(t) for t in input_lis])) #去掉重合的线段
each_line_num_lis = [1] * (len(input_lis) + 1)
for i in range(1,len(input_lis)):
crosspoint_x_set.clear()#crosspoint_x_set计算的第n条直线与前面直线的交点x,我们只需要的是set的长度,算出长度之后作为N加,然后clear,继续计算下一个条直线与前面直线的交点x
for n in range(i):
if input_lis[i-n-1][0]!=input_lis[i][0]:
x=round((input_lis[i][1]-input_lis[i-n-1][1])/(input_lis[i-n-1][0]-input_lis[i][0]),4)
crosspoint_x_set.add(x)
each_line_num_lis[i] += len(crosspoint_x_set)
print(sum(each_line_num_lis) )
10.字串排序
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
int res=0;
cin>>s;
int i=1,j;
for(;i<=s.length();i++){
int fr=i-0,ba=s.length()+1-i;
for(j=i-1;j>0;j--){//向前
if(s[i-1]==s[j-1]){
fr=i-j;
break;
}
}
for(j=i+1;j<=s.length();j++){//向后
if(s[i-1]==s[j-1]){
ba=j-i;
break;
}
}
int n=fr*ba;
res+=n;
//printf("%d %d %d\n",fr,ba,n);
}
printf("%d",res);
return 0;
}