• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            麒麟子

            ~~

            導航

            <2009年4月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            統計

            常用鏈接

            留言簿(12)

            隨筆分類

            隨筆檔案

            Friends

            WebSites

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            [導入]網易程序筆試題

            題目如下:

            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();

            }


            文章來源:http://ly-weiwei.blog.163.com/blog/static/72975283200811281154738

            posted on 2008-12-28 01:15 麒麟子 閱讀(144) 評論(0)  編輯 收藏 引用

            91精品国产91久久久久福利| 少妇久久久久久被弄到高潮| 久久99热只有频精品8| 精品久久久久久无码中文字幕一区| 久久久无码精品亚洲日韩按摩 | 久久久久亚洲av无码专区导航| 精品熟女少妇av免费久久| 伊人久久大香线蕉影院95| 久久精品久久久久观看99水蜜桃| 国产精品岛国久久久久| 性做久久久久久久久| 欧美激情精品久久久久| 欧美久久久久久| 久久国产精品一区| 久久精品无码专区免费青青| 久久涩综合| 久久播电影网| 久久亚洲精品中文字幕三区| 久久无码专区国产精品发布 | 久久99热精品| 无码久久精品国产亚洲Av影片 | 无码人妻久久一区二区三区免费 | 色天使久久综合网天天| 久久不射电影网| 久久国产欧美日韩精品| 奇米综合四色77777久久| 久久午夜无码鲁丝片秋霞| 热久久国产欧美一区二区精品| 一级做a爰片久久毛片16| 久久人人爽爽爽人久久久| 国产aⅴ激情无码久久| 亚洲精品午夜国产VA久久成人| 久久精品国产亚洲Aⅴ香蕉| 精品久久久久久无码中文野结衣| 伊人久久精品线影院| 久久精品国产半推半就| 久久青草国产手机看片福利盒子| 精品久久久久久久| 久久久WWW成人| 中文精品久久久久人妻| 精品一二三区久久aaa片|