Example 2.2 倒蛇阵填数

来源:互联网 发布:怎样应对网络安全问题 编辑:程序博客网 时间:2024/06/08 07:21


</pre><pre name="code" class="cpp">
/* * main.cpp * *  Created on: 2014年9月13日 *      Author: xiangxiyun * *  Description: 倒蛇阵填数 * *  Example: when N = 4 *  Output:  10 11 12 1 *            9 16 13 2 *            8 15 14 3 *            7  6  5 4 */#include <iostream>using namespace std;int main() {int n = 0, i = 0, r = 1;cin >> n;int number[n][n];//initalizefor(int i = 0; i < n; i++) {for(int j = 0; j < n; j++) {number[i][j] = 0;}}while(i < n - 1 -i) {//from upright to downrightfor(int j = i; j < n-i; j++) {number[j][n-i-1] = r;r++;}//from downright to downleftfor(int j = n-i-2; j >=i; j--) {number[n-i-1][j] = r;r++;}//from downleft to upleftfor(int j =  n-i-2; j>=i; j--){number[j][i] = r;r++;}//from upleft to uprightfor(int j = i+1; j <= n-i-2; j++) {number[i][j] = r;r++;}i++;}//terminateif(i == n-i-1) {number[i][i] = n*n;}//outputfor(int i = 0; i < n; i++) {for(int j = 0; j < n; j++) {cout << number[i][j] << ' ';}cout << '\n';}return 0;}

0 0