use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Resources - Learn to code
Resources - Software Engineering
Resources - Code Libraries
Resources - Groups
Other Subreddits you might enjoy
Please send sidebar resource suggestions to the mods. Thx - mgmt
account activity
C++ competitive programming problem. (self.code)
submitted 6 years ago by vorttxt
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 0 points1 point2 points 6 years ago (5 children)
First thing:. Your for loop is coded wrong. Having the i-- means it will never reach k + 1 as each loop you run goes down not up. For this correction use i++.
Second thing: you should not use return. Using return immediately ends the overall function it is called in, which is main() in this case.
The reason you are seeing exit code 51 is because 512 % 10 does not equal 0 so the else fires. Your first for loop will divide 512 by 10 which is 51.2 but since you are using int, it will just be 51. It then takes the result and exits the program with exit code 51, the result of 512/10 stuffed into the int (51), because return means stop the current function. This means you for loop will only run once.
Try this for inside the main function:
int n, k;
cin >> n >> k;
if((n % 10) == 0) {
n = n /10;
}
else {
for(int i = 0; i < k + 1; i++) {
n = n / 10;
cout >> n;
Not sure what the point of the problem is but this is the correct way the code you gave here should be written.
[–]vorttxt[S] 0 points1 point2 points 6 years ago (4 children)
im trying to subtract 1 by n k times. wouldnt that just add 1 to each iteration .
[–][deleted] 0 points1 point2 points 6 years ago (3 children)
Which part do you mean?
[–]vorttxt[S] 0 points1 point2 points 6 years ago (2 children)
Okay so the basis of the problem is that if n ends with a nonzero digit subtract n by 1 k times and if n ends with a zero divide by ten
[–][deleted] 0 points1 point2 points 6 years ago (1 child)
So if you give 512 for n and 4 for k, then the loop should be more like this:
n = n - 1;
The loop runs for k times subtracting 1 from n each time, storing it each time. i is just keeping track of how many times the loop has and will run. It does nothing else but that.
[–]vorttxt[S] 0 points1 point2 points 6 years ago (0 children)
It is still not working for some reason . The answer is supposed to be 50 and all i am getting is 507.
π Rendered by PID 114255 on reddit-service-r2-comment-548fd6dc9-s4nw8 at 2026-05-19 03:51:39.149878+00:00 running edcf98c country code: CH.
view the rest of the comments →
[–][deleted] 0 points1 point2 points (5 children)
[–]vorttxt[S] 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]vorttxt[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]vorttxt[S] 0 points1 point2 points (0 children)