5 Replies - 1242 Views - Last Post: 10 April 2013 - 07:57 AM Rate Topic: -----

#1 neondayz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 02-April 13

largest product of n consecutive digits

Posted 05 April 2013 - 07:27 AM

Hello, I have no idea how to start off this question could someone just give me a few tips i don't want the answer because this a homework question. I have tried searcching online but i didn't find anything that helps.

Consider a large positive integer entered by the user (potentially more than 11 digits) and a positive integer n. Find the largest product of n consecutive digits in the number. For example, if the user enters the number 12345043 and 3, then the products are:

1x2x3=6,
2x3x4=24,
3x4x5=60,
4x5x0=0,
5x0x4=0,
0x4x3=0,

so the largest product is 60.

Heres what i have so far i know its all basic but im stuck.

#include <iostream>
using namespace std;

int main()
{
    int n;
    int k;
    cout << "Please enter a number" << endl;
    cin >> n;
    cout << "Please enter the amount of consecutive numbers" << endl;
    cin >> k;

    return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: largest product of n consecutive digits

#2 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: largest product of n consecutive digits

Posted 05 April 2013 - 08:31 AM

Instead of reading the string of numbers straight into an int,
try reading them into a string. This way you can extract individual
numbers from them.

Look into the function atoi() which converts a char into an int.
So from there you can try looping through the chars and get the
individual numbers.

From there I think you will be able to see how you can find your
desired number using a control loop (for-loop)
Was This Post Helpful? 1
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: largest product of n consecutive digits

Posted 08 April 2013 - 06:56 PM

One method would be to enter each individual digit (0 - 9)into an int type container say an array or vector using a loop to control the number of digits and therefore the size of the array. In a second loop with the same 'size' condition multiply i,i+1 and i+2 and if its larger that array[0]*array[1]*array[2] call it MAX. Print MAX which should be the maximum product.
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3459
  • View blog
  • Posts: 10,666
  • Joined: 05-May 12

Re: largest product of n consecutive digits

Posted 08 April 2013 - 08:40 PM

Only one container is needed: a queue.

In pseudo code:
product = 1;
while (character available && queue.Size < n)
{
    digit = character - '0';
    queue.Enqueue(digit);
    product *= digit;
}

max = product;
while (character available)
{
    product /= queue.Dequeue();
    digit = character - '0';
    queue.Enqueue(digit);
    product *= digit;
    if (product > max)
        max = product;
}


Was This Post Helpful? 0
  • +
  • -

#5 neondayz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 02-April 13

Re: largest product of n consecutive digits

Posted 10 April 2013 - 07:22 AM

Here's what i have so far it only works for the example though.
How would i make it so the user enters the large positive integer and the second number.

#include <iostream>
#include <string.h>


using namespace std;

int main ()
{
    char Number[] = "12345043";
    long long highestMultiplication = 0;

    int length = strlen(Number);
    int i = 0;

    while (i < length - 4) {
        int one = Number[i] - '0';
        int two = Number[i+1] - '0';
        int three = Number[i+2] - '0';


        long answer = one * two * three;
        if (answer > highestMultiplication) {
            highestMultiplication = answer;
        }
        i++;
    }

    cout << highestMultiplication << endl;

    return 0;
}



Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3459
  • View blog
  • Posts: 10,666
  • Joined: 05-May 12

Re: largest product of n consecutive digits

Posted 10 April 2013 - 07:57 AM

Your magic number '4' on line 15 seems to correspond to 3+1, and so therefore is N+1. So you can get that input N from the user normally and store it in a variable.

Your array Number is simply a string. You can ask the user to enter a number and store it into a similar array but with more room available. Or better yet, using the C++ string class so you don't have to worry about allocating enough space.

So the only problem you have left will be lines 16-21. Instead of the hardcoded inspection of just 3 elements, you'll need to use some kind of logic to iterate over N elements. I would suggest using a loop. Hint: Do you really need your temporary variables one, two, and three? What happens when you substitute accessing elements in Number directly of using the temp variables?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1