编程介的小学生 2020-09-28 08:42 采纳率: 0.9%
浏览 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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵