I am pretty new to this and I will try to keep this post as easy as good as possible with images etc.
I am currently solving programming problems on Kattis,
On my last post this nice guy edited it for me to make it look nice, it showed the picture of the programming question but it only seems to let me show a link.
I first solved it in java, and although I was getting the correct answers, it could not be accepted as the time limit was exceeded.
I decided to solve it in c++, I copied my code from java to c++ and it seems as though the break statements in c++ is acting differently than java.
Here is my code for c++, I need the loop to completely break when I have found the answer, but it keeps going looking for more answers. The loops breaks perfectly in java, does anyone know why it wont work in c++?
#include <iostream>
using namespace std;
int main() {
int cases;
cin >> cases;
for (int i = 0; i < cases; i++)
{
int n;
cin >> n;
int sum = 0;
int digits = n;
while (digits > 0)
{
sum += digits % 10;
digits = digits / 10;
}
for (int j = n - 1; j >= 0; j--)
{
int sum2 = 0;
int digits2 = j;
while (digits2 > 0)
{
sum2 += digits2 % 10;
digits2 /= 10;
}
if(sum-1 == sum2)
{
cout << j << " " << endl;
break;
}
}
}
return 0;
}