Leetcode---1716. 计算力扣银行的钱
·

class Solution {
public int totalMoney(int n) {
int start = 1;
int sum = 0;
while(n>=7){
n-=7;
sum = sum + ((start+start+6)*7)/2;
start++;
}
sum = sum + ((start+start+n-1)*n)/2;
return sum;
}
}
分为两种情况,n大于等于7时和n小于7,两种情况
n大于7的时候,就是首项为start,项数为7,差值为1的等差数列。经历数次while循环后,会进入小于7的情况,和>=7时类似,仅仅是等差数列的项数有变化。
更多推荐



所有评论(0)