|
8#
楼主 |
发表于 2015-11-8 15:20:29
|
只看该作者
本帖最后由 hr567 于 2015-11-10 10:50 编辑
我考试时写的代码原样:
- #include <fstream>
- using namespace std;
- ifstream cin("magic.in");
- ofstream cout("magic.out");
- int n, i, j, k, x, y;
- int a[29][29];
- int main()
- {
- cin >> n;
- y = 0;
- x = (n-1)/2;
- do
- {
- a[y][x] = ++k;
- x = (x+1)%n;
- y = (y-1+n)%n;
- if (a[y][x] != 0)
- {
- x = (x-1+n)%n;
- y = (y+2)%n;
- }
- }
- while (k != n*n);
- for (i = 0; i < n; ++i)
- {
- cout << a[i][0];
- for (j = 1; j < n; ++j)
- cout << ' ' << a[i][j];
- cout << '\n';
- }
- return 0;
- }
复制代码 |
|