Stuck!

Pages: 12
closed account (G854izwU)
Hey guys I'm really stuck on this problem. I have asked about this before but you guys give hard to understand and complex answers for a person who is relatively new to C++. The problem I'm working on is this.

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450


I really don't know any idea where to start! I don't know how to store the number in the program or anything so I need your help to get me started moving in the right direction! Like I said earlier please keep it simple and explain what your talking about!! Thanks

-TheNewKid-
Copy and paste it into your code just like you copied it into this forum. Something like

char* inputValue = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
closed account (G854izwU)
But can I still do math with it being a char and why does the char have an * after it.
But can I still do math with it being a char


No is the short answer. Not in the way you might expect is the longer answer.

Part of the point of this exercise is to make you think about how to take a huge input like this and work with it. Maybe there is some way you can turn each char into an int as you need it?

why does the char have an * after it.

If you don't know what a pointer is, you definitely need to learn.

http://www.cplusplus.com/articles/EN3hAqkS/
and
http://www.cplusplus.com/articles/z186b7Xj/
Last edited on
closed account (G854izwU)
Hey thanks for the links I will read them right now and also while I was eating breakfast I was thinking and what I thought was. Maybe there is a way to take the first 5 numbers in the char convert them to int add them and then do the same with the next set of five. =D Maybe that will work.
But can I still do math with it being a char


You can do math with a character:
1
2
char x = '1',y = '2'
char z = x + y;


Will at least compile, and z will equal x + y. The problem is that z doesn't equal 3, it will actually equal 'c'. Characters are integers, the value of the number is from the ASCII table. But, a character can only hold up to 255, so don't try to store any multiplication as a char.

Just saying, given the examples above, there wouldn't be anything extremely fancy about converting inputValue[0] to be 7 as an integer.
Last edited on
You can do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char GiantNumber = "123392181217"; //Your Giant Number here
int main()
{
	unsigned int Length = strlen(GiantNumber); // String Length;
	unsigned int i = 0;
	while (i < (Length-5)) // While GiantNumber still has 5 Characters to be copied
	{
		char Text[6] = "\0\0\0\0\0\0"; // You will get 5 digits every time here
		strncpy(Text,GiantNumber + i,5); // Copies 5 Characters from GiantNumber with a i offset to Text
		int Number = atoi(Text); // Number now has 5 Digits from the GiantNumber
		// Do here your calculations, they will be done every time you have 5 new digits
		i++; // Increase the GiantNumber offset
	}
}
That is one of those extremely fancy things I was referring to. It actually isn't going to work to solve the problem either. The New Kid needs the product of the 5 consecutive numbers, not the integer value of them
five consecutive digits

That code loops in every combination of five consecutive digits, and finds the integer value of them allowing him to have the calculation he needs.
He needs:

arr[0] * arr[1] * arr[2] * arr[3] * arr[4]

not the integer value of that array.
If you're using a character array to receive this (didn't see the other post, and it seems most of you did) then EssGeEich's answer should work. Another way of writing essentially the same thing is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int product;
int largest=0;
char GiantNumber[lots_of_chars]="1928376648372"; \\whatever
int i=0;

do
{

if(i>lots_of_chars-5) /*If you have to include the loop back around your number, then you need this statement. If not, get rid of this statement.*/
product=int(GiantNumber[i])*int(GiantNumber[i+1]*int(GiantNumber[i+2]*int(GiantNumber[i+3]*int(GiantNumber[i+4];

else
//account for the overloop, I don't feel like typing it all here. If you need me to, I will do it later

if(product>largest)
largest=product;

i++;
}

cout << largest;
Those are much different things.

EssGeEich takes a character array of {'1', '2', '3'}, and transforms it into an integer 123.

You're taking a character array of {'1', '2', '3'} and making an integer of '1' * '2' * '3'.

However, like I said before, '1' * '2' * '3' does not equal 6, even when you static cast it. You're still really doing 61 * 62 * 63. At that rate you are going to overflow your integer. (Also not get the product that you actually want).
Last edited on
I got it. Here is the change:
From
1
2
3
4
5
	char Text[6] = "\0\0\0\0\0\0"; // You will get 5 digits every time here
	strncpy(Text,GiantNumber + i,5); // Copies 5 Characters from GiantNumber with a i offset to Text
	int Number = atoi(Text); // Number now has 5 Digits from the GiantNumber
	// Do here your calculations, they will be done every time you have 5 new digits
	i++; // Increase the GiantNumber offset 

to
1
2
3
4
5
6
	int Product = 1; // Equals 1 so it can be multiplied
	for( unsigned int j = 0; j < 5; j++)
	{
		Product *= GiantNumber[j+i] - '0'; // Takes Integer Value of a Char, but be sure it is a valid '0'-'9' char.
	}
	// Do here your comparations 
Last edited on
Why would they become 61 62 and 63? also, isn't it supposed to be adjacent digits? he would want the 1*2*3
'1' != 1
'1' != 1

and
"1337" != 1337

'1' means the ASCII-value of the 1 character.
1 means a integer-value of 1.
Last edited on
I thought they were the same. My bad.
As a side, it might be interesting to declare your "5" as a constant integer. This way it is easier to alter the size of the data you want to check.

Maybe The New Kid will come back with an answer so we can post spoilers :)
As a side, it might be interesting to declare your "5" as a constant integer
Or maybe even like a variable, so you can edit it at runtime, Lol
closed account (G854izwU)
Hey thanks for all the great post's guys just look at what all happened while I was at school haha! I'm going to look at these post's more in-depth I just skimmed them for the time being and I will try to post what my answer is or at least the start of my answer tonight! Thanks again and if any of you have any more ideas or helpful hints =D please be sure to post them!

-TheNewKid-
Pages: 12