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

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

Using for loop to find minimal sum of digits in given number interval

问题

  1. int minSum(int m, int n) {
  2. for (int i = m; i <= n; i++) {
  3. int sm = sum(i);
  4. if (sm < min) min = sm;
  5. if (min == sm) cnt++;
  6. }
  7. return cnt;
  8. }
英文:

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

  1. int minSum(int m, int n){
  2. for (int i=m; i&lt;=n; i++){
  3. int sm=sum(i);
  4. if (sm&lt;min) min=sm;
  5. if (min==sm) cnt++;
  6. }
  7. return cnt;
  8. }

答案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;

  1. if ( !( n &lt; m ) )
  2. {
  3. do
  4. {
  5. int sm = sum( m );
  6. if ( sm &lt; min )
  7. {
  8. min = sm;
  9. cnt = 1;
  10. }
  11. else if ( !( min &lt; sm ) )
  12. {
  13. ++cnt;
  14. }
  15. } while ( m++ != n );
  16. }
  17. 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.

  1. unsigned int minSum( int m, int n )
  2. {
  3. unsigned int cnt = 0;
  4. int min = 0;
  5. if ( !( n &lt; m ) )
  6. {
  7. do
  8. {
  9. int sm = sum( m );
  10. if ( sm &lt; min )
  11. {
  12. min = sm;
  13. cnt = 1;
  14. }
  15. else if ( !( min &lt; sm ) )
  16. {
  17. ++cnt;
  18. }
  19. } while ( m++ != n );
  20. }
  21. return cnt;
  22. }

Pay attention to that the for loop in your original code

  1. for (int i=m; i&lt;=n; i++){

can invoke undefined behavior for example when n is equal to INT_MAX.

答案2

得分: 0

  1. int sum(int x)
  2. {
  3. int result = 0;
  4. while(x)
  5. {
  6. result += abs(x % 10);
  7. x /= 10;
  8. }
  9. return result;
  10. }
  11. size_t minSum(int m, int n){
  12. int min = sum(m);
  13. int sm;
  14. size_t cnt = 0;
  15. for (int i = m + 1; i <= n; i++)
  16. if ((sm = sum(i)) < min) min = sm;
  17. for (int i = m; i <= n; i++)
  18. if (sum(i) == min) cnt++;
  19. return cnt;
  20. }

Or less brute force:

  1. size_t minsum(int m, int n){
  2. int min = sum(m);
  3. int sm;
  4. size_t cnt = 1;
  5. for (int i = m + 1; i <= n; i++)
  6. {
  7. sm = sum(i);
  8. if (sm < min)
  9. {
  10. min = sm;
  11. cnt = 1;
  12. }
  13. else if(sm == min) cnt++;
  14. }
  15. return cnt;
  16. }
英文:
  1. int sum(int x)
  2. {
  3. int result = 0;
  4. while(x)
  5. {
  6. result += abs(x % 10);
  7. x /= 10;
  8. }
  9. return result;
  10. }
  11. size_t minSum(int m, int n){
  12. int min = sum(m);
  13. int sm;
  14. size_t cnt = 0;
  15. for (int i = m + 1; i &lt;= n; i++)
  16. if ((sm = sum(i)) &lt; min) min = sm;
  17. for (int i = m; i &lt;= n; i++)
  18. if (sum(i) == min) cnt++;
  19. return cnt;
  20. }

Or less brute force:

  1. size_t minsum(int m, int n){
  2. int min = sum(m);
  3. int sm;
  4. size_t cnt = 1;
  5. for (int i = m + 1; i &lt;= n; i++)
  6. {
  7. sm = sum(i);
  8. if (sm &lt; min)
  9. {
  10. min = sm;
  11. cnt = 1;
  12. }
  13. else if(sm == min) cnt++;
  14. }
  15. return cnt;
  16. }

答案3

得分: 0

代码中存在的问题:

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

  2. 当找到新的最小值时,cnt 没有被重置。

你的代码可以很容易修复,如下所示:

  1. int minSum(int m, int n){
  2. int cnt = 0; // 初始化 cnt
  3. int min = INT_MAX; // 将 min 初始化为一个较大的值
  4. for (int i = m; i <= n; i++){
  5. int sm = sum(i);
  6. if (sm < min) {
  7. cnt = 0; // 重置 cnt
  8. min = sm;
  9. }
  10. if (min == sm) cnt++;
  11. }
  12. return cnt;
  13. }
英文:

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:

  1. int minSum(int m, int n){
  2. int cnt = 0; // Initialize cnt
  3. int min = INT_MAX; // Initialize min to a high value
  4. for (int i=m; i&lt;=n; i++){
  5. int sm=sum(i);
  6. if (sm&lt;min)
  7. {
  8. cnt = 0; // Reset cnt
  9. min=sm;
  10. }
  11. if (min==sm) cnt++;
  12. }
  13. return cnt;
  14. }

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:

确定