一维到二维数组

时间:2021-02-16 19:59:32

A program in c++ that accept an integer array and its size as arguments and assign the elements into a 2-D array of integers. for ex: if the array is 1,2,3 The resultant 2-D array is given by

c++中的一个程序,它接受一个整数数组及其大小作为参数,并将元素分配到一个二维整数数组中。对于例:如果数组是1,2,3,则合成的二维数组由

1 2 3
1 2 0
1 0 0

1 2 3 1 2 0 1 0 0

1 个解决方案

#1


3  

can you please give me the logic

你能给我逻辑吗

The logic is very simple. Let n be the size of the 1D array.

逻辑很简单。设n为一维数组的大小。

create a 2d array of size n x n
for y in [0, n)
    copy elements [0, n-y) from the 1D array to line y
    set elements [n-y, n) to 0 in line y

...and that's basically it, where [begin, end) denotes a half open range.

…基本上就是这样,(开始,结束)表示半开放的范围。

#1


3  

can you please give me the logic

你能给我逻辑吗

The logic is very simple. Let n be the size of the 1D array.

逻辑很简单。设n为一维数组的大小。

create a 2d array of size n x n
for y in [0, n)
    copy elements [0, n-y) from the 1D array to line y
    set elements [n-y, n) to 0 in line y

...and that's basically it, where [begin, end) denotes a half open range.

…基本上就是这样,(开始,结束)表示半开放的范围。