使用for循环来找到给定数字区间中数字的最小数字和

huangapple go评论69阅读模式
英文:

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&lt;=n; i++){
        int sm=sum(i);
        if (sm&lt;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."

首先,变量 mincnt 在函数内未声明。如果它们是文件作用域变量,那么在函数中使用文件作用域变量是一个不好的主意。

"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 &lt; m ) )
{
    do
    {
        int sm = sum( m );
        if ( sm &lt; min ) 
        {
            min = sm;
            cnt = 1;
        }
        else if ( !( min &lt; 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 &lt; m ) )
    {
        do
        {
            int sm = sum( m );
            if ( sm &lt; min ) 
            {
                min = sm;
                cnt = 1;
            }
            else if ( !( min &lt; sm ) )
            {
                ++cnt;
            }
        } while ( m++ != n ); 
    }

    return cnt;
}

Pay attention to that the for loop in your original code

for (int i=m; i&lt;=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 &lt;= n; i++)
        if ((sm = sum(i)) &lt; min) min = sm;

    for (int i = m; i &lt;= 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 &lt;= n; i++)
    {
        sm = sum(i);
        if (sm &lt; min) 
        {
            min = sm; 
            cnt = 1;
        }
        else if(sm == min) cnt++;
    }

    return cnt;
}

答案3

得分: 0

代码中存在的问题:

  1. 函数中的 cntmin 没有被初始化(也没有定义)。

  2. 当找到新的最小值时,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:

  1. cnt and min not initialized in the function (nor defined)

  2. cnt is 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&lt;=n; i++){
        int sm=sum(i);
        if (sm&lt;min)
        {
            cnt = 0;   // Reset cnt
            min=sm;
        }
        if (min==sm)  cnt++;
    }
    return cnt;
}

huangapple
  • 本文由 发表于 2023年2月6日 21:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定