当前位置: 首页 > news >正文

青训营 X 豆包MarsCode 技术训练营--充电总时间计算

题目

问题描述

小R有nn部电脑,每部电脑的电池容量分别为aiai​。她可以使用两种不同的充电方式来给电脑充电:

普通充电:每单位时间为电脑充电xx单位的电量。
闪充:每单位时间为电脑充电4x4x单位的电量。

现在,所有电脑的电量都为零。小R希望使用闪充给所有电脑充满电,计算她需要的总充电时间。请保留结果的小数点后两位。
测试样例

样例1:

输入:n = 4 ,x = 1 ,a = [2, 3, 4, 5]
输出:'3.50'

样例2:

输入:n = 3 ,x = 2 ,a = [4, 6, 8]
输出:'2.25'

样例3:

输入:n = 2 ,x = 1 ,a = [10, 5]
输出:'3.75'

代码

import java.text.DecimalFormat;

public class Main {
public static String solution(int n, int x, int[] a) {
// 闪充速率是普通充电速率的4倍
int fastChargeRate = 4 * x;

    // 初始化总充电时间double totalChargeTime = 0.0;// 计算每台电脑充满电所需的时间,并累加到总充电时间for (int capacity : a) {double chargeTime = (double) capacity / fastChargeRate;totalChargeTime += chargeTime;}// 格式化结果保留两位小数DecimalFormat df = new DecimalFormat("0.00");return df.format(totalChargeTime);
}public static void main(String[] args) {// 测试用例System.out.println(solution(4, 1, new int[]{2, 3, 4, 5}).equals("3.50"));  // 应该输出 trueSystem.out.println(solution(3, 2, new int[]{4, 6, 8}).equals("2.25"));    // 应该输出 trueSystem.out.println(solution(2, 1, new int[]{10, 5}).equals("3.75"));      // 应该输出 true// 添加更多测试用例System.out.println(solution(12, 4, new int[]{10, 16, 4, 7, 7, 3, 8, 1, 5, 13, 10, 6}).equals("5.62"));  // 应该输出 true
}

}


http://www.mrgr.cn/news/57237.html

相关文章:

  • Artistic Oil Paint 艺术油画着色器插件
  • 生物信息学——三代测序数据:Pacbio
  • C++:反向迭代器
  • YOLO11改进-注意力-引入Large Separable Kernel Attention(LSKA)
  • 状态黑板模式
  • 【计网】理解TCP全连接队列与tcpdump抓包
  • 智能体能和人工智能有什么区别?
  • 云岚到家系统优化
  • 8阻塞队列
  • Python项目内网环境pdm install超时httpx.ReadTimeout: timed out
  • Vue Router实现路由懒加载
  • 简记 Vue3(一)—— setup、ref、reactive、toRefs、toRef
  • Linux中如何理解一切皆文件
  • Python包——Matplotlib
  • 各种数据类型的定义与常规计算
  • 第23周Java主流框架入门-SpringMVC 3.拦截器
  • 【单元测试】深入解剖单元测试的思维逻辑
  • 【论文速看】DL最新进展20241023-多模态、无监督学习、多任务、图像修复
  • 【哈工大_操作系统实验】Lab8 终端设备的控制
  • 买华为系的车,这个理由无法拒绝
  • hass docker openwrt配置
  • 软件分享丨PDF Shaper
  • 不错的二次元个人导航页源码
  • 从新手到高手:map和set的使用技巧全攻略(C++)
  • 重学SpringBoot3-集成Hazelcast
  • 微服务-nacos