• <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>

            麒麟子

            ~~

            導航

            <2008年12月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            統計

            常用鏈接

            留言簿(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 麒麟子 閱讀(147) 評論(0)  編輯 收藏 引用

            无码八A片人妻少妇久久| 欧美大香线蕉线伊人久久| 国产成人精品久久二区二区| 国产综合久久久久| 久久久久久久亚洲精品| 国内精品人妻无码久久久影院导航| 青青草原综合久久大伊人| 午夜不卡久久精品无码免费| 99久久精品国产一区二区| 午夜肉伦伦影院久久精品免费看国产一区二区三区| 91麻豆国产精品91久久久| 无码人妻少妇久久中文字幕蜜桃| 欧美一区二区久久精品| 久久亚洲欧美国产精品| 亚洲国产婷婷香蕉久久久久久| 亚洲精品乱码久久久久久| 久久精品国产亚洲av瑜伽| 久久夜色精品国产噜噜麻豆| 日韩AV毛片精品久久久| 成人妇女免费播放久久久| 久久精品国产久精国产果冻传媒| 青青草国产精品久久久久| 人妻精品久久久久中文字幕一冢本| 久久精品国产欧美日韩| 久久综合九色综合久99| 国产91色综合久久免费| 久久精品国产亚洲av麻豆色欲 | 亚洲香蕉网久久综合影视 | 国内精品伊人久久久久777| 久久99精品久久久久久秒播| 久久99国产精品二区不卡| 久久亚洲精品成人av无码网站| 久久午夜综合久久| 性做久久久久久久久久久| 久久天天躁狠狠躁夜夜不卡| 久久精品亚洲福利| 一级a性色生活片久久无| 久久久无码精品亚洲日韩蜜臀浪潮| 久久久噜噜噜久久中文字幕色伊伊| 国产999精品久久久久久| 久久久久亚洲AV综合波多野结衣|