编程介的小学生 2019-11-10 21:50 采纳率: 0.4%
浏览 129

Funny Games 程序的设计

Problem Description
Nils and Mikael are intergalaxial fighters. Now they are competing for the planet Tellus. The size of this small and unimportant planet is 1 < X < 10000 gobs. The problem is that their pockets only have room for one gob, so they have to reduce the size of the planet. They have available 1 <= K <= 6 FACTOR-weapons characterized by numbers F1, F2, . . . , Fk, all less than 0.9. As is commonly known, a FACTOR-weapon will blow off part of the planet, thus reducing the planet to a fraction of its size, given by the characteristic. Thus, with e.g. F1 = 0.5 an application of the first weapon will half the size of the planet. The fighter who reduces the size to less than, or equal to, 1 gob can take the planet home with him. They take turns attacking the planet with any weapon. If Nils starts, who will win the planet? Assume that both Nils and Mikael are omniscient and always make a winning move if there is one.
Technical note: To ease the problem of rounding errors, there will be no edge cases where an infinitesimal perturbation of the input values would cause a different answer.

Input
The first line of input is N <= 100, the number of test cases. Each of the next N lines consists of X, K and then the K numbers F1, F2, . . . , Fk, having no more than 6 decimals

Output
For each test case, produce one line of output with the name of the winner (either Nils or Mikael).

Sample Input
4
6 2 0.25 0.5
10 2 0.25 0.5
29.29 4 0.3 0.7 0.43 0.54
29.30 4 0.3 0.7 0.43 0.54

Sample Output
Mikael
Nils
Nils
Mikael

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-29 19:00
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # Import necessary packages
    library(readline)
    
    # Define function to calculate the size reduction of a planet using a FACTOR-weapon
    calculate_reduction <- function(size, k, f) {
      sum(f * round(size^(-f))) - floor(size)
    }
    
    # Read input from user
    n_cases <- readline() # Read number of test cases
    for (i in 1:n_cases) {
      x <- as.integer(readline()) # Read size of planet
      k <- as.integer(readline())
      f <- readLines(na.strings = " ") # Read FACTOR-weapon characteristics
      
      # Initialize variables to keep track of the current size and the winner's score
      current_size <- x
      winner_score <- 0
      loser_score <- 0
        
      # Loop through each FACTOR-weapon and apply it until the planet is reduced to 1 gob or less
      while (current_size > 1e-6) {
        for (j in 1:k) {
          factor_weapon <- as.numeric(parse(text = f[j]))
          new_size <- calculate_reduction(current_size, k, factor_weapon)
          
          # Determine which player won and update scores accordingly
          if (new_size == 0 || new_size == 1) {
            winner_score <- max(winner_score, j + 1) # Update winner's score
            break # Exit loop if the planet was reduced to 1 gob or less
          } else {
            loser_score <- max(loser_score, j + 1) # Update loser's score
          }
        }
        
        # Update current size based on the winner's score
        current_size <- loser_score
      }
      
      # Print the result for this test case
      print(paste("Mikael", i))
    }
    

    This code defines a calculate_reduction function that calculates the size reduction of a planet using a FACTOR-weapon, and reads the input from the user using readline. It then loops through each FACTOR-weapon and applies it to the initial size of the planet, updating the winner's and loser's scores accordingly. Finally, it prints the result for each test case.

    评论

报告相同问题?