程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Record of group B of the 9th provincial Blue Bridge Cup (Python)

編輯:Python

Blue Bridge Cup

  • Fill in the blank
    • The next few days
    • Clear code
    • Product tail zero
    • Number of tests
      • Ideas
  • Programming
    • Increment triples
    • Spiral broken line
      • Ideas
      • Code implementation
    • Log statistics
    • global warming
    • Product maximum

Fill in the blank

The next few days

Problem description

2000 Year of 1 month 1 Japan , It was the first of that year 1 God .
that ,2000 Year of 5 month 4 Japan , It was the day of the year ?

Answer submission
Be careful : You need to submit an integer , Don't fill in any superfluous information .

Oral arithmetic or excel Dharma is OK , answer :125

Clear code

Problem description
The form of Chinese characters exists in the word bank , Even today ,16 Dot matrix fonts are still widely used .

16 The dot matrix font looks at each character as 16 x 16 Pixel information , And record this information in bytes .

A byte can store 8 Bit information , use 32 Only one byte can store the font of a Chinese character .

Turn each byte into 2 Hexadecimal said ,1 It means ink ,0 Represents the background color .

Each row 2 individual byte , altogether 16 That's ok , The layout is :

The first 1 byte , The first 2 byte
The first 3 byte , The first 4 byte

The first 31 byte , The first 32 byte

This topic is to give you a piece of information composed of multiple Chinese characters , Every Chinese character uses 32 Byte representation , Here we give the value of bytes as signed integers .

The requirements of the title are hidden in this information . Your task is to restore the glyphs of these Chinese characters , From this we can see the requirements of the topic , And fill in the answers as required .

The message is ( altogether 10 The Chinese characters ):

4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0
16 64 16 64 34 68 127 126 66 -124 67 4 66 4 66 -124 126 100 66 36 66 4 66 4 66 4 126 4 66 40 0 16
4 0 4 0 4 0 4 32 -1 -16 4 32 4 32 4 32 4 32 4 32 8 32 8 32 16 34 16 34 32 30 -64 0
0 -128 64 -128 48 -128 17 8 1 -4 2 8 8 80 16 64 32 64 -32 64 32 -96 32 -96 33 16 34 8 36 14 40 4
4 0 3 0 1 0 0 4 -1 -2 4 0 4 16 7 -8 4 16 4 16 4 16 8 16 8 16 16 16 32 -96 64 64
16 64 20 72 62 -4 73 32 5 16 1 0 63 -8 1 0 -1 -2 0 64 0 80 63 -8 8 64 4 64 1 64 0 -128
0 16 63 -8 1 0 1 0 1 0 1 4 -1 -2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 5 0 2 0
2 0 2 0 7 -16 8 32 24 64 37 -128 2 -128 12 -128 113 -4 2 8 12 16 18 32 33 -64 1 0 14 0 112 0
1 0 1 0 1 0 9 32 9 16 17 12 17 4 33 16 65 16 1 32 1 64 0 -128 1 0 2 0 12 0 112 0
0 0 0 0 7 -16 24 24 48 12 56 12 0 56 0 -32 0 -64 0 -128 0 0 0 0 1 -128 3 -64 1 -128 0 0

Answer submission
Be careful : You need to submit an integer , Don't fill in any superfluous information .

The slight problem here is Negative to binary , It is not possible to transfer directly , What should be wanted here is Complement form , Implementation method :

bin(x&0xffffffff) #x Is the number to be transferred 

Every line has 32 Number ,2 Number into a group , Convert them into binary and put them together , obtain 16*16 To represent a Chinese character

lt = [4, 0, 4, 0, 4, 0, 4, 32, -1, -16, 4, 32, 4, 32, 4, 32, 4, 32, 4, 32, 8, 32, 8, 32, 16, 34, 16, 34, 32, 30, -64, 0]
for i in range(32):
s = bin(lt[i]&0xffffffff)[-8:]
s = s.replace('0b',"")
x = s.zfill(8).replace('0',' ').replace('1','*')
if i%2 == 0:
print(x,end="")
else:
print(x)
''' * * * * * ************ * * * * * * * * * * * * * * * * * * * * * **** ** '''

You can get the result by inputting all the numbers : How much is the ninth power of nine ?

So the answer :387420489

for j in range(10):
lt = []
for item in map(int,input().split()):
lt.append(item)
for i in range(32):
if lt[i] < 0:
s = bin(lt[i]&0xffffffff)[-8:]
else:
s = bin(lt[i])
s = s.replace('0b',"")
x = s.zfill(8).replace('0',' ').replace('1','*')
if i%2 == 0:
print(x,end="")
else:
print(x)
print()

Product tail zero

Problem description

As follows 10 Row data , Each row has 10 It's an integer , Please find out how many zeros there are at the end of their product ?

5650 4542 3554 473 946 4114 3871 9073 90 4329
2758 7949 6113 5659 5245 7432 3051 4434 6704 3594
9937 1173 6866 3397 4759 7557 3070 2287 1453 9899
1486 5722 3135 1170 4014 5510 5120 729 2880 9019
2049 698 4582 4346 4427 646 9742 7340 1230 7683
5693 7015 6887 7381 4172 4341 2909 2027 7355 5649
6701 6645 1671 5978 2704 9926 295 3125 3878 6785
2066 4247 4800 1578 6652 4616 1113 6205 3264 2915
3966 5291 2904 1285 2193 1428 2265 8730 9436 7074
689 5510 8243 6114 337 4096 8199 7313 3685 211

Answer submission
Be careful : You need to submit an integer , The number of zeros at the end . Don't fill in any superfluous information .

Multiply and count , answer :31

Number of tests

Problem description

x The inhabitants of the planet have a bad temper , But fortunately, when they are angry, the only abnormal behavior is : Drop your cell phone .

Major manufacturers have launched a variety of fall resistant mobile phones .x Star's Quality Supervision Bureau has stipulated that mobile phones must be tested for falling resistance , And evaluate a fall resistance index , After that, it is allowed to be listed and circulated .

x There are many towering towers on the planet , It can be used for the fall resistance test .
The height of each floor of the tower is the same , A little different from the earth is , Their first floor is not the ground , It's equivalent to our 2 floor .

If the mobile phone from 7 The layer is not broken , But the first 8 The floor is broken , Then the falling resistance index of mobile phone = 7.

Specially , If the mobile phone from 1 If the layer is dropped, it will be broken , Then the fall resistance index = 0.

If you get to the top of the tower n Layer throwing is not broken , Then the fall resistance index = n

To reduce the number of tests , Sample from each manufacturer 3 Cell phones are tested .

The tower height of a test is 1000 layer , If we always use the best strategy , How many times do you need to test it in the worst of luck to determine the falling resistance index of the mobile phone ?

Please fill in the maximum number of tests .

Answer submission
Be careful : What needs to be filled in is an integer , Don't fill in any superfluous information .

Ideas

First of all, make sure that The best strategy and The worst luck

 If there is only one mobile phone , You can only try from the first floor , The worst case is to go to the highest level to get results .
When there are two mobile phones , The height of the tower is 3 when , There are three options :
1、 From the first floor , So the worst case scenario is testing 3 Time
2、 From the second floor , It may be broken or partially broken , The worst case scenario is testing 2 Time
3、 From the third floor , The worst case scenario is testing 3 Time (3-1-2)
So the best strategy is to start from the second layer , At least test 2 Time

You can use it here DP Methods ,f[i][j] On behalf of i Mobile phone 、 The height of the tower is j When the layer , The minimum number of tests under the worst luck .
From the example of a mobile phone , In the worst case, the number of tests is equal to the total height of the floor , So to initialize :f[i][j] = j

 from x Layer start test , There will be two situations
1、 It's broken
Then the number of mobile phones -1, Go to the lower floor to test , Retest at most x-1 Time
That is, the total number of tests is f[i-1][x-1]+1
2、 Not broken
Then go to a higher floor to test , Retest at most j-x Time
That is, the total number of tests is f[i][j-x]+1
So the current plan ( from x Layer start ) The number of tests required under the worst luck is max(f[i-1][x-1],f[i][j-x]) + 1
The best strategy is to take the minimum value from all schemes

answer :19

high = 1005
f=[[0] * 1005 for i in range(3)]
for i in range(3): # initialization 
for j in range(1005):
f[i][j] = j
for i in range(1,3):
for j in range(1,high):
for k in range(1,j):
f[i][j] = min(f[i][j], max(f[i-1][k-1], f[i][j-k])+1)
# The subscript of one-dimensional array is 0、1、2, Corresponding 1、2、3 Mobile phone 
print(f[2][1000])

Programming

Increment triples

 Given three arrays of integers
A = [A1, A2, ... AN],
B = [B1, B2, ... BN],
C = [C1, C2, ... CN],
Please count how many triples there are (i, j, k) Satisfy :
1. 1 <= i, j, k <= N
2. Ai < Bj < Ck

Input format

 The first line contains an integer N.
The second line contains N It's an integer A1, A2, ... AN.
The third line contains N It's an integer B1, B2, ... BN.
The fourth line contains N It's an integer C1, C2, ... CN.
about 30% The data of ,1 <= N <= 100
about 60% The data of ,1 <= N <= 1000
about 100% The data of ,1 <= N <= 100000 0 <= Ai, Bi, Ci <= 100000

Output format
An integer represents the answer

The sample input

3
1 1 1
2 2 2
3 3 3

Sample output

27

Direct violence will lead to overtime , You can only take 37 branch , Need to change a more concise way of thinking .

Need to meet Ai < Bj < Ck, It can be divided into Ai < Bj and Bj < Ck Judge separately , After sorting all the elements in the three arrays , Traverse b Array , Come to satisfaction Ai < Bj There are x individual , Satisfy Bj < Ck Yes y individual , Multiply the two and add up .

n = int(input())
a = sorted(list(map(int,input().split())))
b = sorted(list(map(int,input().split())))
c = sorted(list(map(int,input().split())))
out = 0
x = 0
y = 0
for j in range(n):
while x < n and a[x] < b[j]:
x += 1
while y < n and b[j] >= c[y]:
y += 1
out += x*(n-y)
print(out)

Spiral broken line

The spiral polyline as shown in the figure passes through all integral points on the plane exactly once .

For the whole point (X, Y), We define the distance from the origin dis(X, Y) It's from the origin to (X, Y) The length of the helix of the line .
for example dis(0, 1)=3, dis(-2, -1)=9
Give the coordinates of the whole point (X, Y), You can figure out dis(X, Y) Do you ?

Input format

X and Y

about 40% The data of ,-1000 <= X, Y <= 1000
about 70% The data of ,-100000 <= X, Y <= 100000
about 100% The data of , -1000000000 <= X, Y <= 1000000000

Output format

Output dis(X, Y)

The sample input

0 1

Sample output

3

Ideas

When the input point is (-3,1) when , It can be assumed that it is The third square On , The square here is slightly modified from the original figure , Point for example A(0,0)、 spot B(-1,0), Make Line segment AB Around the point B Clockwise rotation 90°, You get the first square .

What is required at this time dis(-3,1) It can be broken down into : front 3-1 The circumference of a square and + spot (-3,1) point-to-point (-3,-3) Distance of

So we need to know (X,Y) On which side of the several squares .

Code implementation

X,Y = map(int,input().split())
n = max(abs(X),abs(Y)) # How many squares 
if X < 0 and X == Y: # A special case 
print(4*n*(n+1))
else:
out = 4*n*(n-1) # front n-1 The circumference of a square and 
#(X,Y) And (-n,-n) Distance of 
if X == -n:
d = Y + n
elif Y == n:
d = X + 3*n
elif X == n:
d = 5*n - Y
else:
d = 7*n - X
print(out + d)

Log statistics

Xiaoming maintains a programmer Forum . Now he has a collection of " give the thumbs-up " journal , The logs share N That's ok . The format of each line is :

ts id

It means that ts Time number id 's post received a " Fabulous ".

Now Xiaoming wants to count which posts have been " Hot post ". If a post has been in any length of D Receive no less than within the time period of K A great , Xiao Ming thinks this post was " Hot post ".

say concretely , If there is a moment T Satisfy This post is on [T, T+D) In this period of time ( Notice that it's left closed right open ) Received no less than K A great , The post was once " Hot post ".

Given the log , Please help Xiao Ming count out all the things that used to be " Hot post " The post number of .

Input format

The first line contains three integers N、D and K.
following N Each line has a log , Contains two integers ts and id.
about 50% The data of ,1 <= K <= N <= 1000
about 100% The data of ,1 <= K <= N <= 100000 0 <=ts <= 100000 0 <= id <= 100000

Output format

Output hot posts from small to large id. Every id a line .

The sample input

7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3

Sample output

1
3

It's not hard to , Just grasp the minimum interval

n,d,k = map(int,input().split())
dic = {
} # Deposit id And corresponding ts
out = []
for i in range(n):
ts,id = map(int,input().split())
if id in dic:
lt = dic[id]
else:
lt = []
lt.append(ts)
dic[id] = lt
for key in dic:
value = sorted(dic[key])
if len(value) < k:
pass
else:
for i in range(len(value)-k+1):
if value[i+k-1]-value[i] < d:
out.append(key)
break
for i in sorted(out):
print(i)

global warming

You have a picture of a certain sea area NxN Pixel photos ,".“ Represents the ocean 、”#" Representing land , As shown below :

.......
.##....
.##....
....##.
..####.
...###.
.......

among " The up and down or so " A piece of land connected in four directions forms an island . For example, the picture above shows 2 Islands .

Because of global warming, the sea level is rising , Scientists predict the next few decades , One pixel of the edge of the island will be inundated with seawater . Specifically, if a land pixel is adjacent to the ocean ( There's an ocean in four adjacent pixels up, down, left, right ), It will be submerged .

For example, the sea area in the picture above will look like this in the future :

.......
.......
.......
.......
....#..
.......
.......

Please calculate : According to the scientist's prediction , How many islands in the picture will be completely submerged .

Input format

The first line contains an integer N. (1 <= N <= 1000)
following N That's ok N Column represents a sea photo .
Photo guarantee No 1 That's ok 、 The first 1 Column 、 The first N That's ok 、 The first N The pixels of the column are all oceans .

Output format

An integer represents the answer .

The sample input

7
.......
.##....
.##....
....##.
..####.
...###.
.......

Sample output

1

The initial idea is to simulate the future of the sea area , Later, it was found that it would be tedious to judge the number of islands , So change your mind , Traverse the islands in turn , Total number of Islands - The number of islands not submerged That's the answer. .

n = int(input())
lt = [] # Store pixel data 
out = 0 # The number of islands not submerged 
all = 0 # Total number of Islands 
for i in range(n):
l = list(input())
lt .append(l)
# The coordinate corresponding value is 1 Is traversed 
flag = [[0]*n for i in range(n)]
dirs = [
lambda x,y:(x+1,y), # towards the right 
lambda x,y:(x,y+1), # Down 
lambda x,y:(x-1,y), # towards the left 
lambda x,y:(x,y-1) # Up 
]
for i in range(n):
for j in range(n):
if lt[i][j] == '#' and flag[i][j] == 0:
ls = [] # Store coordinates corresponding to land pixels 
stack = []
stack.append((i,j))
flag[i][j] = 1
k = 1 # Mark Avoid double counting 
all += 1
while len(stack) != 0:
now = stack[-1] # Current point coordinates 
if k:
# Judge whether it will be flooded 
try:
m = 1 # Mark 
for dir in dirs:
x,y = dir(now[0],now[1])
if lt[x][y] == '.':
m = 0
if m:
out += 1
k = 0
except:
pass
# Traverse 
try:
for dir in dirs:
x,y = dir(now[0],now[1])# Coordinates of the next point 
if lt[x][y] == '#' and flag[x][y] == 0:
flag[x][y] = 1
stack.append((x,y))
break
else:
stack.pop()
except:
pass
print(all-out)

Product maximum

Given N It's an integer A1, A2, … AN. Please choose from them K Number , To maximize the product .

Please find the maximum product , Since the product may be out of the range of integers , You just output the product divided by 1000000009 The remainder of .

Be careful , If X<0, We define X Divide 1000000009 The remainder of is negative (-X) Divide 1000000009 The remainder of .
namely :0-((0-x) % 1000000009)

Input format

The first line contains two integers N and K.
following N Each row is an integer Ai.

about 40% The data of ,1 <= K <= N <= 100
about 60% The data of ,1 <= K <= 1000
about 100% The data of ,1<= K <= N <= 100000 -100000 <= Ai <= 100000

Output format

An integer , Answer .

The sample input 1

5 3
-100000
-10000
2
100000
10000

Sample output 1

999100009

The sample input 2

5 3
-100000
-100000
-2
-100000
-100000

Sample output 2

-999999829

There are a lot of things , The follow-up meeting will be able to AC Release your code


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved