我们必须找到构成给定数字所需的最少位数,例如:14 => 95 (9 + 5 = 14) 是两位数,这是构成 14 的最小值。
int moves(int n) {
int m = 0; // Minimum count
while (n-9 >= 0) { // To place maximum number of 9's
n -= 9;
m++;
}
if (n == 0) { // If only nines made up the number
return m;
}
else {
m++;
return m;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了在线法官的 TLE(超出运行时间限制)。我该如何改进它或者有更好的方法?