我已经正确地编写了获取大量(长长)超数位的程序,但是由于超时和中止调用,似乎无法通过某些情况。请提出一些优化措施以改善程序的运行时间:
int superDigit(long long m) {
int d=countDigit(m);
if(d==1){
return m;
}
long s=sumDigit(m);
return superDigit(s);
}
//utility functions to calculate digit count and sum of digits
int countDigit(long long n)
{
int count = 0;
while (n != 0) {
n = n / 10;
++count;
}
return count;
}
long sumDigit(long long n)
{
long sum = 0;
while (n != 0) {
sum += n % 10;
n = n / 10;
}
return sum;
}
Run Code Online (Sandbox Code Playgroud)
理论: …