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.
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
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.
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.
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.
char GiantNumber = "123392181217"; //Your Giant Number here
int main()
{
unsignedint Length = strlen(GiantNumber); // String Length;
unsignedint 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
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
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;
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).
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( unsignedint 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
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!