怎样求这样一个矩阵?
假如有一个nxm 的矩阵,其值分布如下, 哪位能用C or C++ 求出呀, 谢谢帮忙啦.1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9 写一下思路
定义4个m = 4,m=0,i=0,j=0作如下循环
while(j<m) j++;
while(i<m) i++;
while(j>n) j--;
whlie(i>n) i--;
k--;
n++;
[ 本帖最后由 Jor 于 2007-7-16 21:18 编辑 ] Actaully , i can't get you, couldyou explain clearly, maybe my brain doesn't work smartly now. thank you . int a;
int m = 4,m=0,i=0,j=0,b=1;
while(m!=n) {
while(j<m) {a [ i] =b;j++;b++;}
while(i<m) {a [ i] =b;i++;b++;}
while(j>n) {a [ i] =b;j--;b++;}
whlie(i>n) {a [ i] =b;i--;b++}
m--;
n++;
}
[ 本帖最后由 Jor 于 2007-7-16 21:33 编辑 ] complete code for c++:
#include <iostream.h>
int main(int argc,char* argv[])
{
int a;
int m_b,n_b,m_t,n_t,i,j,v;
v=1;i=0;j=0;
m_b=0;n_b=0;
m_t=4;n_t=4;
while((m_b<=m_t) && (n_b<=n_t))
{
while(j<=m_t) {a=v; v++; j++;}
j--;v--;
while(i<=n_t) {a=v; v++; i++;}
i--;v--;
while(j>=m_b) {a=v; v++; j--;}
j++;v--;
while(i>=m_b+1) {a=v; v++; i--;}
i++;v--;
m_b++;m_t--;n_b++;n_t--;
}
for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
cout<<a<<" ";
cout<<endl;
}
return 0;
} 再改良成工整一点的:
#include <iostream.h>
int main(int argc,char* argv[])
{
int a;
int m_b,n_b,m_t,n_t,i,j,v;
v=1;i=0;j=0;
m_b=0;n_b=0;
m_t=4;n_t=4;
while((m_b<=m_t) && (n_b<=n_t))
{
while(j<=m_t) {a=v; v++; j++;}
j--;v--;
while(i<=n_t) {a=v; v++; i++;}
i--;v--;
while(j>=m_b) {a=v; v++; j--;}
j++;v--;
while(i>=m_b+1) {a=v; v++; i--;}
i++;v--;
m_b++;m_t--;n_b++;n_t--;
}
for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
cout<<a<<"\t";
cout<<endl;
}
return 0;
} 不好意思,上面那个算法在某些情况下会有点问题,而且这里的文本编辑有BUG,不能正常显示.我另外发一个帖子,把源代码贴出来. 瓦。。楼上的写的那么完整阿。。偶也来写个 顺便骗骗帖子~~~
#include "stdio.h"
void main() {
int a;
int m=4,n=0,b=1;
int s=0,t=0;
int i=0,j=0;
while((m>=2) && (n<=2)) {
while(j<=m) {a[ i]=b;j++;b++;}
i++;j--;
while(i<=m) {a[ i]=b;i++;b++;}
j--;i--;
while(j>=n) {a[ i]=b;j--;b++;}
i--;j++;
n++;
while(i>=n) {a[ i]=b;i--;b++;}
m--;
j++;i++;
}
for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
printf("%d ",a[ i]);
printf("\n");
}
} #include <stdio.h>
main()
{
int array,t=0,m=0,r=0,c=0,s1=1,s2=4,s3=0,s4=4,i=1;
while(s1!=4)
{
while(m<=s4)
{
array=i;
m++;
i++;
}
s4--;
m--;
i--;
while(t<=s2)
{
array=i;
t++;
i++;
}
s2--;
t--;
i--;
while(m>=s3)
{
array=i;
m--;
i++;
}
s3++;
m++;
while(t>s1)
{
t--;
array=i;
i++;
}
t=s1;
m=s1;
s1++;
}
for(r=0;r<5;r++)
{
for(c=0;c<5;c++)
printf("%3d",array);
printf("\n");
}
}
sdlkfj1 收到, 谢谢个位的回复
页:
[1]