编程介的小学生 2020-09-28 08:42 采纳率: 0.4%
浏览 30

Hex Factorial 是如何一个实现的

Problem Description
The expression N!, reads as the factorial of N, denoting the product of the first N positive integers. If the factorial of N is written in hexadecimal without leading zeros, can you tell us how many zeros are there in it? Take 15! as an example, you should answer "3" because (15)10! = (13077775800)16, and there are 3 zeros in it.

Input
The input contains several cases. Each case has one line containing a non-negative decimal integer N (N ≤ 100). You need to count the zeros in N! in hexadecimal. A negative number terminates the input.

Output
For each non-negative integer N, output one line containing exactly one integer, indicating the number of
zeros in N!.

Sample Input
1
15
-1

Sample Output
0
3

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-25 09:24
    关注

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

    # 读取输入数据
    input <- readLines()
    
    # 定义变量
    zero_count <- 0
    
    for (line in input) {
      # 将输入字符串转换为整数
      num <- as.integer(strsplit(line, "")[[1]])
      
      # 计算n!
      fact <- factorial(num)
      
      # 计算fact在十六进制中的长度,并添加到零计数中
      zero_count <- zero_count + length(as.character(fact)) %% 16
      
      # 输出结果
      print(zero_count)
    }
    

    请注意,此代码可能不会正确地处理所有边缘情况。例如,如果输入包含负数或非数字字符,它将抛出错误。此外,由于Python和R之间的差异,某些函数(如factorial())可能不适用。在这种情况下,您可能需要使用特定于R的语言库来解决这个问题。

    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了