gpt4 book ai didi

java - 电话调用编程逻辑

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

部分手机使用率可能描述如下:

The first minute of a call costs min1 cents.

Each minute from the 2nd up to 10th (inclusive) costs min2_10 cents each minute.

After the 10th minute, the call costs min11 cents for every additional minute.

You have s cents on your account before the call. What is the duration of the longest call (in minutes rounded down to the nearest integer) you can have?

输入数据:

对于min1 = 3 , min2_10 = 1 , min11 = 2 ,和s = 20 ,输出应该是 phoneCall(min1, min2_10, min11, s) = 14 .

原因如下:

第一分钟花费 3 美分,这样您就剩下 20 - 3 = 17 美分。第 2 到 10 分钟的总成本为 1 * 9 = 9,因此您可以再讲 9 分钟,但仍然有 17 - 9 = 8 美分。接下来每分钟花费 2 美分,这意味着您可以再讲 8/2 = 4 分钟。因此,您可以调用的最长通话时间为 1 + 9 + 4 = 14 分钟。

我不确定我的代码逻辑有什么问题。

int phoneCall(int min1, int min2_10, int min11, int s) {
int sum = 0;

if (s >= min1) {
sum++;
s = s - min1;
for (int i = 1; i <= 9; i++) {
if (s >= min2_10) {
sum = sum++;
s = s - min2_10;
} else
break;
}
sum = sum + s / min11;

}
return sum;
}

最佳答案

在 for 循环内的 if 语句中,您可以执行以下两件事之一,以使返回值为 14。

更改 sum=sum++;sum += 1; 或删除 sum= 所以它只是 sum++;

这应该返回 14 作为总和。

关于java - 电话调用编程逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51546841/

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