算weeks里面算了几次,再加上剩下来的不满一周的天数攒的钱
class Solution {
public:
int totalMoney(int n) {
int weeks = n / 7;
int days = n - (7 * weeks);
int sumDay(0);
int firstDay(1); // 第1次存钱的周一存1块,以后的每个周一加1
for (int i = 0; i < weeks; i++) {
for (int i = firstDay; i < (firstDay+7); i++) {
// 周一到周日
sumDay += i;
}
firstDay += 1; // 更新周一的存钱值
}
// 算剩下的天数存的钱
for (int i = firstDay; i < (firstDay + days); i++) {
sumDay += i;
}
return sumDay;
}
};