Hey ppl,
Im currently writing a code for the following problem.

"
Problem Statement

***Note: Please keep programs under 7000 characters in length. Thank you


Class Name: SquareDigits
Method Name: smallestResult
Parameters: int
Returns: int

Define the function S(x) as the sum of the squares of the digits of x.
For example: S(3)=3*3=9 and S(230)=2*2+3*3+0*0=13.

Define the set T(x) to be the set of unique numbers that are produced by
repeatedly applying S to x. That is: S(x), S(S(x)), S(S(S(x))), etc...
For example, repeatedly applying S to 37:
S(37)=3*3+7*7=58.
S(58)=5*5+8*8=89.
S(89)=145.
S(145)=42.
S(42)=20.
S(20)=4.
S(4)=16.
S(16)=37.
Note this sequence will repeat so we can stop calculating now and:
T(37)={58,89,145,42,20,4,16,37}.
However, note T(x) may not necessarily contain x.

Implement a class SquareDigits, which contains a method smallestResult. The
method takes an int, n, as a parameter and returns the smallest int, x, such
that T(x) contains n.

The method signature is (be sure your method is public):
int smallestResult(int n);

TopCoder will ensure n is non-negative and is between 0 and 199 inclusive.

Examples:
If n=0: S(0) = 0, so T(0)={0}, so the method should return 0.

If n=2: T(0) through T(10) do not contain the value 2. If x=11, however:
S(11)=1*1+1*1=2, so T(11) contains 2, and the method should return 11.

If n=10: T(0) through T(6) do not contain 10. If x=7:
S(7)=49.
S(49)=97.
S(97)=130.
S(130)=10.
S(10)=1.
and it starts to repeat...
so T(7) is {49,97,130,10,1}, which contains 10, and the method should return 7.

n=1 -> x=1
n=19 -> x=133
n=85 -> x=5
n=112 -> x=2666
Definition

Class:
SquareDigits
Method:
smallestResult
Parameters:
int
Returns:
int
Method signature:
int smallestResult(int param0)
(be sure your method is public)


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. "

In the process, i have written a code and it is in a intermediary stage now. I have given the code below. While i compiled this code, it didn't show me an error. But it gives me a segmentation fault in the run time.

#include<iostream>
#include<vector>

using namespace std;

class SquareDigits
{
	public:
		int smallestResult (int);
};

int SquareDigits :: smallestResult (int input)
{
	int i=0,n=1,nc=2,flags=0,flagc=0,sx,quotient,remainder,q,r;
	vector<int> tx;
	vector<int>::iterator it,it1;

		do
		{
			if(nc<100)
			{
				quotient = nc/10;
				remainder = nc%10;
				sx = (quotient*quotient) + (remainder*remainder);
				tx.push_back(sx);
				nc = sx;
			}

			else if(nc>100)
			{
				quotient = nc/100;
				remainder = nc%100;
				q = remainder/10;
				r = remainder%10;
				sx = (quotient*quotient) + (q*q) + (r*r);
				tx.push_back(sx);
				nc = sx;
			}
			
			for(it = tx.begin(); it != tx.end(); it++)
			{
				for(it1 = it++; it1 != tx.end(); it1++)
				{
					if(*it == *it1)
					{
						flags=1;
					}
				}
			}
		}while(flags==0);

                if(flags==1)
			{
				for(it = tx.begin(); it != tx.end(); it++)
				{
					cout << *it << endl;
				}
			}
	return 0;
}

Recommended Answers

All 7 Replies

The problem is on line 46. The first time through where is only one number in the vector and iterator it is set to tx.end() because you incremented it on line 44.

Oops. Forgot that part. Will an if loop checking if(it++ != tx.end()) help me solve this problem?

That will most likely crash too. If it already is tx.end() then incrementing it again won't work. And you're using the post-increment operator which means the comparison is made before incrementing, and then you will have the exact problem that you have now.

Maybe what you want is this: for(it1 = it + 1; it1 != tx.end();

it

is an integer pointer or something similar to an array subscript?

Thanks ancient dragon, your suggestion worked. But i still have some doubts.

1) Iterator is a pointer or subscript

2) When i increment the int vector's iterator, it increases by 1 or the size allocated to an integer variable?

1) most likely, but that would be implementation dependent, which means compilers can do it however they wish.

2) Neither. It advances to the next object in the vector, whatever that is.

Thanks ancient dragon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.