題目如下:
21 22 23 24 25 26
20 7 8 9 10 27
19 6 1 2 11 28
18 5 4 3 12 29
17 16 15 14 13 30
如圖:設“1”的坐標為(0,0) “7”的坐標為(-1,-1) 編寫一個小程序,使程序做到輸入坐標(X,Y)之后顯示出相應的數字。我的程序,沒有怎么調整,很粗糙,不過,實現就行了:#include <iostream>
using namespace std;
/*
設“1”的坐標為(0,0) “7”的坐標為(-1,-1) 編寫一個小程序,
使程序做到輸入坐標(X,Y)之后顯示出相應的數字。
*/
/************************************************************************
**算法:數字是圍繞1“盤旋”, 移動的步進值是1,1,2,2,3,3,4,4,5,5,6,6……
**對于一個數,我們可以算出他移動的步數,然后和一個沒有走完的偏移,如果恰好走完就是
**偏移為0。
**然后我們對于輸入的值,我們模擬走過的路徑來求值,步數表示已經走過而偏移表示要繼續
**走偏移數目的步數
*************************************************************************/
enum X {RIGHT = 1, DOWM = 1,LEFT = -1, UP = -1};
/*
*get_attribution()函數取得輸入值移動過幾次用times表示,
*以及比每次移動的終點要多走的步數,用dif表示
*/
void get_attribution(int in_number, int &dif, int ×) {
int i = 0;
in_number--;
while (in_number >= 0) {
in_number = in_number - (i/2+1);
times = i;
i++;
if (in_number >= 0) {
dif = in_number;
}
}
}
int main()
{
int a = 21; //輸入一個數值,這里就不人機交互輸入了
int dif = 0, times = 0; // 起始偏移距離和次數
get_attribution(a, dif, times);
cout << "偏移" << dif << "中間走了" << times << "次" << endl;
int x = 0, y = 0; //起始端點
for (int i = 1; i <= times; i++) {
switch (i%4) { //已經走過了一些步數
case 0: //上移到下一個端點
y += UP *((i-1)/2+1); break;
case 1: //右移到下一個端點
x += RIGHT * ((i-1)/2+1); break;
case 2: //下移到下一個端點
y += DOWM * ((i-1)/2+1); break;
case 3: //左移到下一個端點
x += LEFT * ((i-1)/2+1); break;
}
}
switch (times%4) { //繼續完成要偏移的值
case 3: //接下來的操作是上移,x不變,y減小
y += UP * dif; break;
case 0: //接下來的操作是右移
x += RIGHT * dif; break;
case 1: //接下來的操作是下移
y += DOWM * dif; break;
case 2: //接下來的操作是左移
x += LEFT * dif; break;
}
cout << "(" << x << ", " << y << ")" << endl;
return 0;
}
作者給出了自己的程序,太長我就引用了,也給出了人家的程序 ,挺不錯,如下:#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int newVal(int x, int y)
{
//以結點1為原點
//以相鄰兩結點間的距離為單位(如結點2與結點3的之間線段)
//結點7所在的正方形(由結點2、3、4、5、6、7、8、9構成)的邊長
//的一半為1,即結點7到原點1的最大投影距離為1。
//于是由結點坐標,可以求出此結點所在的正方形的投影距離:
int r = max(abs(x),abs(y));
//進行坐標變換,即把坐標原點移動到正方形的一個角結點上,
//使整個正方形落在第一象限,例如,當r=1時,將把坐標原點從結點1
//移動到結點7。
x += r;
y += r;
//正方形的邊長,等于投影距離的兩倍
int d = 2*r;
int s; //s為結點在自己的正方形的偏移量
if (y == 0)
s = 3*d + x;
else if (x == 0)
s = 2*d + (d-y);
else if (y == d)
s = d + (d-x);
else
s = y;
//pow((r+1),2)為內層的結點數。
//例如,結點10的內層由結點1和正方形A(2、3、4、5、7、8、10)構成
//這些內層的總結點數恰為:(正方形A的邊長+1)的平方,
//因為:正方形A的邊長 =(結點10所在正方形的半徑-1)*2
//故:內層結點數 = (結點10所在正方形的邊長-1)的平方
//結點值 = 在當前正方形的偏移量 + 內層的結點數
s += pow((d-1),2);
return s;
}
int main(int argc,char * argv[])
{
int x, y;
cout <<"請輸入坐標(x y):";
while (cin>>x>>y)
{
cout <<"坐標所在的結點值為:"<<f(x, y)<<endl;
cout <<"請輸入坐標(x y):";
}
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------
這是我寫的,算法請看二樓
#include<stdio.h>
int GetX(int x)//求(X,0)
{
int result=1,i=0;
if (x==0) return result;
else if (x>0)
{
for(i=0;i<x;i++)
{
result = result + 1+8*i;//通項公試. a(n) = a(n-1) + a(0) + d*(n-1)
}
return result;
}
else if(x<0)
{
for(i=0;i<-x;i++)
{
result = result + 5+8*i;
}
return result;
}
}
int GetY(int y)//求(0,Y)
{
int result=1,i=0;
if (y==0) return result;
else if (y>0)
{
for(i=0;i<y;i++)
{
result = result + 7+8*i;
}
return result;
}
else if(y<0)
{
for(i=0;i<-y;i++)
{
result = result + 3+8*i;
}
return result;
}
}
int GetNum(int x,int y)//求(X,Y)對應的值
{
if(abs(x)<=abs(y))
{
if(y<=0)
return GetY(y)+x;
else return GetY(y)-x;
}
else
{
if(x<=0)
return GetX(x)-y;
else return GetX(x)+y;
}
}
void main()
{
int x,y;
while(1)
{
printf("please input (X,Y)\n");
scanf("%d,%d",&x,&y);
printf("The result is:%d\n",GetNum(x,y));
}
getch();
}