旋转吧巨人

来源:互联网 发布:重生之数据人生txt 编辑:程序博客网 时间:2024/04/28 17:26
// Spin.cpp : Defines the entry point for the console application.#include "stdafx.h"#include "iostream"#include "stdio.h"#define M 6#define K 10//internal -> outtervoid spin(int n){int **a = new int *[10];//direction flagint flag = 1;//the initial numberint number =1;int i,j;//initializationfor(int m = 0; m < K; m++){        a[m] = new int[K] ;    }for(i = 0;i < K;i++){for(j = 0;j < K;j++){a[i][j] = 0;} }//judge odd or evenif(K & 1){    //oddi = K/2;j = K/2;}else{    //eveni = K/2 -1;j = K/2 -1;}while(true){    //judge the border    if(number > n){     break;}//fill the blank if the space hasn't been filled!if(a[i][j] == 0){     a[i][j] = number++;}//from left to rightif(flag ==1 ){if(a[i+1][j] == 0){   //fill the bottom     i++; flag = 2;}else{     //or fill the rightj++;}}//from upward to bottomif(flag == 2){if(a[i][j-1] == 0){    //fill the left    j--;flag = 3;}else{       //or fill the bottom    i++; }}//from right to leftif(flag == 3){if(a[i-1][j] == 0){  //fill the upward     i--; flag = 4;}else{          //or fill the left j--; }}//from bottom to upwardif(flag == 4){if(a[i][j+1] ==0 ){  //fill the right     j++; flag =1;}else{     //or fill the upward     i--; }}}for(i =0;i< K;i++){for(j = 0; j< K;j++){if(a[i][j]!=0){printf("%d",a[i][j]);}else{   printf("X");}}printf("\n");}}int _tmain(int argc, _TCHAR* argv[]){spin(4);printf("please input a random number:\n");//while(scanf_s("%d",&x)!=EOF){//}return 0;}

0 0
原创粉丝点击