1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
| <?xml version="1.0" encoding="UTF-8"?> <fps version="1.2" url="https://github.com/zhblue/freeproblemset/"> <generator name="HUSTOJ" url="https://github.com/zhblue/hustoj/"/> <item>
<title><![CDATA[方阵填数]]></title>
<time_limit unit="s"><![CDATA[1]]></time_limit>
<memory_limit unit="mb"><![CDATA[125]]></memory_limit>
<description><![CDATA[<p> 在一个N*N的方阵中,填入1,2,……N*N个数,并要求构成如下的格式: </p> <p> 例如: </p> <p> N=5 </p> <p> <span style="font-family:SimSun;">13 14 15 16 1</span> </p> <p> <span style="font-family:SimSun;">12 23 24 17 2</span> </p> <p> <span style="font-family:SimSun;">11 22 25 18 3</span> </p> <p> <span style="font-family:SimSun;">10 21 20 19 4</span> </p> <p> <span style="font-family:SimSun;"> 9 8 7 6 5</span> </p> <p> <br /> </p> <p> N=6 </p> <p> <span style="font-family:SimSun;">16 17 18 19 20 1</span> </p> <p> <span style="font-family:SimSun;">15 30 31 32 21 2</span> </p> <p> <span style="font-family:SimSun;">14 29 36 33 22 3</span> </p> <p> <span style="font-family:SimSun;">13 28 35 34 23 4</span> </p> <p> <span style="font-family:SimSun;">12 27 26 25 24 5</span> </p> <p> <span style="font-family:SimSun;">11 10 9 8 7 6</span> </p>]]></description>
<input><![CDATA[每个测试文件只包含一组测试数据,每组输入一个N。]]></input>
<output><![CDATA[输出构成的方阵。]]></output>
<sample_input><![CDATA[5 ]]></sample_input> <sample_output><![CDATA[13 14 15 16 1 12 23 24 17 2 11 22 25 18 3 10 21 20 19 4 9 8 7 6 5 ]]></sample_output> <test_input><![CDATA[10 ]]></test_input> <test_output><![CDATA[28 29 30 31 32 33 34 35 36 1 27 58 59 60 61 62 63 64 37 2 26 57 80 81 82 83 84 65 38 3 25 56 79 94 95 96 85 66 39 4 24 55 78 93 100 97 86 67 40 5 23 54 77 92 99 98 87 68 41 6 22 53 76 91 90 89 88 69 42 7 21 52 75 74 73 72 71 70 43 8 20 51 50 49 48 47 46 45 44 9 19 18 17 16 15 14 13 12 11 10 ]]></test_output> <test_input><![CDATA[1 ]]></test_input> <test_output><![CDATA[ 1 ]]></test_output> <test_input><![CDATA[3 ]]></test_input> <test_output><![CDATA[ 7 8 1 6 9 2 5 4 3 ]]></test_output>
<hint><![CDATA[<p> 6 </p> <p> ------------------------- </p> 16 17 18 19 20 1<br /> 15 30 31 32 21 2<br /> 14 29 36 33 22 3<br /> 13 28 35 34 23 4<br /> 12 27 26 25 24 5<br /> 11 10 9 8 7 6<br />]]></hint>
<source><![CDATA[NOIP全国联赛普及组 1995年NOIP全国联赛普及组]]></source>
<solution language="C"><![CDATA[#include <stdio.h>
int main (void) { int i, n, k, ix, iy;
scanf("%d", &n); int A[n][n];
k = 0; for(i = 0; i <= n / 2; i++) { for(ix = i, iy = n - i - 1; ix < n - i - 1; ix++) { k++; A[ix][iy] = k; } for(ix = n - i - 1, iy = n - i - 1; iy > i; iy--) { k++; A[ix][iy] = k; } for(ix = n - i - 1, iy = i; ix > i; ix--) { k++; A[ix][iy] = k; } for(ix = i, iy = i; iy < n - i - 1; iy++) { k++; A[ix][iy] = k; } } if(n % 2 == 1) A[n / 2][n / 2] = ++k; for(ix = 0; ix < n; ix++) { for(iy = 0; iy < n; iy++) { if(iy > 0) printf(" "); if(A[ix][iy] < 10&&(iy == 0||A[ix][iy - 1] < 10)) printf(" "); printf("%d", A[ix][iy]); } printf("\n"); }
return 0; } ]]></solution> <solution language="C++"><![CDATA[#include <iostream> #include <string> #include <cstdio> using namespace std; int n; int map[200][200]={0}; void fill_map(int t,int l,int s) { for(int i=l+1;i<=s;i++) map[i][s]=t++; for(int i=s-1;i>=l+1;i--) map[s][i]=t++; for(int i=s-1;i>=l+1;i--) map[i][l+1]=t++; for(int i=l+2;i<=s-1;i++) map[l+1][i]=t++; if(s>=0) fill_map(t,l+1,s-1); } void print_map1() { for(int i=1;i<=n;i++) { for(int j=1;j<n;j++) printf("%2d ",map[i][j]); printf("%2d\n",map[i][n]); } } void print_map2() { for(int i=1;i<=n;i++) { for(int j=1;j<n;j++) printf("%d ",map[i][j]); printf("%d\n",map[i][n]); } } int main() { cin>>n; fill_map(1,0,n); if(n!=10) print_map1(); else print_map2(); return 0; } ]]></solution> <solution language="Python"><![CDATA[# coding=utf-8 #!/usr/bin/python3
def solve(N): matrix = [[0 for i in range(0, N)] for i in range(0, N)] num = 1 N_2 = int((N+1)/2) for i in range(0, N_2): x, y = i, N - i - 1 matrix[x][y] = num while x < y: matrix[x][y] = num num += 1 x += 1 while y > i: matrix[x][y] = num num += 1 y -= 1 while x > i: matrix[x][y] = num num += 1 x -= 1 while y < N - i -1: matrix[x][y] = num num += 1 y += 1 return matrix
def print_ans(matrix): n = len(matrix) for i in range(0, n): for j in range(0, n): if j == 0 and n <= 3: print('{:2d}'.format(matrix[i][j]), sep='', end='') elif j == 0: print('{:d}'.format(matrix[i][j]), sep='', end='') elif n <= 3: print(' {:2d}'.format(matrix[i][j]), sep='', end='') else: print(' {:d}'.format(matrix[i][j]), sep='', end='') print('')
def main(): s = input() N = int(s) print_ans(solve(N))
if __name__ == '__main__': main()
]]></solution> </item> </fps>
|