20230411- Saima.com brush questions

1. Read a matrix with no number of rows and columns given

Time limit: 3000MS
Memory limit: 589824KB
Title description:
[Input question] During the test, sometimes the number of rows n of the data is not given. For the matrix, the number of rows n and the number of columns m may not be given. This question is for training input on such topics. (You can read before doing the question: https://labfiles.acmcoder.com/ojhtml/index.html#/?id=%e6%b2%a1%e6%9c%89%e7%bb%99%e5%87 %ba%e7%9f%a9%e9%98%b5%e7%9a%84%e8%a1%8c%e5%88%97%e6%95%b0)

For a given two-dimensional matrix, please output after transposing.

#!/usr/bin/env python  
# coding=utf-8
arr = []
while 1:
  s = input()

  if s != "":
    arr.append(list(map(int, s.split())))
  else:
    break
    
n=len(arr)
m=len(arr[0])
for j in range(m):
    for i in range(n):
        print(arr[i][j],end=' ')
    print('\n')
2. Typing

Time limit: 3000MS
Memory limit: 589824KB
Title description:
Xiao Ming likes typing very much. Today Xiao Hong gave Xiao Ming a string.
This string contains only uppercase and lowercase letters.
We know that by pressing the CapsLock key, you can switch between uppercase and lowercase modes.
When we are in lowercase mode, press shift + letter key at the same time to write uppercase letters.
When in uppercase mode, press shift+letter key to write lowercase letters.
Now the question is, give you a string, ask you how many keys you use at least, can you write this string?
Note that pressing shift and a letter key counts as two key presses. All are lowercase at the beginning.

样例输入
	3
	A
	AA
	AAAAAA
样例输出
	2
	3
	7

It's very strange, I obviously feel that my thinking is correct, but why is the correct rate only 9%? what went wrong

def fun_3(s):
    flag=0 #表示此时属于小写状态
    cnt=0
    for i in range(len(s)):
        if s[i]>='A' and s[i]<='Z':
            if flag==0:
                if i+1<len(s):
                    if s[i+1]>='A' and s[i+1]<='Z':
                        cnt+=2
                        flag=1
                    else:
                        cnt+=2
                else:
                    cnt+=2
            else:
                cnt+=1
        elif s[i]>='a' and s[i]<='z':
            if flag==0:
                cnt+=1
            else:
                if i+1<len(s):
                    if s[i+1]>='a' and s[i+1]<='z':
                        cnt+=2
                        flag=0
                    else:
                        cnt+=2
                else:
                    cnt+=2
        else:
            cnt+=1

    return cnt

n=int(input())
for i in range(n):
    s=input()
    print(fun_3(s))

How to write state transition

def fun(s):
    dp=[[0 for i in range(len(s))] for i in range(2)]
    for i in range(len(s)):
        if s[i]>='a' and s[i]<='z':
            if i==0:
                dp[0][i]=1
                dp[1][i]=2
            else:
                dp[0][i]=min(dp[0][i-1]+1,dp[1][i-1]+2)
                dp[1][i]=min(dp[0][i-1]+2,dp[1][i-1]+2)
        else:
            if i==0:
                dp[0][i]=2
                dp[1][i]=2
            else:
                dp[0][i]=min(dp[0][i-1]+2,dp[1][i-1]+2)
                dp[1][i]=min(dp[0][i-1]+2,dp[1][i-1]+1)
                
    for i in dp:print(i)
    return min(dp[0][-1],dp[1][-1])
3.Manager

Time limit: 3000MS
Memory limit: 589824KB
Title description:
Xiao Ming is the boss of an Internet company and needs to recruit employees. Now there are students from k schools applying for the job.

Due to special reasons, the number of employees who are required to join the school at the end should be different.

For example, we can admit 5 students to University A and 4 students to University B. But it is not allowed to admit 5 people to both University A and University B.

May I ask how many people were admitted at the end?
The correct rate is 67%, I don't know why

n=int(input())
li=list(map(int,input().split()))
li.sort(reverse=True)
ans=li[0]
tmp=li[0]
for i in range(1,n):
    ans+=min(tmp-1,li[i])
    tmp=min(tmp-1,li[i])
    
print(ans)

Guess you like

Origin blog.csdn.net/F13122298/article/details/130091867