Adjacent repetitions in any number

Say I have a number i.e 9112322544. In this number there are some adjacent repetitions. If repetition then I have to add that numbers.So the number would look like 9223458. Again some repetition is there and number would be 943458.
So we need to do the same operation until there are no more adjacent repetitions. Please help me as soon as possible. Thanks in advance...
pakashsun, Try to follow the following:

1 Create a for loop which goes through all your numbers.

2 Save the numbers two at a time.

3 Compare the two numbers using a conditional statement

4 If they are not identical save the first number (but not the second one because you still have to compare it with the third one), else if they are identical add the numbers & save the result.

5 At the end of the process,I suggest that if identical numbers where found you re-check the whole number again by repeating the same process because you still may have repetitions.

I hope this will help you at least getting started.


It is simple to make everything inside one loop. All what you need is to split the original number into a sequence of digits.
I am able to print the digits in a number, but failing to check if adjacent number is same and add those two same numbers and finally i need output the number doesn't have any repetition.

I have written c++ code to print the digits in a number but not able to do how to add those adjacent same digits.

using namespace std;


int printdigits(int number)
{
if (number == 0)
return 0;

int digit = number % 10;
int a[10];
int sum = printdigits(number / 10);
cout <<digit << " ";
for (i=0; i < 10; i++) {
a[i] = digit;
if( a[i])
return sum+digit;
}


int main(int argc, char *argv[])
{
int intLength =0;
int number;
int digit;
int sum = 0;
string s;
cout << "Please enter an integer ";
cin >>number;
cout << "Orginal Number = "<<number <<endl;
//make the number positive
if (number<0)
number = -number;
intLength = printdigits(number);
//break apart the integer into digits
cout <<endl <<"Sum of the digits is: "<<intLength<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
I do not know maybe there is some splendid algorithm but the task can be done by means of using a stack.

For example that to split up a number into digits the following code can be written

1
2
3
4
unsigned long long x = 9112322544ull;
std::stack<unsigned long long> s;

do { s.push( x % 10 ); } while ( x /= 10 ); 


In fact you need to write two functions. One function extract some value based on digits ( it is either one digit or a sum of several digits if they are equal each other). The other function shall append extracted value to a previos value.

I will show the first function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
unsigned long long next( std::stack<unsigned long long> &s )
{
	if ( s.empty() ) return ( std::numeric_limits<unsigned long long>::max() );

	unsigned long long y = 0;
	unsigned long long digit = s.top();

	do
	{
		y += s.top();
		s.pop();
	} while ( !s.empty() && s.top() == digit );

	return ( y );
}


If you do not know std::numeric_limits then you can change it to -1. For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
unsigned long long next( std::stack<unsigned long long> &s )
{
	if ( s.empty() ) return ( -1 );

	unsigned long long y = 0;
	unsigned long long digit = s.top();

	do
	{
		y += s.top();
		s.pop();
	} while ( !s.empty() && s.top() == digit );

	return ( y );
}


So this function extract a next value from the sequence of digits of the original number.

So you need to write the function append that will append the extracted value to the previous extracted value.

When the main loop of your program will look like

while ( !s.empty() ) dest = append( dest, next( s ) );
Last edited on
By the way if the original number is equal to 1222222 then shall the result be equal to 4?
Last edited on
The other way to solve the task is to use standard function to_string
Topic archived. No new replies allowed.