英文:
Using for loop to find minimal sum of digits in given number interval
问题
int minSum(int m, int n) {
    for (int i = m; i <= n; i++) {
        int sm = sum(i);
        if (sm < min) min = sm;
        if (min == sm) cnt++;
    }
    return cnt;
}
英文:
So, I am doing a task that asks to find the amount of positive integers from interval [m, n] with minimum sum of digits.
I declared a function that computes the sum of digits, however I am having problems finding the amount of numbers. Below is the code I wrote for finding the minimum sum of digits and counting. I hope I made myself clear. I would appreciate if you could help me fix this code.
Input: 1 100
Output: 3
int minSum(int m, int n){
  
    for (int i=m; i<=n; i++){
        int sm=sum(i);
        if (sm<min) min=sm;
        if (min==sm)  cnt++;
    }
    return cnt;
}
答案1
得分: 0
以下是翻译好的部分:
"For starters the variables min and cnt are not declared within the function. If they are a file scope variables then using file scope variables in the function is a bad idea."
首先,变量 min 和 cnt 在函数内未声明。如果它们是文件作用域变量,那么在函数中使用文件作用域变量是一个不好的主意。
"The function can look the following way. I assume that sum of digits can not be a negative number."
该函数可以如下所示。我假设数字的总和不会是负数。
"unsigned int minSum( int m, int n )
{
unsigned int cnt = 0;
int min = 0;
if ( !( n < m ) )
{
    do
    {
        int sm = sum( m );
        if ( sm < min ) 
        {
            min = sm;
            cnt = 1;
        }
        else if ( !( min < sm ) )
        {
            ++cnt;
        }
    } while ( m++ != n ); 
}
return cnt;
}"
"Pay attention to that the for loop in your original code
for (int i=m; i<=n; i++){
can invoke undefined behavior for example when n is equal to INT_MAX."
请注意,你原始代码中的 for 循环可能会在 n 等于 INT_MAX 时引发未定义行为。
英文:
For starters the variables min and cnt are not declared within the function. If they are a file scope variables then using file scope variables in the function is a bad idea.
The function can look the following way. I assume that sum of digits can not be a negative number.
unsigned int minSum( int m, int n )
{
    unsigned int cnt = 0; 
    int min = 0;
    if ( !( n < m ) )
    {
        do
        {
            int sm = sum( m );
            if ( sm < min ) 
            {
                min = sm;
                cnt = 1;
            }
            else if ( !( min < sm ) )
            {
                ++cnt;
            }
        } while ( m++ != n ); 
    }
    return cnt;
}
Pay attention to that the for loop in your original code
for (int i=m; i<=n; i++){
can invoke undefined behavior for example when n is equal to INT_MAX.
答案2
得分: 0
int sum(int x)
{
    int result = 0;
    while(x) 
    {
        result += abs(x % 10);
        x /= 10;
    }
    return result;
}
size_t minSum(int m, int n){
    int min = sum(m);
    int sm;
    size_t cnt = 0;
    for (int i = m + 1; i <= n; i++)
        if ((sm = sum(i)) < min) min = sm;
    for (int i = m; i <= n; i++)
        if (sum(i) == min) cnt++;
    return cnt;
}
Or less brute force:
size_t minsum(int m, int n){
    int min = sum(m);
    int sm;
    size_t cnt = 1;
    for (int i = m + 1; i <= n; i++)
    {
        sm = sum(i);
        if (sm < min) 
        {
            min = sm; 
            cnt = 1;
        }
        else if(sm == min) cnt++;
    }
    return cnt;
}
英文:
int sum(int x)
{
    int result = 0;
    while(x) 
    {
        result += abs(x % 10);
        x /= 10;
    }
    return result;
}
size_t minSum(int m, int n){
    int min = sum(m);
    int sm;
    size_t cnt = 0;
    for (int i = m + 1; i <= n; i++)
        if ((sm = sum(i)) < min) min = sm;
    for (int i = m; i <= n; i++)
        if (sum(i) == min) cnt++;
    return cnt;
}
Or less brute force:
size_t minsum(int m, int n){
    int min = sum(m);
    int sm;
    size_t cnt = 1;
    for (int i = m + 1; i <= n; i++)
    {
        sm = sum(i);
        if (sm < min) 
        {
            min = sm; 
            cnt = 1;
        }
        else if(sm == min) cnt++;
    }
    return cnt;
}
答案3
得分: 0
代码中存在的问题:
- 
函数中的
cnt和min没有被初始化(也没有定义)。 - 
当找到新的最小值时,
cnt没有被重置。 
你的代码可以很容易修复,如下所示:
int minSum(int m, int n){
    int cnt = 0;        // 初始化 cnt
    int min = INT_MAX;  // 将 min 初始化为一个较大的值
    for (int i = m; i <= n; i++){
        int sm = sum(i);
        if (sm < min) {
            cnt = 0;   // 重置 cnt
            min = sm;
        }
        if (min == sm)  cnt++;
    }
    return cnt;
}
英文:
Problems:
- 
cntandminnot initialized in the function (nor defined) - 
cntis not reset when a new minimum value is found. 
Your code is easy to fix. Like:
int minSum(int m, int n){
    int cnt = 0;        // Initialize cnt
    int min = INT_MAX;  // Initialize min to a high value
    for (int i=m; i<=n; i++){
        int sm=sum(i);
        if (sm<min)
        {
            cnt = 0;   // Reset cnt
            min=sm;
        }
        if (min==sm)  cnt++;
    }
    return cnt;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论