编程介的小学生 2020-09-28 09:03 采纳率: 0.9%
浏览 17

Become A Hero 具体用什么方式实现呢

Problem Description
Lemon wants to be a hero since he was a child. Recently he is reading a book called “Where Is Hero From” written by ZTY. After reading the book, Lemon sends a letter to ZTY. Soon he recieves a reply.

Dear Lemon,
It is my way of success. Please caculate the algorithm, and secret is behind the answer. The algorithm follows:
Int Answer(Int n)
{
.......Count = 0;
.......For (I = 1; I <= n; I++)
.......{
..............If (LCM(I, n) < n * I)
....................Count++;
.......}
.......Return Count;
}
The LCM(m, n) is the lowest common multiple of m and n.
It is easy for you, isn’t it.
Please hurry up!
ZTY

What a good chance to be a hero. Lemon can not wait any longer. Please help Lemon get the answer as soon as possible.

Input
First line contains an integer T(1 <= T <= 1000000) indicates the number of test case. Then T line follows, each line contains an integer n (1 <= n <= 2000000).

Output
For each data print one line, the Answer(n).

Sample Input
1
1

Sample Output
0

  • 写回答

1条回答 默认 最新

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

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

    # Calculate the Least Common Multiple (LCM)
    lcm <- function(a, b){
        return(abs(a*b)/gcd(a,b))
    }
    
    # Calculate the Greatest Common Divisor (GCD)
    gcd <- function(a, b){
        while(b != 0){
            temp <- b
            b <- a % b
            a <- temp
        }
        return(a)
    }
    
    # Main function
    answer <- function(n){
        count <- 0
        for(i in 1:n){
            if(lcm(i,n)<n*i){count += 1}
        }
        return(count)
    }
    
    # Test cases
    t <- 5 # Number of test cases
    for(i in 1:t){
        n <- readline() # Read input from stdin
        print(answer(n)) # Print output to stdout
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥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键失灵