PROBLEM WITH INTEGER SUM
-
I am trying to generate the below sequence. below is my code, which doesnot give expected output. What is wrong here?
`` cpp
#include <iostream>class Test {
public:
static int solve(int N)
{
if (N == 0)
{
return 0;
}
if (N <= 9)
{
return N;
}
int sum1 = 0;
while (N != 0)
{
sum1 += N % 10;
N /= 10;
}int sum2 = 0; for (int i = 10; i <= 1200; i++) { sum2 += i; if (sum2 == sum1) { sum1 = sum2; } } return sum1;
}
};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
-
N solve
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 19
11 29
12 39
13 49
14 59
15 69
16 79
17 89
18 99
19 199
20 299
21 399
22 499
-
I need to get the above pattern. What is wrong in my code logic? How to fix it?
-
Any suggestions?
-
I suspect for loop is having problems. How to fix it to generate sequence?
-
Hier sieht es verdächtig nach Spam aus. Jedenfalls wurde sich nicht an die normalen Verhaltensregeln gehalten, imho.
-
@EinNutzer0 sagte in PROBLEM WITH INTEGER SUM:
Hier sieht es verdächtig nach Spam aus. Jedenfalls wurde sich nicht an die normalen Verhaltensregeln gehalten, imho.
Ungewöhnlich, klar. Kann aber nix konkretes entdecken. Ob es bei der Qualität der Fragestellung eine Antwort geben wird, ist natürlich fraglich, aber da ist er bei weitem nicht der einzige.
-
@SeppJ sagte in PROBLEM WITH INTEGER SUM:
@EinNutzer0 sagte in PROBLEM WITH INTEGER SUM:
Hier sieht es verdächtig nach Spam aus. Jedenfalls wurde sich nicht an die normalen Verhaltensregeln gehalten, imho.
Ungewöhnlich, klar. Kann aber nix konkretes entdecken. Ob es bei der Qualität der Fragestellung eine Antwort geben wird, ist natürlich fraglich, aber da ist er bei weitem nicht der einzige.
Da hast du wahr. Ich für meinen Teil helfe hierbei jedenfalls mal nicht, weil ich den unformatierten Code gar net lesen kann.
Andere mögen sich beteiligen.
-
How to add formatted code here? can anyone guide me here? I am new here.
-
@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
andnumber % 9
.Example 19:
19 / 9 = 2. You have two 9 at the end.
19 % 2 = 1. A 1 before.
=> 19921 / 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:`
-
@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.
-
@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!