// raoQuan.cpp : Defines the entry point for the console application.
//1 12 11 10
//2 13 16 9
//3 14 15 8
//4 5 6 7
#include "stdafx.h"
#include "iostream.h"
#include "malloc.h"
//根據x,y和size來返回這個位置的數值; x,y均從0開始計數
int orbit(int x,int y,int size);
int girth(int size);
int offsetOfBegin(int x,int y,int size);
int cycleXy(int x,int y,int size)
{
int value=0;
int level=orbit(x,y,size);
int offset=offsetOfBegin(x,y,size);
int i=-1;
while(level>0)
{
level--;
value += girth(size-level*2);
}
value += offset;
return value;
}
//根據size來返回周長
int girth(int size)
{
if(size == 1)
{
return 1;
}
return (size-1)*4;
}
//根據x,y,size確定座標在第幾層軌道
int orbit(int x,int y,int size)
{
//int levels = size/2;
//levels = size%2==0 ? levels : levels+1;
int ox=size-1-x < x ? size-1-x : x;
int oy=size-1-y < y ? size-1-y : y;
return ox < oy ? ox : oy;
}
//座標x,y相對於該層軌道起始點的偏移
int offsetOfBegin(int x,int y,int size)
{
int levels = size/2;
levels = size%2==0 ? levels-1 : levels;
int x0,y0; //軌道起始點
int offset=0;
int level=orbit(x,y,size);
x0=y0=level;
int max=girth(size-level*2);
offset=(x-x0)+(y-y0);
if(x>y)
{
offset = max - offset;
}
return offset+1;
}
void raoQuan(unsigned int size)
{
char *str;
str = NULL;
str = (char *)malloc(size*size*sizeof(char));
int len=size*size;
int i(0),index(0),nSize(size);
for(;i<len;i++)
{
str[index]=i;
index += nSize;
}
}
int main(int argc, char* argv[])
{
int size(7);
//cin >> size;
int x(0),y(0);
for(y;y<size;y++)
{
for(x=0;x<size;x++)
{
cout << cycleXy(x,y,size) << '\t';
}
cout << endl;
}
return 0;
}
打印繞圈矩陣(C語言風格的代碼)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.