Help me find a way to find the number of smallest numbers.

#include <iostream> using namespace std; //Сума цифр числа int allsum(int num){ int asum = 0; while(num>0){ asum += num%10; num /= 10; } return asum; } //Перевіряє в якому діапазоні число і знаходить суму цифр int check(int i){ int sum = 0; if( 0 <= i && i <= 9) sum = i; else if( 10 <= i && i <= 99) sum = i%10 + i/10; else if( 100 <= i && i <= 999) sum = allsum(i); else if( 1000 <= i && i <= 9999) sum = allsum(i); else if( 10000 <= i && i <= 99999) sum = allsum(i); else if( 100000 <= i && i <= 999999) sum = allsum(i); return sum; } int main(){ int m,n,count = 0; cin >> m >> n; for(m;m<=n;m++){ if(check(m)==1)count++; } cout << count << endl; return 0; } 

Minimum amount of digits

How many positive integers from the interval [M, N] have the smallest sum of digits?

Input data

In the input file there are two numbers M and N (1 ≤ M ≤ N ≤ 1000000).

Weekend danne

In the output file you need to write the answer - one number.

  • Well, have you already recorded the answer "one number" in the output file? :) - Vlad from Moscow
  • Why do you need so many identical allsum() calls? What is the difference between the calls of this function for 42 and 123456 ? - PinkTux

2 answers 2

I would save on the fact that there is no need to check the input data - they are guaranteed, and it makes no sense to count the amount if it has already exceeded the minimum ...

 int sum(int n, int min) { int s = 0; while(n) { s += n%10; if (s > min) return s; // На максимальном диапазоне эта проверка // ускоряет счет примерно в 3 раза. n/=10; } return s; } int main(int argc, const char * argv[]) { int m, n, min = 100000, count = 0; cin >> m >> n; for(;m <= n;++m) { int s = sum(m,min); if (s == min) count++; else if (s < min) { min = s; count = 1; } } cout << count << endl; } 
  • Honestly, I did not understand why min = 100000? - goodalien
  • Yes, anything more than 9 * 6 = 54. - Harry

Here is a demo program that counts the number of numbers with the smallest sum of digits in a given range. In it, the range is entered from the console and the result is nowhere except the console.

Based on this demo program, you can write your own program that reads data from a file and writes the result to a file.

 #include <iostream> #include <algorithm> unsigned int sum_of_digits( unsigned int x ) { const unsigned int Base = 10; unsigned int sum = 0; do { sum += x % Base; } while ( x /= Base ); return sum; } int main() { while ( true ) { std::cout << "Enter a range of positive numbers m and n (0-exit): "; unsigned int m, n; if ( not ( std::cin >> m >> n ) or ( m == 0 or n == 0 ) ) break; if ( n < m ) std::swap( m, n ); unsigned int min_sum = 0; unsigned int count = 0; for ( unsigned int i = m; not ( n < i ); i++ ) { unsigned int sum = sum_of_digits( i ); if ( i == m or sum < min_sum ) { min_sum = sum; count = 1; } else if ( sum == min_sum ) { ++count; } } std::cout << "\nThere are " << count << " numbers " << std::endl; std::cout << "with minimum sum of digits equal to " << min_sum << std::endl; } return 0; } 

The dialogue with the program may look, for example, as follows.

 Enter a range of positive numbers m and n (0-exit): 1 100 There are 3 numbers with minimum sum of digits equal to 1 Enter a range of positive numbers m and n (0-exit): 0 
  • Let me say that your code does not maintain the not and or style to the end :), but worse, it gives the wrong result. Between 1 and 100 inclusive, there are three numbers with a minimum sum of digits equal to 1: 1 , 10 , 100 ... - Harry
  • @Harry Thank you, there was just a typo, instead of count = 0 instead of count = 1 should be in the if statement. - Vlad from Moscow