转置1维数组 [英] Transpose 1 Dimensional Array

查看:103
本文介绍了转置1维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个一维数组与N值,其中N是一个完美的正方形。我将这个一维数组可视化为二维数组(尽管它不是)。例如,值为 int Array的数组= {0,1,2,3,4,5,6,7,8}

So I have a ONE dimensional array with N values, where N is a perfect square. I visualize this one dimensional array as a two dimensional array (although it is not). For example, an array with values int Array = { 0,1,2,3,4,5,6,7,8 }

这是

int *Array = new int [9];                                                                                                                                                                                                    
for ( int i = 0 ; i < 9 ; i ++ )
         Array[i] = i; // For example

打印为

0 1 2
3 4 5 
6 7 8

所以,我想交换一维数组中的位置,以便我得到它的转置,...

So, I want to interchange the position in the one dimensional array such that I get the transpose of it,...

例如。 ..

0 3 6
1 4 7
2 5 8

这是基本上相同的一维数组,但 值被交换, strong>数组现在 int Array = {0,3,6,1,4,7,2,5,8}

This is basically the same one dimensional array , but the values are swapped such that the array is now int Array = {0,3,6,1,4,7,2,5,8}

如果我将它扩展为1024 * 1024的数组,逻辑将如何?

If I were to scale it to an array of dimension 1024*1024, how will the logic be ?

推荐答案

p> n = sqrt(N),你可以尝试简单的例子:

With n = sqrt(N), you could just try something simple like:

for(int i = 0; i < n; ++i)
    for(int j = i+1; j < n; ++j)
        std::swap(Array[n*i + j], Array[n*j + i]);

这篇关于转置1维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆