输出方阵上三角

aqian_ 2012-03-11 03:43:28
方阵的主对角线之上称为“上三角”,请设计一个用于填充n阶的上三角区域。
例如 :当n=3时,则输出

1 2 3

6 4

5

当n=4时,则输出

1 2 3 4

9 10 5

8 6

7

其中n在(3~20)
我写了写,一直写不对,程序有问题,希望各位帮帮忙,指引一下。在3到5之间都能正确输出,6之后就有问题了
程序如下:
#include <stdio.h>
void main()
{
int n,i,j,k=1,m;
printf("请输入方针的阶数:");
scanf("%d",&n);
m=(1+n)*n/2; //记录数组个数
int a[20][20];
int row1=0,row2=n,col1=0,col2=n;//row1,col1分别是行和列的上限,row2,col2分别是行和列的下限

for (i=0;i<n;i++) //对每个元素赋初值0
for(j=0;j<n;j++)
a[i][j]=0;
while (k<=m)
{
for (j=col1;j<col2;j++) //对每行赋值
{
if(a[row1][j]==0)
{
if (k>m)
{
break;
}
a[row1][j]=k;k++;

}
}
col2=j-1;
for (i=row1+1,j=col2-1;i<row2&&j>=col1;j--,i++)//对对角线那列赋值
{

if (a[i][j]==0)
{
if (k>m)
{
break;
}
a[i][j]=k;k++;

}

}
row2=i-1;
col1=j+1;
for (i=row2-1;i>row1;i--) //对竖列赋值
{
if (a[i][col1]==0)
{
if (k>m)
{
break;
}
a[i][col1]=k;k++;
}

}
row1=i+1;
}
for (i=0;i<n;i++) //输出
{
for (j=0;j<n;j++)
{
if(a[i][j]!=0)
{
if (a[i][j]<10)
{
printf(" %d ",a[i][j]);
}
else
printf("%d ",a[i][j]);
}
}
printf("\n");
}

}
...全文
573 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ckcz123 2012-03-24
  • 打赏
  • 举报
回复
说一句,我是用c++语言编的,稍微修改一下就可以转化成c语言的。
只要(i,j)的公式没错,就毫无压力。
ckcz123 2012-03-24
  • 打赏
  • 举报
回复
首先判断第i行j列是在第几层
设k=min(i,j,n+2-i-j)
那么,就是在第k层。
然后,求出(k,k)的值是多少(s=3*(k-1)*(n-1)-9*(k-1)*(k-2)/2)
最后就可以求出(i,j)是多少啦。。。
ckcz123 2012-03-24
  • 打赏
  • 举报
回复


#include <iostream>
using namespace std;

int min(int x,int y,int z)
{
int a=99999;
if (a>x)
a=x;
if (a>y)
a=y;
if (a>z)
a=z;
return a;
}

int main()
{
int n;
cin >> n;
int i,j,k,s;
for (i=1;i<=n;i++)
{
for (j=1;j<=n+1-i;j++)
{
k=min(i,j,n+2-i-j);
s=3*(k-1)*(n-1)-9*(k-1)*(k-2)/2;
if (i==k)
cout << s+j+1-k << " ";
if (n+2-i-j==k && i!=k && j!=k)
cout << s+n-3*(k-1)+i-k << " ";
if (j==k && i!=k)
cout << 3*k*(n-1)-9*k*(k-1)/2-i+k+1 << " ";
}
cout << endl;
}
return 0;
}



我是直接求出第i行j列的数是多少的,我会乱说么?
楼主给点分吧。。。
JackBurd 2012-03-24
  • 打赏
  • 举报
回复
这种题就是要考虑循环变量的边界问题,仔细分析(毎转一圈时每行每列每斜列的元素个数及下标变化)规律就可以写出程序来.
#include<stdio.h>

#define N 9

void main()
{
int a[N][N] = {1};

for(int i = 0, sum = 1; sum <= N; sum += 2+i, i++) //这是控制“转几圈”的循环变量
{
for(int j = i; j < N-2*i; j ++)
{
if(j == 0) continue; //防止第一次a[j-1]数组越界,以后不会有此情况了。
a[i][j] = a[i][j-1] + 1; //这是横着的那一列的依次赋值
}

for(int k = i+1; k < N-2*i; k ++)
{
a[k][N-1-k-i] = a[k-1][N-i-k] + 1; //这是斜着那一列的依次赋值
} //斜列规律是行列下标和为n-1-i

for(int n = N-2*i-2; n >= i+1; n--)
{
a[n][i] = a[n+1][i] + 1; //这是竖直向上那一列的依次赋值
}
}

for(int row = 0; row < N; row ++)
{
for(int col = 0; col < N-row; col ++)
{
printf("%d\t",a[row][col]);
}
printf("\n\n\n");
}
}


ckcz123 2012-03-24
  • 打赏
  • 举报
回复
我正在努力求a[i][j]的通项公式,
也就是直接算出第i行j列的值是多少。。

算算着。。
liman90 2012-03-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 javkburd 的回复:]
这确实就是个半螺旋矩阵(我自己的叫法),不过写起来比螺旋矩阵费力多了。

#include<stdio.h>

#define N 9

void main()
{
int a[N][N] = {1};
int sum = 1;

for(int i = 0; sum <= N; sum += 2+i, i++)
{
for(int j = i; j < N-2*i; j……
[/Quote]
你好,能不能加点注释,有些地方看不太明白。
yiqi1209 2012-03-13
  • 打赏
  • 举报
回复

#include <iostream>
#include<iomanip>
using namespace std;

void main()
{
const int MAXSIZE=30;
int i=0,j=0,n;
int a[MAXSIZE][MAXSIZE];
int count=1; //the first number of the matrix
int k=0; //the layer of the triangle matrix
cout<<"Input the n:"<<ends;
cin>>n;
while(count<=((n*n+n)/2)) //use the number of the matrix element
{
for(i=k,j=k;j<n-2*k-1;j++) //right
{
a[i][j]=count++;
}


for(i=k,j=n-2*k-1;j>k;i++,j--) //down to the left
{
a[i][j]=count++;
}

for(i=n-2*k-1,j=k;i>k;i--) //up
{
a[i][j]=count++;
}

k++; //the next layer of the matrix
}

for(int u=0;u<n;u++) //show the matrix
{
for(int v=0;v<n-u;v++)
{
cout<<setw(4)<<a[u][v];
}
cout<<endl;
}
}
dic_008 2012-03-13
  • 打赏
  • 举报
回复
http://blog.csdn.net/dic_008/article/details/7062364

写过一个,这是个3角形,那是个正方形
Clay_1925 2012-03-12
  • 打赏
  • 举报
回复
#include<iostream>
#define Max 100
using namespace std;
int main()
{
int input,i,j,k=1,n=0;
int a[Max][Max]={0};
cout<<"请输入n的值"<<endl;
cin>>input;
if(input==1)
{
cout<<1<<endl;
return 0;
}
for(n=0;n<input/2;n++)
{
for(j=n;j<input-n*2-1;j++)
{
a[n][j]=k++;
}
for(i=n,j=input-1-n*2;j>=n;j--,i++)
{
a[i][j]=k++;
}
for(i=input-1-1-n*2;i>n;i--)
{
a[i][n]=k++;
}
}
for(i=0;i<input;i++)
{
for(j=0;j<input-i;j++)
{
printf("%4d",a[i][j]);
}
cout<<endl;
}
return 0;
}
JackBurd 2012-03-12
  • 打赏
  • 举报
回复
这确实就是个半螺旋矩阵(我自己的叫法),不过写起来比螺旋矩阵费力多了。

#include<stdio.h>

#define N 9

void main()
{
int a[N][N] = {1};
int sum = 1;

for(int i = 0; sum <= N; sum += 2+i, i++)
{
for(int j = i; j < N-2*i; j ++)
{
if(j == 0) continue;
a[i][j] = a[i][j-1] + 1;
}

for(int k = i+1; k < N-2*i; k ++)
{
a[k][N-1-k-i] = a[k-1][N-i-k] + 1;
}

for(int n = N-2*i-2; n >= i+1; n--)
{
a[n][i] = a[n+1][i] + 1;
}
}

for(int row = 0; row < N; row ++)
{
for(int col = 0; col < N-row; col ++)
{
printf("%d\t",a[row][col]);
}
printf("\n\n\n");
}
}


aliqing777 2012-03-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 goldbeef 的回复:]

感觉和输出螺旋矩阵类似,只需要控制三个方向就可以了
C/C++ code
#include <iostream>
#define MAXSIZE 30
using namespace std;


void f(int n)
{
int Matrix[MAXSIZE][MAXSIZE];
int max_value=n*(n+1)/2;
int row=1,colu……
[/Quote]

我表示,受益匪浅
goldbeef 2012-03-11
  • 打赏
  • 举报
回复
感觉和输出螺旋矩阵类似,只需要控制三个方向就可以了
#include <iostream>
#define MAXSIZE 30
using namespace std;

void f(int n)
{
int Matrix[MAXSIZE][MAXSIZE];
int max_value=n*(n+1)/2;
int row=1,colum=1;
int temp_value=1;
int edge=n;
int i,j;
while (edge>0)
{
//right
for (i=0;i<edge;i++)
{
Matrix[row][colum]=temp_value;
colum++;
temp_value++;
}
//change to 对角线
row++;
colum-=2;

//对角线
for (i=0;i<edge-1;i++)
{
Matrix[row][colum]=temp_value;
temp_value++;
row++;
colum--;
}
//change to up
row-=2;
colum++;

for (i=0;i<edge-2;i++)
{
Matrix[row][colum]=temp_value;
row--;
temp_value++;
}
//change to new right
row++;
colum++;
if (temp_value>max_value)
{
break;
}
edge-=3;

}

//show matrix
for (i=1;i<=n;i++)
{
for (j=1;j<=n+1-i;j++)
{
cout<<Matrix[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;

}

int main()
{
int n;
cin>>n;
while (n>0)
{
f(n);
cin>>n;
}
}
laoyang103 2012-03-11
  • 打赏
  • 举报
回复
呵呵 全国软件大赛 省级选拔赛的题目吧 怎么兄弟在备战软件大赛?

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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