leet-code/src/main/java/leetcode/editor/cn/CalculateMoneyInLeetcodeBank.java
2022-01-15 23:41:22 +08:00

66 lines
1.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//Hercy 想要为购买第一辆车存钱。他 每天 都往力扣银行里存钱。
//
// 最开始,他在周一的时候存入 1 块钱。从周二到周日,他每天都比前一天多存入 1 块钱。在接下来每一个周一,他都会比 前一个周一 多存入 1 块钱。
//
// 给你 n ,请你返回在第 n 天结束的时候他在力扣银行总共存了多少块钱。
//
//
//
// 示例 1
//
// 输入n = 4
//输出10
//解释:第 4 天后,总额为 1 + 2 + 3 + 4 = 10 。
//
//
// 示例 2
//
// 输入n = 10
//输出37
//解释:第 10 天后,总额为 (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37 。注意到第二个星期一,
//Hercy 存入 2 块钱。
//
//
// 示例 3
//
// 输入n = 20
//输出96
//解释:第 20 天后,总额为 (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3
//+ 4 + 5 + 6 + 7 + 8) = 96 。
//
//
//
//
// 提示:
//
//
// 1 <= n <= 1000
//
// Related Topics 数学 👍 62 👎 0
package leetcode.editor.cn;
//1716:计算力扣银行的钱
class CalculateMoneyInLeetcodeBank {
public static void main(String[] args) {
//测试代码
Solution solution = new CalculateMoneyInLeetcodeBank().new Solution();
System.out.println(solution.totalMoney(20));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int totalMoney(int n) {
int count = n / 7;
int bef = (56 + 7 * (count - 1)) * count / 2;
int index = n % 7;
int aft = (2 * count + index + 1) * index / 2;
return bef + aft;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}