都来帮我看看!~!!!请那位大哥帮帮忙?小弟先谢/..你们都来帮我看看

zezeze 2005-10-14 04:36:21
1 2 6 7 15
3 5 8 14 16
4 9 13 17 22
10 12 18 21 23
11 19 20 24 25
如图有如此规律的一个阵..题目意思..由用户输入要产生的行和列的值..然后根据这个规律给用户显示出用户输入的几行几列的阵!!
...全文
131 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
djfu 2005-10-15
  • 打赏
  • 举报
回复
OK , 搞定!
【思路】:以斜向的对角线的值递增分析为主。
【源码】:如下:

#include <iostream>
using namespace std;

typedef int BOOL;
#define TRUE 1
#define FALSE 0
#define DownToUp TRUE
#define UpToDown FALSE

int min(int x, int y)
{
return x < y ? x : y;
}

int max(int x, int y)
{
return x > y ? x : y;
}

int main()
{
int nRow, nCol, nFoldLine;
cout << "Please enter matrix row and column number:" ;
cin >> nRow >> nCol;
cout << endl;

nFoldLine = nRow + nCol -1;
int i, j, nR, nC;

int **a = new int*[nRow];
for (i = 0; i < nRow; i++)
a[i] = new int[nCol];

for(i = 0; i < nRow; i ++)
for(j = 0; j < nCol; j ++)
a[i][j] = 0;


BOOL Order = DownToUp;
int nCurValue = 1;
for( i = 0; i < nFoldLine; i++ )
{
if(Order == UpToDown)
{
for(nR = max(0, i - nCol + 1); nR <= min(i, nRow-1); nR ++)
{
nC = i - nR;
a[nR][nC] = nCurValue ++;
}
}
else if(Order == DownToUp)
{
for(nR = min(nRow - 1, i); nR >= 0; nR --)
{
nC = i - nR;
if(nC >= nCol )
break;
a[nR][nC] = nCurValue ++;
}
}

Order = !Order;
}

cout << "The matrix's row = " << nRow << ",column = " << nCol << ", totoal size = " << nRow * nCol << endl;
cout << "The whole matrix is :" << endl;
for(i = 0; i < nRow; i ++)
{
for(j = 0; j < nCol; j++)
cout << a[i][j] << "\t" ;
cout << endl;
}

for (i = 0; i < nRow; i++)
delete [nCol] a[i];
delete [nRow] a;

getchar();
return 0;
}

【示例一】:
Please enter matrix row and column number:15 7

The matrix's row = 15,column = 7, totoal size = 105
The whole matrix is :
1 2 6 7 15 16 28
3 5 8 14 17 27 29
4 9 13 18 26 30 42
10 12 19 25 31 41 43
11 20 24 32 40 44 56
21 23 33 39 45 55 57
22 34 38 46 54 58 70
35 37 47 53 59 69 71
36 48 52 60 68 72 84
49 51 61 67 73 83 85
50 62 66 74 82 86 95
63 65 75 81 87 94 96
64 76 80 88 93 97 102
77 79 89 92 98 101 103
78 90 91 99 100 104 105
Press any key to continue

【示例二】:
Please enter matrix row and column number:5 5

The matrix's row = 5,column = 5, totoal size = 25
The whole matrix is :
1 2 6 7 15
3 5 8 14 16
4 9 13 17 22
10 12 18 21 23
11 19 20 24 25
Press any key to continue
【示例三】:
Please enter matrix row and column number:1 9

The matrix's row = 1,column = 9, totoal size = 9
The whole matrix is :
1 2 3 4 5 6 7 8 9
Press any key to continue
【示例四】:
Please enter matrix row and column number:9 1

The matrix's row = 9,column = 1, totoal size = 9
The whole matrix is :
1
2
3
4
5
6
7
8
9
Press any key to continue
xiaocai0001 2005-10-14
  • 打赏
  • 举报
回复
又是一个蛇形阵啊~~

http://community.csdn.net/Expert/topic/4300/4300725.xml?temp=.614773

看看上面那个帖子

跟你的阵基本上一个样

33,028

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧