Programming Exercise @8-29

Topic one:

Title Description
Recently, Xiaoqiang likes to play a game of warriors and monsters. Both warriors and monsters have an attribute: ability value. When a brave man encounters a monster, the fighting method is as follows:
if the hero's ability value is not lower than the monster's ability value, the brave man wins.
Otherwise, the brave loses.
The brave man can fight monsters in any order. When the brave man successfully defeats a monster, he can get a gold coin. At the beginning, the total number of coins of the brave man is 0, and the number of gold coins in the hand of the brave man cannot be negative at any time.
A brave man can spend a gold coin at any time to increase his ability by 1 point. Now Xiaoqiang wants to know how many gold coins the brave can have at most, and the monsters don't have to finish fighting.

Input description The first line is a positive integer aa
separated by two spacesa n n n , represent the initial ability value of the hero and the number of monsters respectively. The next line is a positive integer A i {A_i}
separated by n spacesAi, which is used to describe the ability value of the i-th monster.
Output Description
Only one line with an integer representing the answer.

Example 1
input
1 3
2 2 1
output
2

Description:
Defeat the monster with the lowest ability value to get 1 gold coin, then increase the ability value by 1, then defeat the remaining two monsters, and get 2 gold coins in hand.

'''
贪心算法
'''
def max_coins(ability, monster_abilities):
    monster_abilities.sort()  # 对怪物的能力值进行排序
    coins = 0
    max_coins = 0

    for monster_ability in monster_abilities:
        if ability >= monster_ability: # 战胜怪兽
            coins += 1
            max_coins = max(max_coins, coins)
        else: # 使用钞能力也无法战胜怪兽
            if monster_ability > coins + ability:
                break
            else: # 临阵花钱提升自己
                coins -= monster_ability - ability
                coins += 1
                ability = monster_ability

    return max_coins

if __name__ == '__main__':
    ability , num_monsters = map(int, input().split())
    monster_abilities = list(map(int, input().split()))

    print(max_coins(ability, monster_abilities))
2

Topic 2:

Title Description
Xiaohong plans to use mmm day brushnnn questions, theiii day plan to brushai a_iaiquestion. Xiaohong will strictly follow the plan to solve the questions until the iii day, Xiaohong will evaluate the remaining average daily need to brushtttopic . youngai a_iaigreater than ttt , then record>>> ; if equal tottt then record=== ; if less thanttt , then record<<< .
Please output Xiaohong's evaluation record.

Input description
The first line inputs two positive integers n and m.
Next, enter m non-negative integers ai a_i in the second lineai.
Guaranteed all ai a_iaiThe sum of is equal to n.
Output description
The output is a one-line string, representing Xiaohong's evaluation record.

Example 1
input
15 6
4 5 0 3 1 2
output
'>><><='

Example 2
input
12 4
3 2 5 2
output
'=<>='

'''
模拟
'''
def solve(n, m, arr):
    resi = n
    ans = ''
    for idx, num in enumerate(arr):
        if num > resi / (m - idx):
            ans += '>'
        elif num < resi / (m - idx):
            ans += '<'
        else:
            ans += '='
        resi -= num
    return ans

if __name__ == '__main__':
    n, m = map(int, input().split())
    arr = list(map(int, input().split()))
    print(solve(n, m, arr))
>><><=

The two questions are only tested on use cases. If there are any mistakes, welcome to communicate!

Supongo que te gusta

Origin blog.csdn.net/cjw838982809/article/details/132569222
Recomendado
Clasificación