Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

c++ - How do I find the number with the most divisors in an array?

I have to input numbers into an array and at the end get the number that has the most divisors, or if there are more numbers with the same amount, print out the first one.

Example: 4 numbers, 6 12 48 108. 108 has the most divisors, so this one needs to show up. if there were numbers after 108 with the same amount of divisors, 108 would have still been the only one to show up.

#include <iostream>

using namespace std;

int main()
{
    int n = 0, d, largestCnt = 0;
    int cntA=0, cntB=0;

    cout << "How many elements?
";
    cin >> n;

    int* v = new int[n];

    for(int i=0; i<n; i++)
        cin >> v[i];

    for(int i=0; i<n; i++){
        for(d=2; d<v[i]/2; d++)
            if(v[i]%d==0)
            cntA++;
        for(d=2; d<v[i+1]/2; d++)
            if(v[i+1]%d==0)
            cntB++;
        if(cntA > largestCnt)
            largestCnt = cntA;
        if(cntB > largestCnt)
            largestCnt = cntB;
    }
    cout << largestCnt;

    return 0;
}

this is the most I've done the past 2 hours, and I can't get past it

EDIT:

#include <iostream>

using namespace std;

int main()
{
    int n = 0, d;
    int mostDivisors=0, number_with_most_divisors=0;
    int currentDivisors = 0;

    cout << "How many elements?
";
    cin >> n;

    int* v = new int[n];

    for(int i=0; i<n; i++)
        cin >> v[i];

    number_with_most_divisors = v[0];
    
    for(d=2; d<=v[0]/2; d++){
        if(v[0]%d == 0)
            mostDivisors++;
    }
    for(int i=1; i<n; i++){
        for(d=2; d<=v[i]/2; d++)
            if(v[i]%d == 0)
                currentDivisors++;
                
        if(currentDivisors > mostDivisors){
            mostDivisors = currentDivisors;
            number_with_most_divisors = v[i];
        }
    }
    cout << number_with_most_divisors;
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is the algorithm of what you have to do:

  1. Count the number of divisors of the first element in array. Save this value in mostDivisors. Set number_with_most_divisors as the first element in the array.

  2. Start from the second element in array (position 1) and for each element, count how many divisors it has, save it in currentDivisors. If currentDivisors > mostDivisors then set mostDivisors to be equal to currentDivisors and update number_with_most_divisors to be the current element in the array.

  3. The result is number_with_most_divisors at the end of the loop.

UPDATE

You are forgetting to initialize currentDivisors for each element after the first loop:

for(int i=1; i<n; i++){
        currentDivisors = 0; // You forgot to put this line!
        for(d=2; d<=v[i]/2; d++)
            if(v[i]%d == 0)
                currentDivisors++;
                
        if(currentDivisors > mostDivisors){
            mostDivisors = currentDivisors;
            number_with_most_divisors = v[i];
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...