一共4题,第一题简单,第二题一般,第三四题困难

多少天回本(简单)

买个月卡需要m块,登陆一天送n块,求多少天回本

1
return parseInt((m + n - 1) / n);

最大分割和(一般)

对一个n位的十进制数进行分割,分割后每个数不能大于99,求分割后的最大值

如:12345 -> 1 + 23 + 45 = 69

思路

  1. 使用动态规划思想
  2. dp[i]表示str[0]到str[i]所能分割的最大和
  3. 状态转移方程
    1
    2
    3
    4
    dp[i] = max(
    dp[i - 1] + parseInt(str.slice(i, i + 1)),
    dp[i - 2] + parseInt(str.slice(i - 1, i + 1)),
    );
  4. 初始状态dp[0]=parseInt(str.slice(0, 1)), dp[1]=parseInt(str.slice(0, 2))

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

const fn = (str) => {
const l = str.length;
if (l <= 2) {
return parseInt(str);
}

const dp = [parseInt(str.slice(0, 1)), parseInt(str.slice(0, 2))];
for (let i = 2; i < l; i++) {
let best = Math.max(
dp[i - 1] + parseInt(str.slice(i, i + 1)),
dp[i - 2] + parseInt(str.slice(i - 1, i + 1)),
);
dp.push(best);
}
return dp.at(-1);
};

void (async function () {
// Write your code here
let str = await readline();
console.log(fn(str));
process.exit(0);
})();