编程介的小学生 2020-01-07 00:46 采纳率: 0.4%
浏览 190

Consecutive Digits 数字的问题

Problem Description
As a recruiting ploy, Google once posted billboards in Harvard Square and in the Silicon Valley area just stating “{first 10-digit prime found in consecutive digits of e}.com”. In other words, find that 10-digit sequence and then connect to the web site— and find out that Google is trying to hire people who can solve a particular kind of problem. Not to be outdone, Gaggle (a loosy-goosy fuzzy logic search firm), has devised its own recruiting problem. Consider the base 7 expansion of a rational number. For example, the first few digits of the base 7 expansion of 1/510 = 0.12541...7,33/410 = 11.15151...7, and 6/4910 = 0.06000...7, From this expansion, find the digits in a particular range of positions to the right of the "decimal" point.

Input
The input file begins with a line containing a single integer specifying the number of problem sets in the file. Each problem set is specified by four base 10 numbers on a single line, n d b e, where n and d are the numerator and
denominator of the rational number and 0 ≤ n ≤ 5,000 and 1 ≤ d ≤ 5,000. b and e are the beginning and ending positions for the desired range of digits, with 0 ≤ b,e ≤ 250 and 0 ≤ (e-b) ≤ 20. Note that 0 is the position immediately to the right of the decimal point.

Output
Each problem set will be numbered (beginning at one) and will generate a single line: Problem k: n / d, base 7 digits b through e: result where k is replaced by the problem set number, result is your computed result, and the other values are the corresponding input values.

Sample Input
4
1 5 0 0
6 49 1 3
33 4 2 7
511 977 122 126

Sample Output
Problem set 1: 1 / 5, base 7 digits 0 through 0: 1
Problem set 2: 6 / 49, base 7 digits 1 through 3: 600
Problem set 3: 33 / 4, base 7 digits 2 through 7: 151515
Problem set 4: 511 / 977, base 7 digits 122 through 126: 12425

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-26 19:47
    关注

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

    # Import necessary packages
    library(readline)
    
    # Read input from user
    input <- readline(prompt="Enter problem set number: ")
    
    # Function to convert a string representation of a fraction to a numeric value
    fraction_to_numeric <- function(fraction_str){
      numerator <- as.numeric(strsplit(fraction_str, "/")[[1]][[1]])
      denominator <- as.numeric(strsplit(fraction_str, "/")[[1]][[2]])
      return(numerator/denominator)
    }
    
    # Initialize variables for storing the results
    results <- list()
    
    # Iterate over each problem set
    for (i in 1:length(input)){
      # Prompt user for input values
      n <- as.integer(readline("Enter numerator n: "))
      d <- as.integer(readline("Enter denominator d: "))
      
      # Prompt user for range of digits to search for
      b <- as.integer(readline("Enter starting digit b: "))
      e <- as.integer(readline("Enter ending digit e: "))
      
      # Convert input values to numeric strings
      num_str <- paste0(n, "/", d)
      denom_str <- paste0(d, "/", 1)
      
      # Calculate the numerator and denominator of the fractional part of the input
      frac_num <- fraction_to_numeric(num_str)
      frac_denom <- fraction_to_numeric(denom_str)
      
      # Check if the input is valid
      if (frac_num == 0 || frac_denom == 0){
        cat("Invalid input.\n")
        break
      }
      
      # Initialize variables for storing intermediate results
      frac_num_str <- paste0(frac_num, "/", frac_denom)
      frac_denom_str <- paste0(1/frac_denom, "/", 1)
      
      # Find the length of the fractional part
      frac_len <- length(paste0(frac_num_str, ".", 0, sep=""))
      
      # Extract the digits in the desired range from the fractional part
      frac_digits <- strsplit(frac_num_str, ".")[[1]][[(b-1):((e-b))]]
      
      # Convert the extracted digits back to their original bases
      frac_num_base_7 <- sprintf("%0*d", 1, paste(rev(as.character(frac_digits)), collapse=""))
      frac_denom_base_7 <- sprintf("%0*d", 1, paste(rev(as.character(strsplit(frac_denom_str, "/")[1])), collapse=""))
      
      # Print the solution
      results[[paste0(i, "_solution")] <- sprintf("%.7s", c(frac_num_base_7, frac_denom_base_7))
    }
      
    # Print all the solutions
    print(results)
    

    This code reads input from the user, prompts them for the numerator, denominator, start and end positions of the digits they want to extract, converts these inputs to numeric strings, calculates the length of the fractional part, extracts the digits in the desired range from the fractional part, converts them back to their original bases, and prints the solution. It uses sprintf to format the output string to display up to seven digits after the decimal point.

    评论

报告相同问题?