0

The below code gives SIGTSTP but I am not able to find the fault. The original question link is: https://codeforces.com/contest/102/problem/B . Please Help.

 #include<bits/stdc++.h>
 using namespace std;

  int main()
  {
    int x,sum=0,count=0;
    string s; cin>>s;


    while(s.length()>1){

      for(int i=0;i<s.length();i++){
        x=s[i]-'0';
        sum+=x;
      }
      s=std::to_string(sum);
      count++;
    }


    cout<<count;      
    return 0;
  }
  • 2
    Unrelated to your problem, but please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Jan 26 '20 at 12:27
  • 1
    As for your problem, I recommend that you learn how to use a debugger to step through your code statement by statement while monitoring variables and their values. – Some programmer dude Jan 26 '20 at 12:30
  • Thanks for your suggestion but I applied a hell lot of print statements but none of them helped:( – tempmail 102 Jan 26 '20 at 12:32
  • `#include using namespace std;` - No. Just *no*. Don't *ever* do that. – Jesper Juhl Jan 26 '20 at 12:36
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Jan 26 '20 at 12:38
  • @tempmail102 You are supposed to run the program in a *debugger* on your own machine. A debugger like `gdb` allows you to step through your program line by line and see what exactly it does. Using print statements is a very poor method of debugging in many cases. – walnut Jan 26 '20 at 12:41
  • You forgot to reset the variable sum to 0 at the beginning of each while loop. – NGI Jan 26 '20 at 12:48

1 Answers1

0

Lets say the input is 19.

The first iteration of the outer loop will make sum equal to 10, and sets s to "10".

The second iteration of the outer loop (when s is "10") will add 1 (and 0) to sum, making it equal to 11, so when the inner loops end then s becomes "11".

The third iteration of the outer loop will add 2 (1 and 1) to sum, so sum becomes 13 and s becomes "13".

And so on forever.

The length of s will never become equal to (or smaller than) 1. And that leads to an infinite loop. Which will likely be stopped by the online system due to a timeout.

I haven't read the problem description (in the future please include it in the question, to make your questions self-contained) but your algorithm is wrong, and you need to rethink your solution. Very likely you should reset sum (i.e. sum = 0;) before the inner loop, or better yet define it inside the inner loop:

int x,count=0;
string s; cin>>s;

while(s.length()>1){
  int sum = 0;

  for(int i=0;i<s.length();i++){
    x=s[i]-'0';
    sum+=x;
  }
  s=std::to_string(sum);
  count++;
}

[I reasoned me to this by doing rubber duck debugging of the code]

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621