一维数组转二维数组
来源:互联网 发布:淘宝账号永久无法注销 编辑:程序博客网 时间:2024/06/06 03:46
假设你一维数组的大小为100
你需要把它拆分为20*5的一个二维矩阵。
不妨这样去实现:
假设一维数组的下标为X,可以把它在逻辑上转换为二维数组的下标:
m = X / 5
n = X % 5
比如你要找第一行第二列的元素,那在一维数组中就是a[1]
转化为二维坐标就是b[0][1]。
其实多维数组和一维数组一样,在计算机中都是用的一段连续的内存。只是他们的表现形式不一样。
另外,C里边没有动态的二维数组,第二维的大小必须指定的。
引用这个二维矩阵的时候,直接使用B[3][4]来引用第三行第四列的元素?
用宏定义(以下宏定义并没有上机调试,只是提供思路)
#define B[(x)][(y)] A[((x*m)+(y%n)]
你需要把它拆分为20*5的一个二维矩阵。
不妨这样去实现:
假设一维数组的下标为X,可以把它在逻辑上转换为二维数组的下标:
m = X / 5
n = X % 5
比如你要找第一行第二列的元素,那在一维数组中就是a[1]
转化为二维坐标就是b[0][1]。
其实多维数组和一维数组一样,在计算机中都是用的一段连续的内存。只是他们的表现形式不一样。
另外,C里边没有动态的二维数组,第二维的大小必须指定的。
引用这个二维矩阵的时候,直接使用B[3][4]来引用第三行第四列的元素?
用宏定义(以下宏定义并没有上机调试,只是提供思路)
#define B[(x)][(y)] A[((x*m)+(y%n)]
解释一下:x*m,加入你需要B[3][4],那么就是对应一维数组第3*8+4%9=28个元素。
代码一:
但代码一有个缺点,那就是如果有多个最值,只能输出最后一个。下面的代码是经过改进的。
#include<stdio.h>
<span style="white-space:pre"></span>int MAX,MIN;
<span style="white-space:pre"></span>int maxi,maxj,mini,minj;
void main()
{ <span style="white-space:pre"></span>void result(int x[][5],int m,int n);
<span style="white-space:pre"></span>int a[5][5],max,min,i,j;
<span style="white-space:pre"></span> for(i=0;i<5;i++)
<span style="white-space:pre"></span>for(j=0;j<5;j++)
<span style="white-space:pre"></span>scanf("%d",&a[i][j]);
<span style="white-space:pre"></span>MAX=MIN=a[0][0];
<span style="white-space:pre"></span>maxi=maxj=mini=minj=0;
<span style="white-space:pre"></span> result(a,5,5);
<span style="white-space:pre"></span>printf("MAX=%d,maxi=%d,maxj=%d\n",MAX,maxi,maxj);
<span style="white-space:pre"></span>printf("MIN=%d,mini=%d,minj=%d\n",MIN,mini,minj);
}
</pre><pre name="code" class="cpp">void result(int x[][5],int m,int n)
{ <span style="white-space:pre"></span>int i,j;
<span style="white-space:pre"></span> for(i=0;i<m;i++)
<span style="white-space:pre"></span>for(j=0;j<n;j++)
<span style="white-space:pre"></span>{ if(x[i][j]>MAX)
<span style="white-space:pre"></span>{ MAX=x[i][j];
<span style="white-space:pre"></span> <span style="white-space:pre"></span> maxi=i;
<span style="white-space:pre"></span> maxj=j;
<span style="white-space:pre"></span>}
<span style="white-space:pre"></span> if(x[i][j]<MIN)
<span style="white-space:pre"></span>{ MIN=x[i][j];
<span style="white-space:pre"></span> mini=i;
minj=j;
<span style="white-space:pre"></span> }
<span style="white-space:pre"></span>}
}
但代码一有个缺点,那就是如果有多个最值,只能输出最后一个。下面的代码是经过改进的。
代码二:
#include<stdio.h> <span style="white-space:pre"></span>int MAX,MIN; <span style="white-space:pre"></span>int maxi,maxj,mini,minj;
void main()
{ <span style="white-space:pre"></span>void result(int x[][5],int m,int n);
<span style="white-space:pre"></span>int a[5][5],max,min,i,j;
<span style="white-space:pre"></span>for(i=0;i<5;i++) <span style="white-space:pre"></span>
<span style="white-space:pre"></span>for(j=0;j<5;j++)
<span style="white-space:pre"></span>scanf("%d",&a[i][j]);
<span style="white-space:pre"></span>MAX=MIN=a[0][0];
<span style="white-space:pre"></span>maxi=maxj=mini=minj=0;
<span style="white-space:pre"></span>result(a,5,5);
}
void result(int x[][5],int m,int n)
{ <span style="white-space:pre"></span>int i,j;
<span style="white-space:pre"></span>for(i=0;i<m;i++)
<span style="white-space:pre"></span>for(j=0;j<n;j++)
<span style="white-space:pre"></span>{ if(x[i][j]>MAX)
<span style="white-space:pre"></span> MAX=x[i][j];
if(x[i][j]<MIN)
<span style="white-space:pre"></span>MIN=x[i][j];
<span style="white-space:pre"></span> }
<span style="white-space:pre"></span>for(i=0;i<m;i++)
<span style="white-space:pre"></span>for(j=0;j<n;j++)
<span style="white-space:pre"></span> { if(x[i][j]==MAX)
<span style="white-space:pre"></span>{ MAX=x[i][j];
<span style="white-space:pre"></span>maxi=i;
<span style="white-space:pre"></span>maxj=j;
<span style="white-space:pre"></span>printf("MAX=%d,maxi=%d,maxj=%d\n",MAX,maxi,maxj);
<span style="white-space:pre"></span> }
<span style="white-space:pre"></span> if(x[i][j]==MIN)
<span style="white-space:pre"></span>{ MIN=x[i][j];
<span style="white-space:pre"></span> mini=i;
<span style="white-space:pre"></span> minj=j;
<span style="white-space:pre"></span> printf("MIN=%d,mini=%d,minj=%d\n",MIN,mini,minj);
<span style="white-space:pre"></span> }
<span style="white-space:pre"></span>}
}
1 0
- 一维数组转二维数组,旋转
- 一维数组转二维数组
- PHP 一维数组转二维数组
- 一维数组转二维数组,旋转
- c++ 一维数组转二维数组
- 一维数组转二维数组
- IOS一维数组转二维数组
- C#一维数组转二维数组
- 一维数组、二维数组
- Java数组 一维数组,二维数组
- array 数组 一维数组 二维数组
- Javascript - 数组 一维数组 二维数组
- C#数组 一维数组、二维数组、三维数组
- 一维数组,二维数组,三维数组,多维数组整理
- 二级联动;字符串转一维数组,一维数组转二维数,二维数组转DataSet
- js一维数组转二维数组 及 二维数组排序
- 一维数组与二维数组互转
- 一维key value关联数组 转 二维数组
- PHP语言中的SPL
- Android Studio集成Genymotion
- oracle查看锁表进程,杀掉锁表进程
- 系统分区
- easyUI中datagrid如何展示对象下属性的多个子属性
- 一维数组转二维数组
- hibernate+xfire学习整理
- MyEclipse安装SVN插件
- Android点击Button实现功能的几种方法总结
- php 生成笛卡尔集
- elem.attr()无法正确判断checkbox是否选中
- PHP的服务器Apache中httpd.conf 的中文详解
- java动态代理(JDK和cglib)
- cocos2dx CardinalSpline和CatmullRom算法