PROBLEM WITH INTEGER SUM



  • How to add formatted code here? can anyone guide me here? I am new here.


  • Mod

    @bugsfinder2022 sagte in PROBLEM WITH INTEGER SUM:

    How to add formatted code here? can anyone guide me here? I am new here.

    This forum, as most forums, uses a variation of the Markdown language. You can also use the formatting buttons above the edit window if you want to use a GUI instead of using special characters.



  • @bugsfinder2022 sagte in PROBLEM WITH INTEGER SUM:

    How to add formatted code here?

    you need THREE backticks before and after your code (you have TWO). Or just select your code and press the </> button.



  • To solve, consider calculating number / 9 and number % 9.

    Example 19:

    19 / 9 = 2. You have two 9 at the end.
    19 % 2 = 1. A 1 before.
    => 199

    21 / 9 = 2. Two 9s.
    21 % 9 = 3. => 399



  • Modified code, but output is not expected value.
    """
    class Test {

    public:
    static int solve(int N)
    { int sum1 = 0;
    while (N != 0)
    {
    sum1 += N % 10;
    N /= 10;
    }
    return sum1;
    }

    static void smallestnum(int N)
    {
    std::cout << (N % 9 + 1)
    * pow(10, (N / 9))
    - 1;

    }
    };

    int main()
    {
    std::cout << Test::solve(16) << + " --> 79" << std::endl;
    std::cout << Test::solve(19) << + " --> 199" << std::endl;
    std::cout << Test::solve(7) << + " --> 7" << std::endl;

    }

    OUTPUT:
    7 --> 79
    10 --> 199
    7 --> 7
    """



  • Three back-ticks, not three double quotes.
    Double quote: "
    Back-tick: `


  • Gesperrt

    @hustbaer sagte in PROBLEM WITH INTEGER SUM:

    Three back-ticks, not three double quotes.
    Double quote: "
    Back-tick: `

    This person/individual wants to "bullshit you", and no one notices. As an excuse comes: I'm a newbie or my cat ran over the keyboard...



  • Thanks cough bear. I have noted your comments.


  • Mod

    @EinNutzer0 sagte in PROBLEM WITH INTEGER SUM:

    This person/individual wants to "bullshit you", and no one notices. As an excuse comes: I'm a newbie or my cat ran over the keyboard...

    Du merkst vielleicht, dass hier (und in seinem anderen Thread) keine ernsthaften Antworten kommen, sondern jeder nur amüsiert dem Zugunglück zusieht. Auch wir anderen merken, wenn wir verarscht werden 😉



  • @SeppJ sagte in PROBLEM WITH INTEGER SUM:

    . Auch wir anderen merken, wenn wir verarscht werden 😉

    Spätestens bei 'cough bear' ...



  • Modified code as below.

    #include <iostream>
    
    class Test {
    
    public:
    static int solve(int N)
    { int sum1 = 0;
    while (N != 0)
    {
    sum1 += N % 10;
    N /= 10;
    }
    return sum1;
    }
    
    static void smallestnum(int N)
    {
    std::cout << (N % 9 + 1)
    * pow(10, (N / 9))
    - 1;
    
    }
    };
    
    int main()
    {
       int N = 16; 
        Test::smallestnum(N);
        return 0;
    
    }
    
    


  • @bugsfinder2022 Are you usually using java? Thank God, we do not have to put our free-functions in stupid and useless classes!


  • Mod

    Das ist übrigens A051885, falls sich ein Leser wundert, was die Aufgabe soll.


  • Gesperrt

    @bugsfinder2022 If all "functions" in a class are static-defined, there is no need for such a class, especially if it contains no more than two functions.

    Moreover, the class does not contain any attributes at all...



  • Yes, I agree that we dont need classes nor static members here.


  • Gesperrt

    Ich hab mir mal die Mühe gemacht und das implementiert... Ich wäre froh, wenn noch mal jemand draufschauen könnte... 🙂

    #include <iostream>
    #include <vector>
    
    class Smallest
    {
    public:
        long smallest_number_whose_sum_of_digits_is_n(const unsigned int n)
        {
    	if(numbers.size() < n) {
    	    for(unsigned int i = numbers.size(); i < n; i++) {
    		smallest_number_whose_sum_of_digits_is_n(i);
    	    }
    	    return smallest_number_whose_sum_of_digits_is_n(n);
    	}
    	if(numbers.size() == n) {
    	    long start = numbers.back();
    	    long sum;
    	    while((sum = sum_of_digits(start)) != n) {
    		start++;
    	    }
    	    numbers.push_back(start);
    	}
    	return numbers[n];
        }
        long sum_of_digits(long n)
        {
    	long sum = 0;
    	while(n > 0) {
    	    sum += n % 10;
    	    n /= 10;
    	}
    	return sum;
        }
    
    private:
        std::vector<long> numbers { 0 };
    };
    
    int main(int argc, char** argv)
    {
        Smallest s;
        std::cout << " 5: " << s.smallest_number_whose_sum_of_digits_is_n(5) << "\n";
        std::cout << "19: " << s.smallest_number_whose_sum_of_digits_is_n(19) << "\n";
        for(int i = 10; i <= 30; i++) {
    	std::cout << i << ": " << s.smallest_number_whose_sum_of_digits_is_n(i) << "\n";
        }
        return 0;
    }
    
    

    In der main-Methode stehen ein paar Testausgaben.


  • Gesperrt

    Upps, klar gab es noch ein paar Unstimmigkeiten:

    #include <iostream>
    #include <vector>
    
    class Smallest
    {
    public:
        long smallest_number_whose_sum_of_digits_is_n(const unsigned int n)
        {
    	if(numbers.size() < n) {
    	    for(unsigned int i = numbers.size(); i < n; i++) {
    		smallest_number_whose_sum_of_digits_is_n(i);
    	    }
    	}
    	if(numbers.size() == n) {
    	    long start = numbers.back();
    	    while(sum_of_digits(++start) != n)
    		;
    	    numbers.push_back(start);
    	}
    	return numbers[n];
        }
        long sum_of_digits(long n)
        {
    	long sum = 0;
    	while(n > 0) {
    	    sum += n % 10;
    	    n /= 10;
    	}
    	return sum;
        }
    
    private:
        std::vector<long> numbers { 0 };
    };
    
    int main(int argc, char** argv)
    {
        Smallest s;
        std::cout << " 5: " << s.smallest_number_whose_sum_of_digits_is_n(5) << "\n";
        std::cout << "19: " << s.smallest_number_whose_sum_of_digits_is_n(19) << "\n";
        for(int i = 0; i <= 30; i++) {
    	std::cout << i << ": " << s.smallest_number_whose_sum_of_digits_is_n(i) << "\n";
        }
        return 0;
    }
    
    

  • Gesperrt

    @EinNutzer0 sagte in PROBLEM WITH INTEGER SUM:

    ++start

    Das ist nicht besonders performant... Beispiel:

        for(int i = 0; i < std::numeric_limits<int>::max(); i++) {
    	std::cout << i << ": " << s.smallest_number_whose_sum_of_digits_is_n(i) << "\n";
        }
    

    😭


  • Gesperrt

    Jetzt aber / Now for it:

    #include <algorithm>
    #include <iostream>
    #include <limits>
    #include <vector>
    
    class Smallest
    {
    public:
        long smallest_number_whose_sum_of_digits_is_n(const unsigned int n)
        {
    	if(n > 162) {
    	    return -1;
    	}
    	if(numbers.size() < n) {
    	    for(unsigned int i = numbers.size(); i < n; i++) {
    		smallest_number_whose_sum_of_digits_is_n(i);
    	    }
    	}
    	if(numbers.size() == n) {
    	    long start = numbers.back();
    	    while(sum_of_digits(start = inc_backwards(start)) != n)
    		;
    	    numbers.push_back(start);
    	}
    	return numbers[n];
        }
        long sum_of_digits(long n)
        {
    	long sum = 0;
    	while(n > 0) {
    	    sum += n % 10;
    	    n /= 10;
    	}
    	return sum;
        }
        long inc_backwards(long n)
        {
    	std::string s = std::to_string(n);
    	std::reverse(s.begin(), s.end());
    	if(s.back() == '9') {
    	    s.push_back('0');
    	}
    	s = std::to_string(std::stol(s) + 1);
    	std::reverse(s.begin(), s.end());
    	return std::stol(s);
        }
    
    private:
        std::vector<long> numbers { 0 };
    };
    
    int main(int argc, char** argv)
    {
        Smallest s;
        for(int i = 0; i <= 200; i++) {
    	std::cout << i << ": " << s.smallest_number_whose_sum_of_digits_is_n(i) << "\n";
        }
        return 0;
    }
    
    


  • @EinNutzer0 Warum nicht die Formel?

    #include <iostream>
    #include <cmath>
    
    int main(int argc, char** argv)
    {
        for(int i = 0; i <= 50; i++) {
            std::cout << i << ": " << (i%9 + 1) * std::pow(10, i / 9) - 1 << "\n";
        }
    }
    

Anmelden zum Antworten