牛客读取入参

初始化输入流

Scanner sc = new Scanner(System.in);

读取入参

nextInt():读取下一个整数。
nextDouble():读取下一个浮点数。
nextBoolean():读取布尔值(true 或 false)。
next():读取下一个单词(以空白字符为分隔符)。
nextLine():读取整行(包括空格,直到换行符)。
nextByte()、nextShort()、nextLong():读取其他数值类型。

判断是否还有入参

hasNext():检查是否还有下一个输入项。
hasNextInt():检查下一个输入项是否为整数。
hasNextDouble():检查下一个输入项是否为浮点数。
hasNextLine():检查是否还有下一行。

通常需要与split()搭配分隔入参

String[] s = s.split(" ");

### NC200369 四舍五 解题思路 对于给定的小数字符串,需要按照指定的次数 `t` 进行四舍五操作。每次四舍五都是基于当前最右边的一位小数来决定是否进位。 #### 思路分析 1. **初始化处理** - 首先读取数据并解析成可操作的形式。 - 将原始分数转换为字符列表以便逐位修改[^2]。 2. **核心逻辑** - 对于每一次四舍五: - 如果当前位置小于等于4,则直接截断后续部分; - 若大于等于5则向前一位加一,并继续检查前一位是否会再次触发进位直到不再发生为止。 - 更新剩余需处理的有效位数计数器 `t`,当其减至零时停止循环。 3. **特殊情况考虑** - 当所有有效数字都被处理完毕但仍存在未完成的四舍五需求时,在结果前面补上相应数量的'1'。 #### 代码实现 以下是 Python 实现: ```python def round_number(n, t, num_str): # 转换为list方便操作 nums = list(num_str) dot_index = nums.index('.') start_pos = dot_index + min(t, len(nums) - 1) while t > 0 and '.' not in str(start_pos): current_digit = int(nums[start_pos]) if current_digit >= 5: carry = True for i in range(start_pos - 1, -1, -1): if nums[i].isdigit(): next_digit = (int(nums[i]) + 1) % 10 if next_digit != 0 or i == 0: carry = False nums[i] = str(next_digit) break if carry: nums.insert(0, '1') del nums[start_pos:] t -= 1 try: start_pos = max(dot_index + min(t, len(nums) - dot_index - 1), 0) except ValueError as e: pass result = ''.join([str(x) for x in nums]).rstrip('0').rstrip('.') or "0" return result if __name__ == "__main__": n, t = map(int, input().split()) initial_score = input() print(round_number(n, t, initial_score)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值