编程介的小学生 2019-09-22 13:48 采纳率: 0.4%
浏览 128

Rout 66 程序的编写

Problem Description
While the Romans may have had a fancy number system, the Visigoths (led by Alareiks, known now as Alaric I) managed to take Rome on August
24, 410. This was the first time that Rome had been taken by foreign troops in eight hundred years.

You will simulate a considerably less bloody (and less realistic) rout of soldiers by a potentially overwhelming force.

Consider a group of invaders of strength I, and an opposing force of strength J, with distance D between them and a defensive stronghold of strength
S. The Routing Force F of the invaders can be calculated as their strength times the distance:

F = ID

and the Blocking Force B of the defenders can be calculated as their strength times the square of their defenses:

B = JS2

If F is less than or equal to B, the invaders' rout fails, and they are driven off. If F is greater than B, the rout is successful, all defenders of the
stronghold are driven off, and the invaders can continue. However, their strength is reduced by removing a fraction equal to the ratio of the
defenders' Blocking Force to their Routing Force for that attack. The resulting number should be rounded up to the nearest integer, to ensure that a
successful rout leaves at least one soldier:

Inew = ?I(1 - B/F)?

If the invaders make it through a stronghold, its location is considered the starting point for calculations regarding the next group of defenders, and so
on, until either the invading force makes it all the way through the defenders or is completely routed.

Your goal is to determine whether or not a given invading force can make it through a particular gauntlet of defenders.

For the purposes of simplification, all defending strongholds in this simulation are considered to be in a straight line from the starting position of the
invaders, and must be encountered in order from nearest to furthest away. No strongholds will be in the same location in a given set of data.

Input
Input to this problem will begin with a line containing a single integer N (1 ≤ N ≤ 100) indicating the number of data sets. Each data set consists of
the following components:

A line containing a single integer E (1 ≤ E ≤ 20) indicating the number of defensive strongholds in the data set;
A series of E lines, each with three integers D, J, S (1 ≤ D, J ≤ 10000; 1 ≤ S ≤ 50) separated by spaces representing the strongholds. D is the
distance of the stronghold from the invaders' starting position; remember that all strongholds are considered to be in a straight line from
the invaders' starting position, and must be encountered in order from nearest to furthest away. J and S represent the strength of the
defenders and the stronghold, respectively, as in the above equation; and
A line containing a single integer I (1 ≤ I ≤ 30000) representing the strength of the invading force.

Output
For each data set, print "ROUT!" if the invaders make it through all of the strongholds, or "RETREAT!" if the invaders cannot make it through all of the
strongholds.

Sample Input
2
1
10 10 5
100
2
75 100 5
10 10 5
50

Sample Output
ROUT!
RETREAT!

展开全部

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-30 00:49
    关注

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

    # Import necessary packages
    library(readline)
    
    # Read input from user
    input <- readline(prompt = "Enter a number of data sets: ")
    
    # Initialize empty list to store results
    results <- numeric(length(input))
    
    # Loop over each data set
    for (i in 1:min(input, 100)) {
      # Get number of defensive strongholds
      num_defenses <- readline(prompt = paste("Enter number of defensive strongholds:", i, ": ", sep = ""))
      
      # Convert input string to integer
      num_defenses <- as.integer(num_defenses)
      
      # Initialize variables
      distances <- matrix(NA, nrow = 10000, ncol = 3)
      strengths <- matrix(NA, nrow = 10000, ncol = 3)
      invader_strengths <- matrix(NA, nrow = 10000, ncol = 1)
      
      # Populate distances, strengths, and invader strengths matrices
      for (j in 1:num_defenses) {
        # Get values of distances, strengths, and invader strengths
        d <- readline(prompt = paste("Enter distance", j, "from invaders:", i, ": "))
        j_stronghold <- readline(prompt = paste("Enter strength of stronghold", j, "in defense:", i, ": "))
        s_stronghold <- readline(prompt = paste("Enter strength of stronghold", j, "in defense:", i, ": "))
        
        # Convert inputs to numeric
        d <- as.numeric(d)
        j_stronghold <- as.numeric(j_stronghold)
        s_stronghold <- as.numeric(s_stronghold)
        
        # Add row to respective matrices
        distances[i, j] <- d
        strengths[i, j] <- cbind(c(j_stronghold, s_stronghold))
        invader_strengths[i, ] <- invader_strengths[i, ] + abs(distances[i, j])
      }
      
      # Determine whether invaders can make it through all defenses
      invader_strengths <- invader_strengths[invader_strengths > 0]
      distances <- dist(distances)
      blocking_force <- invader_strengths * distances^2
      
      # Calculate new invader strength after routing
      new_invader_strength <- invader_strengths[which(blocking_force <= invader_strengths)]
      invader_strengths[new_invader_strength, ] <- 0
      
      # Determine whether invaders can make it through all defenses
      if (sum(invader_strengths) == 0) {
        results[i] <- "ROUT!"
      } else {
        results[i] <- "RETREAT!"
      }
    }
    
    # Print results
    print(results)
    

    This code reads in the number of data sets from the user, then loops over each data set. It calculates the distances, strengths, and invader strengths for each stronghold, determines whether the invaders can make it through all defenses, and prints "ROUT!" if the invaders can make it through all defenses or "RETREAT!" otherwise.

    评论
编辑
预览

报告相同问题?