gpt4 book ai didi

java - 计算并返回 FizzBu​​zzProblem 中 3*f+5*b-15*fb 的值

转载 作者:行者123 更新时间:2023-12-01 18:10:31 24 4
gpt4 key购买 nike

Given: A number is considered fizz if it is divisible by 3. It is considered buzz if it is divisible by 5. It is considered fizzbuzz if it is divisible by both 3 and 5. A fizzbuzz is neither fizz nor buzz. Given two numbers n1 and n2 such that n2>n1, let f be the number of fizz, b be the number of buzz and fb be the number of fizzbuzz between n1 and n2(both n1 and n2 are included).

Calculate and return the value of 3*f+5*b-15*fb?

我的方法:

我使用了问题中给出的所有条件,但我很困惑如何使用这个条件“嘶嘶声既不是嘶嘶声也不是嗡嗡声”

我得到错误的输出可能是因为这个,任何人都可以指导我如何解决这个问题。

public int calcFB (int n1, int n2) 
{
int f=0;
int b=0;
int fb=0;
for(int i=n1;i<=n2;i++)
{
if((i%3==0)&&(i%5==0))
{
fb++;
}
else if(i%3==0)
{
f++;
}
else if(i%5==0)
{
b++;
}


}
return 3*f+5*b+15*fb;

}

对于输入:

 Parameters      Actual Output  Expected Output
'1' '21' 56 18

最佳答案

在您的返回声明中,您添加了 15*fb,在问题描述中,您说公式为 3*f+5*b-15*fb。所以你需要用这个 return 语句减去 15*fb:

return 3*f+5*b-15*fb

关于java - 计算并返回 FizzBu​​zzProblem 中 3*f+5*b-15*fb 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33404868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com