gpt4 book ai didi

math - 如何使用Dart计算此数学?

转载 作者:行者123 更新时间:2023-12-03 04:56:26 25 4
gpt4 key购买 nike

totalDirection = 1540m

1st KM = $10

2nd KM = $8

Others(normal) KM = $5



如何计算此逻辑?用户应为第一公里支付10美元,然后为2ns公里支付8美元,其他用户则支付5美元。 model.startUpCharge是数组,存储10 $和$ 8

/// [totalDistance] is in meters
double calculateTripPrice(RideModel model, double totalDistance) {
double totalPrice = 0;
double standardPrice = 0;

totalPrice = model.normalCharge *
((totalDistance / 1000) - model.startUpCharge.length);
for (var i = 0; i < model.startUpCharge.length; i++) {
standardPrice = standardPrice + model.startUpCharge[i];
}
totalPrice = totalPrice + standardPrice;
return totalPrice;
}

1540m逻辑= 1000m支付$ 10和540m支付$ 8

@creativecreatoror可能无法回答

答案应该是50,但结果是5。

问题是:如果我增加1米,价格应该是50。第1公里有初始费用。没变
import 'dart:math' as math;

class RideModel{
final List startUpCharge;
final double normalCharge;

RideModel({this.startUpCharge,this.normalCharge});
}
main() {
calculateTripPrice(RideModel(startUpCharge: [50,51],normalCharge: 10),1);
}

double calculateTripPrice(RideModel model, double totalDistance) {
var remainingDistance = totalDistance, price = .0;

// Kilometers with special charge
for (var i = 0; i < model.startUpCharge.length; i++) {
price += model.startUpCharge[i] / 1000 * math.min(1000, remainingDistance);
remainingDistance -= math.min(1000, remainingDistance);
}

// Remaining kilometers
price += model.normalCharge / 1000 * remainingDistance;
remainingDistance = 0;
print(price);

return price;
}

最佳答案

您可以简单地反复添加价格并从距离中减去。基于此,只有在仍存在剩余距离且每次使用正确的系数时,才会添加价格。

double calculateTripPrice(RideModel model, double totalDistance) {
var remainingDistance = totalDistance, price = .0;

// Kilometers with special charge
for (var i = 0; i < model.startUpCharge.length; i++) {
price += model.startUpCharge[i] / 1000 * math.min(1000, remainingDistance);
remainingDistance -= math.min(1000, remainingDistance);
}

// Remaining kilometers
price += 5 / 1000 * remainingDistance;
remainingDistance = 0;

return price;
}

记住要 import 'dart:math' as math;

关于math - 如何使用Dart计算此数学?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60551997/

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