生活随笔
收集整理的這篇文章主要介紹了
【PAT乙级】1050 螺旋矩阵 (25 分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目地址
#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std
;
const int N
=10005;int a
[10005][105];
int s
[N
];
int k
,n
,m
;int dx
[4]={0,1,0,-1};
int dy
[4]={1,0,-1,0};
bool cmp(int x
,int y
)
{return x
>y
;
}
int main(void)
{cin
>>k
;for(int i
=0;i
<k
;i
++) cin
>>s
[i
];if(k
==0) return 0;for(int i
=1;i
<=sqrt(k
);i
++) if(k
%i
==0) n
=i
,m
=k
/i
;sort(s
,s
+k
,cmp
);memset(a
,0x3f,sizeof a
);int x
=0,y
=0,d
=0;int index
=0;while(index
<k
){a
[x
][y
]=s
[index
++];int tempx
=x
+dx
[d
];int tempy
=y
+dy
[d
];if(tempx
<0||tempx
>=m
||tempy
<0||tempy
>=n
||a
[tempx
][tempy
]!=0x3f3f3f3f) d
=(d
+1)%4; x
=x
+dx
[d
];y
=y
+dy
[d
];}for(int i
=0;i
<m
;i
++){for(int j
=0;j
<n
;j
++){cout
<<a
[i
][j
];if(j
+1!=n
) cout
<<" ";}cout
<<endl
;}return 0;
}
#include<bits/stdc++.h>
using namespace std
;
int a
[10005][105],n
,m
,k
;
int b
[10005];
int dx
[4]={0,1,0,-1};
int dy
[4]={1,0,-1,0};
bool cmp(int x
,int y
) { return x
>y
; }
int main(void)
{cin
>>k
;for(int i
=0;i
<k
;i
++) cin
>>b
[i
];sort(b
,b
+k
,cmp
);for(int i
=1;i
<=sqrt(k
);i
++) if(k
%i
==0) m
=k
/i
,n
=i
;if(m
>n
) swap(n
,m
);int d
=0,step
=0,x
=0,y
=0;while(step
!=k
){a
[x
][y
]=b
[step
];int tempx
=x
+dx
[d
],tempy
=y
+dy
[d
];if(tempx
<0||tempx
>=n
||tempy
<0||tempy
>=m
||a
[tempx
][tempy
]) d
=(d
+1)%4;tempx
=x
+dx
[d
],tempy
=y
+dy
[d
];x
=tempx
,y
=tempy
;step
++; }for(int i
=0;i
<n
;i
++){for(int j
=0;j
<m
;j
++) {cout
<<a
[i
][j
];if(j
!=m
-1) cout
<<" ";}if(i
!=n
-1)cout
<<endl
;}return 0;
}
總結
以上是生活随笔為你收集整理的【PAT乙级】1050 螺旋矩阵 (25 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。