C++ 如何找到数组中除数最多的数?

C++ 如何找到数组中除数最多的数?,c++,arrays,C++,Arrays,我必须把数字输入一个数组,最后得到除数最多的数字,或者如果有更多相同数量的数字,打印出第一个 示例:4个数字,61248108。108的除数最多,所以这个需要显示。如果108后面的数字具有相同数量的除数,108仍然是唯一出现的数字 #include <iostream> using namespace std; int main() { int n = 0, d, largestCnt = 0; int cntA=0, cntB=0; cout <

我必须把数字输入一个数组,最后得到除数最多的数字,或者如果有更多相同数量的数字,打印出第一个

示例:4个数字,61248108。108的除数最多,所以这个需要显示。如果108后面的数字具有相同数量的除数,108仍然是唯一出现的数字

#include <iostream>

using namespace std;

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

    cout << "How many elements?\n";
    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;
}
#包括
使用名称空间std;
int main()
{
int n=0,d,最大Cnt=0;
int cntA=0,cntB=0;
cout>n;
int*v=新的int[n];
对于(int i=0;i>v[i];
对于(int i=0;i v[i];
除数最多的数=v[0];
对于(d=2;d
//函数,用于计算除数
整数计数因子(整数n)
{ 
int-cnt=0;

对于(int i=1;i,这里是您必须执行的算法:

  • 计算数组中第一个元素的除数。将此值保存在
    mostdivisiors
    中。将
    number\u和\u most\u除数设置为数组中的第一个元素

  • 从数组中的第二个元素开始(位置1)对于每个元素,计算它有多少个除数,将其保存在
    currentDivisors
    中。如果
    currentDivisors>mostDivisors
    则将
    mostDivisors
    设置为等于
    currentDivisors
    并将
    number\u更新为数组中的当前元素

  • 结果是
    number\u,在循环的末尾有\u most\u除数

  • 更新

    在第一个循环之后,您忘记为每个元素初始化
    电流除数

    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];
            }
    
    用于(int i=1;i
    #包括
    int main(){
    int n=0,max\u divs=0,count=0,max\u divs\u index=0;
    标准:cin>>n;
    int*v=新的int[n];
    对于(inti=0;i>v[i];
    对于(int i=0;i最大除数){//检查当前计数是否大于最大除数
    max_divs=count;//更新最大除数
    max_divs_index=i;//使用最大除数更新数组元素的索引
    }
    }
    
    std::我是否只需要使用1个函数,这是我的问题之一?你的问题是什么?它不编译吗?如果是,请复制并粘贴错误消息。它是否打印不正确的输出?如果是,请提供输入、预期和实际输出。有一件事,
    drelated:看看:计算除数对我没有帮助,我需要找到一种方法来计算p我很笨,所以我做的有点像你让我做的,但它不起作用,所以我要用新程序更新我的问题,也许你可以看看it@MickeyMoise将根据您的代码在此处更新
    
    // function to count the divisors 
    int countDivisors(int n) 
    { 
        int cnt = 0; 
        for (int i = 1; i <= sqrt(n); i++) { 
            if (n % i == 0) { 
                // If divisors are equal, 
                // count only one 
                if (n / i == i) 
                    cnt++; 
      
                else // Otherwise count both 
                    cnt = cnt + 2; 
            } 
        } 
        return cnt; 
    } 
    
    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];
            }
    
    #include <iostream>
    
    int main() {
        int n = 0, max_divs = 0, count = 0, max_divs_index = 0;
        std::cin >> n;
        int *v = new int[n];
        for (int i = 0; i < n; ++i) std::cin >> v[i];
        for (int i = 0; i < n; ++i) {
            count = 0; // resetting count to 0 in each iteration
            for (int j = 2; j < v[i]/2; ++j)
                if (v[i] % j == 0) count++; // checking if the number is divisible by numbers between 1 and number itself/2 (1 and num/2 exclusive)
            if (count > max_divs) { // checking if current count is greater than the maximum divisors
                max_divs = count; // updating maximum divisors
                max_divs_index = i; // updating the index of array element with maximum divisors
            }
        }
        std::cout << v[max_divs_index];
        delete[] v;
        return 0;
    }