题解 | #神奇牛的体重#
神奇牛的体重
https://www.nowcoder.com/practice/456d819bb5b3440eb147296dbfa7af80
知识点
函数使用
解题思路
使用Math.pow方法来计算牛的体重。根据题目的描述,经过m天后,牛的体重变为n * m * m公斤。使用Math.pow(m, 2)来计算m的平方。然后,将牛的初始体重n与m的平方相乘得到体重的精确值。最后,使用Math.round方法将体重四舍五入到整数,并返回结果。
Java题解
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @param m int整型 * @return long长整型 */ public long weightAfterMDays (int n, int m) { // write code here double weight = n * Math.pow(m, 2); long roundedWeight = Math.round(weight); return roundedWeight; } }