C++ 一种数组,其中每个元素都是一个数的除数和下一个数的除数

C++ 一种数组,其中每个元素都是一个数的除数和下一个数的除数,c++,arrays,C++,Arrays,首先,我是编程新手。我实际上是一名高中生,因此我的问题听起来可能很愚蠢。 我想创建一个数组,其中每个元素都是一个数字的除数和下一个数字的除数:例如,对于n=12 1 2 6 12 我的解决方案: #include <iostream> using namespace std; int main() { int n, d, i, j, a[100]; cin>>n; for(d=n; d>=1; d--) { if

首先,我是编程新手。我实际上是一名高中生,因此我的问题听起来可能很愚蠢。 我想创建一个数组,其中每个元素都是一个数字的除数和下一个数字的除数:例如,对于n=12

1 2 6 12
我的解决方案:

#include <iostream>

using namespace std;

int main()
{
    int n, d, i, j, a[100];
    cin>>n;
    for(d=n; d>=1; d--)
    {
        if(n%d==0)
            j++;
    }

    for(i=1; i<=j; i++)
    {
        for(d=n; d>=1; d--)
        {
            if(n%d==0)
                a[i]=n%d;
        }
    }
    for(i=1; i<=j; i++)
    {
        cout<<a[1];
        if(a[i]%a[i+1]==0)
            cout<<a[i+1]<<" ";
    }
    return 0;
}
#包括
使用名称空间std;
int main()
{
int n,d,i,j,a[100];
cin>>n;
对于(d=n;d>=1;d--)
{
如果(n%d==0)
j++;
}
对于(i=1;i=1;d--)
{
如果(n%d==0)
a[i]=n%d;
}
}
for(i=1;ij用于计算a中的索引,但它从未在main中初始化

如果(n%d==0)a[i]=n%d;
so
a[i+1]
始终为0,则初始化do
,因此模总是无效的

此外,因为只初始化索引j之前的a,所以当i为j时,通过
a[i+1]
访问未初始化的值


一种可能性是:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
  int n;

  if (!(cin >> n))
    return -1;

  if (n == 0) {
    cout << "there are an infinite number of solution for 0" << endl;
    return -1;
  }

  vector<int> v;

  // 1 is a first value
  v.push_back(1);

  int max = (n > 0) ? n : -n;

  for (int d = 2; d <= max; ++d) {
    if ((n % d) == 0) {
      // can be a candidate, check if compatible with last value
      if ((d % v.back()) == 0)
        v.push_back(d);
    }
  }

  for (vector<int>::const_iterator it = v.begin(); it != v.end(); ++it)
    cout << *it << ' ';
  cout << endl;

  return 0;
}

请注意,如果我从给出其他值的数字而不是1中搜索:

#include <iostream>
#include <list>
using namespace std;

int main()
{
  int n;

  if (!(cin >> n))
    return -1;

  if (n == 0) {
    cout << "there are an infinite number of solution for 0" << endl;
    return -1;
  }

  list<int> v;

  // the number itself is a first value
  v.push_back(n);

  for (int d = ((n > 0) ? n : -n) - 1; d != 0; --d) {
    if ((n % d) == 0) {
      // can be a candidate, check if compatible with first value
      if ((v.front() % d) == 0)
        v.push_front(d);
    }
  }

  for (list<int>::const_iterator it = v.begin(); it != v.end(); ++it)
    cout << *it << ' ';
  cout << endl;

  return 0;
}

欢迎使用堆栈溢出!请阅读。关于您的问题,您必须从代码中提取a,并更准确地了解您实际观察到的内容。如果不进行深入分析,请检查缓冲区溢出和未初始化值的使用情况。欢迎使用堆栈溢出!听起来您可能需要学习如何使用调试使用一个好的调试器,你可以逐行执行你的程序,看看它偏离了你的预期。如果你要做任何编程,这是一个必不可少的工具。进一步阅读:并在if(a[i]%a[i+1]==0)处设置断点并检查i的值。它将超过数组的大小。欢迎使用堆栈溢出和一般编程!有一个问题(以及可能的后续问题)您是否遇到编译或运行时错误?数组索引是基于0的,即
a[100]
的有效索引是0到99而不是1到100
pi@raspberrypi:/tmp $ g++ -pedantic -Wextra v.cc
pi@raspberrypi:/tmp $ ./a.out
12
1 2 4 12 
pi@raspberrypi:/tmp $ 
#include <iostream>
#include <list>
using namespace std;

int main()
{
  int n;

  if (!(cin >> n))
    return -1;

  if (n == 0) {
    cout << "there are an infinite number of solution for 0" << endl;
    return -1;
  }

  list<int> v;

  // the number itself is a first value
  v.push_back(n);

  for (int d = ((n > 0) ? n : -n) - 1; d != 0; --d) {
    if ((n % d) == 0) {
      // can be a candidate, check if compatible with first value
      if ((v.front() % d) == 0)
        v.push_front(d);
    }
  }

  for (list<int>::const_iterator it = v.begin(); it != v.end(); ++it)
    cout << *it << ' ';
  cout << endl;

  return 0;
}
pi@raspberrypi:/tmp $ g++ -pedantic -Wextra vv.cc
pi@raspberrypi:/tmp $ ./a.out
12
1 3 6 12