如何找到数组中每个数字的因数

时间:2019-02-15 15:25:25

标签: c++

我有一个由用户输入的数字数组,然后程序按升序对其进行排序。我只需要找到一种方法来获取数组中每个数字的因数并将其打印出来

#include "stdafx.h"
#include <iostream>
#include <limits>
#define MAX 200

using namespace std;
int arr[MAX];
int n, i, j, k;
int temp;


int main()
{
    //array declaration
        int arr[MAX];
    int n, i, j;
    int temp;

    //read total number of elements to read
    cout << "Enter total number of numbers to read: ";
    cin >> n;

    //check bound
    if (n<0 || n>MAX)
    {
        cout << "Input valid range!!!" << endl;
        return -1;
    }

    //read n elements
    for (i = 0; i < n; i++)
    {
        cout << "Enter element [" << i + 1 << "] ";
        cin >> arr[i];
        cout << endl;
    }

    //print input elements
    cout << "Unsorted Array elements:" << endl;
    for (i = 0; i < n; i++)
        cout << arr[i] << "\t";
    cout << endl;

    //sorting - ASCENDING ORDER
    for (i = 0; i<n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (arr[i]>arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

    //print sorted array elements
    cout << endl;
    cout << "Sorted (Ascending Order) Array elements:" << endl;
    for (i = 0; i < n; i++)
        cout << arr[i] << "\t";
    cout << endl <<endl;

    //trying to find factors
    cout << "Factors of " << arr[i] << " are: " << endl;
    for (k = 1; k <= arr[i]; ++i)
    {
        if (arr[i] % k == 0)
            cout << k << endl;
    }
system ("pause")
return 0;
}

我希望它使用以下命令打印数组中的每个数字 “(数量)的因素是...” “(下一个数字)的因素是...”

以此类推

2 个答案:

答案 0 :(得分:2)

最后一个for循环应与k循环,而您忘记增加k。 您还应该编写i循环:

//trying to find factors
for (i = 0; i < n; i++)
{
    cout << "Factors of " << arr[i] << " are: " << endl;
    for (k = 1; k <= arr[i]; ++k)
    {
        if (arr[i] % k == 0)
            cout << k << endl;
    }
}

此外,正如@LocTran指出的那样,外部循环的上限应为n-1。 另外,您可以使用arr轻松对std::sort进行排序,如下所示:

std::sort(arr, arr+n);

那么您的代码将很适合您:

Live Demo

答案 1 :(得分:1)

您的源代码存在一些问题。

1>外循环排序问题

    //sorting - ASCENDING ORDER
    for (i = 0; i < (n-1); i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (arr[i] > arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

外部循环的上限应为(n-1),而不是n,如下所示,但也许您很幸运,当n

    //sorting - ASCENDING ORDER
    //for (i = 0; i < n; i++)
    for (i = 0; i < (n-1); i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (arr[i] > arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    } 

2>整个数组的打印功能,您应该为数组的索引添加外循环,并在循环中也通过k ++更改i ++

//trying to find factors
cout << "Factors of " << arr[i] << " are: " << endl;
for (k = 1; k <= arr[i]; ++i)
{
    if (arr[i] % k == 0)
        cout << k << endl;
}

应替换为

    //trying to find factors
    for (i = 0; i < n; i++)
    {
        cout << "Factors of " << arr[i] << " are: " << endl;
        //for (k = 1; k <= arr[i]; ++i)
        for (k = 1; k <= arr[i]; ++k)
        {
            if (arr[i] % k == 0)
                cout << k << endl;
        }
    }

这是我基于修改后的源代码的解决方案

#include <iostream>
#include <limits>
#define MAX 200

using namespace std;
int arr[MAX];
int n, i, j, k;
int temp;


int main()
{
    //array declaration
    int arr[MAX];
    int n, i, j;
    int temp;

    //read total number of elements to read
    cout << "Enter total number of numbers to read: ";
    cin >> n;

    //check bound
    //if (n<0 || n>MAX)
    if (n<0 || n>MAX)
    {
        cout << "Input valid range!!!" << endl;
        return -1;
    }

    //read n elements
    for (i = 0; i < n; i++)
    {
        cout << "Enter element [" << i + 1 << "] ";
        cin >> arr[i];
        cout << endl;
    }

    //print input elements
    cout << "Unsorted Array elements:" << endl;
    for (i = 0; i < n; i++)
        cout << arr[i] << "\t";
    cout << endl;

    //sorting - ASCENDING ORDER
    //for (i = 0; i < n; i++)
    for (i = 0; i < (n-1); i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (arr[i] > arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

    //print sorted array elements
    cout << endl;
    cout << "Sorted (Ascending Order) Array elements:" << endl;
    for (i = 0; i < n; i++)
        cout << arr[i] << "\t";
    cout << endl << endl;

    //trying to find factors
    for (i = 0; i < n; i++)
    {
        cout << "Factors of " << arr[i] << " are: " << endl;
        //for (k = 1; k <= arr[i]; ++i)
        for (k = 1; k <= arr[i]; ++k)
        {
            if (arr[i] % k == 0)
                cout << k << endl;
        }
    }
    return 0;
}