如何用对角数编写二维数组? 如何用斜角数在中编写C++ 2D数组 n - size of array (width and height) x - how many the same number in a row c - how many numbers must be used

如何用对角数编写二维数组? 如何用斜角数在中编写C++ 2D数组 n - size of array (width and height) x - how many the same number in a row c - how many numbers must be used,c++,arrays,C++,Arrays,举例 n = 5 x = 2 c = 2 输出为: 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 我当前的代码: #include <iostream> #include <string> using namespace std; int main() { int n=0, x=0, c=0; int temp_x=0,temp_c=-1; cin >> n >&g

举例

n = 5 
x = 2 
c = 2
输出为:

0 0 1 1 0
0 1 1 0 0
1 1 0 0 1
1 0 0 1 1
0 0 1 1 0
我当前的代码:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n=0, x=0, c=0;
    int temp_x=0,temp_c=-1;
    cin >> n >> x >> c;

    c--;
    for(int i=0; i<n;i++){
        for(int j=0; j<n;j++){
            cout << ++temp_c;
            temp_x++;

            if(temp_x>x){
                temp_x=0;
                if(temp_c=c){
                    temp_c=-1;
                }
            }
        }
        cout << endl;
    }
}
我将感谢你的帮助
但是我的代码返回的号码不正确:

您要这样做吗

int main()
{
    int n=0, x=0, c=0;
    int temp_x=0,temp_c=0;
    cin >> n >> x >> c;

    c--;
    for(int i=0; i<n;i++){
        for(int j=0; j<n;j++){

            if(temp_x<x)
            {
                temp_x++;
                cout << temp_c << " ";
                continue;
            }

            temp_c++;
            temp_x=0;

            if(temp_c>c)
            {
                temp_c=0;
            }

            cout << temp_c << " ";
            temp_x++;

            }
            cout << endl;
    }
}

我想提出另一种算法:


你想要什么不清楚你能改进你的例子吗?我的理解是你想在反对角线上写1,宽度为c,间距为x。是吗?对不起,我的英语不是我的母语,这是我关于stackoverflow的第一个问题:你能解释一下这个输入的输出吗n=5 x=2 c=2对于10,2,2我需要::在这种情况下,我的代码与你需要的算法不符:
5 2 2
0 0 1 1 0
0 1 1 0 0
1 1 0 0 1
1 0 0 1 1
0 0 1 1 0

5 2 3
0 0 1 1 2
2 0 0 1 1
2 2 0 0 1
1 2 2 0 0
1 1 2 2 0

5 3 2
0 0 0 1 1
1 0 0 0 1
1 1 0 0 0
1 1 1 0 0
0 1 1 1 0
#include <iostream>
#include <vector>
#include <numeric>  // iota

using std::cout;
using std::endl;

void fill(const size_t n   ///< size of array (width and height)
        , const size_t x   ///< how many the same number in a row
        , const size_t c)  ///< how many numbers must be used
{
    // generate the sequence of possible numbers
    std::vector<int> numbers(c);
    std::iota(numbers.begin(), numbers.end(), 0);

    //std::vector<int> all(n * n);  // for storing the output, if needed
    for (size_t i = 0,            // element index
                k = 0,            // "number" index
                elements = n * n; // the square matrix can also be viewed as a n*n-long, 1D array
         i < elements;
         k = (k + 1) % c)  // next number (and the modulus is for circling back to index 0)
    {
        // print the number "x" times
        for (size_t j = 0; j < x && i < elements; ++j, ++i)
        {
            // break the line every "n" prints
            if ((i % n) == 0)
            {
                cout << endl;
            }

            //all[i] = numbers[k];
            cout << numbers[k] << " ";
        }
    }
    cout << endl;

}

int main()
{
    fill(5, 2, 2);
}
0 0 1 1 0 
0 1 1 0 0 
1 1 0 0 1 
1 0 0 1 1 
0 0 1 1 0