-2

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.

Programming Question

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;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Eric
  • 53
  • 9
  • 1
    which loop do you want to break the 'i', the 'j' or both ? I think, it break only the 'j' loop. – vcloarec Jul 02 '18 at 21:21
  • ***time limit was exceeded*** Normally in online judge sites this means your algorithm is not efficient enough. Think big O – drescherjm Jul 02 '18 at 21:24
  • @vcloarec Sorry I should have said, I want to break out of the j loop. – Eric Jul 02 '18 at 21:32
  • @drescherjm Yes I need a better algorithm if I was to use java, this is the reason I changed to c++. – Eric Jul 02 '18 at 21:32
  • 1
    Maybe the condition (sum-1==sum2) is never true ... – vcloarec Jul 02 '18 at 21:35
  • Since you are manipulating digits of a number, you may want read the number as a string. You can get the value of a digit by: `digit_value = number_as_text[i] - '0';` – Thomas Matthews Jul 02 '18 at 21:43

1 Answers1

1

AFAICT, you code works fine, at least insofar as that break statement is concerned. I modified it slightly to print out a bit more information (which is never a bad idea in times of trouble) and now it looks like this:

#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;
        }

        cout << "sum=" << sum << "\n";

        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=" << j << " " << endl;
                break;
            }
        }
    }
    return 0;
}

And with this input:

2
12345
6789

It produces:

sum=15
j=12344 
sum=30
j=6788 

Is that what you were expecting?

Live demo - you can experiment with the code there. See also: Why is "using namespace std" considered bad practice?.

Also, factor out sum_digits().

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • thanks for your reply, yes it is something like that but the break statement does not work for me and it prints out multiple "j= xxxx". Do you know why the break statement is not working ? – Eric Jul 02 '18 at 23:20
  • 1
    @Eric No I don't. Are you sure you're running the code exactly as posted? – Paul Sanders Jul 03 '18 at 05:01
  • ***Do you know why the break statement is not working?*** I also don't see how it is not working. – drescherjm Jul 03 '18 at 18:17
  • @drescherjm, yes i got it thanks for your help, how do i give points to people on this> im not exactly sure how to use it. – Eric Jul 07 '18 at 13:58