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

            為生存而奔跑

               :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            積分與排名

            • 積分 - 328893
            • 排名 - 74

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            百度一個(gè)筆試題
            #include<iostream>
            using namespace std;

            struct complex_t
            {
                
            int real;
                
            int imag;
            }
            ;

            int create(complex_t *p, unsigned int n)
            {
                p 
            = new complex_t[n];
                
            if(p == NULL)
                    
            return -1;
                
            else return 0;
            }


            int main()
            {
                complex_t 
            *comps = NULL;
                unsigned 
            int num = 0;
                cin
            >>num;
                
            if(create(comps,num) < 0)
                
            {
                    printf(
            "create failed\n");
                    
            return -1;
                }


                
            /*if(comps == NULL)
                {
                    cout<<"comps is NULL\n";
                    return -1;
                }
            */


                
            //comps = new complex_t[num];

                
            long long int sum = 0;
                unsigned 
            int pos = 0;
                cin
            >>pos;
                
            while(pos<num)
                
            {
                    cin
            >>comps[pos].real>>comps[pos].imag;
                    cin
            >>comps[pos+1].real>>comps[pos+1].imag;
                    sum 
            += comps[pos].real*comps[pos+1].real + comps[pos].imag*comps[pos+1].imag;
                    pos
            +=2;
                }

                cout
            <<"sum is "<<sum<<endl;
                
            return 0;
            }

            很容易被忽悠的一個(gè)地方是,create函數(shù)的第一個(gè)參數(shù),類(lèi)型時(shí)complex_t * p。 然后,在create里面給p分配了一塊存儲(chǔ)空間。 乍一看,因?yàn)槭侵羔樧鰠?shù),所以會(huì)傳遞回去。其實(shí)不然。在這里,相當(dāng)于,首先,main函數(shù)中調(diào)用create函數(shù)時(shí),把comps賦值給p。即指針p指向與comps相同的一段存儲(chǔ)空間。  但是,在create里面,p=new complex_t[n],使得p又指向了一塊新的存儲(chǔ)空間。而此時(shí),comps還是指向原來(lái)的存儲(chǔ)空間。所以,在create里面對(duì)p做的更改對(duì)comps并沒(méi)有影響。

            一個(gè)解決方法是使用指向指針的指針。如下
            #include<iostream>
            using namespace std;

            struct complex_t
            {
                
            int real;
                
            int imag;
            }
            ;

            int create(complex_t **p, unsigned int n)
            {
                
            *= new complex_t[n];
                
            if(p == NULL)
                    
            return -1;
                
            else return 0;
            }


            int main()
            {
                complex_t 
            *comps = NULL;
                unsigned 
            int num = 0;
                cin
            >>num;
                
            if(create(&comps,num) < 0)
                
            {
                    printf(
            "create failed\n");
                    
            return -1;
                }


                
            /*if(comps == NULL)
                {
                    cout<<"comps is NULL\n";
                    return -1;
                }
            */


                
            //comps = new complex_t[num];

                
            long long int sum = 0;
                unsigned 
            int pos = 0;
                cin
            >>pos;
                
            while(pos<num)
                
            {
                    cin
            >>comps[pos].real>>comps[pos].imag;
                    cin
            >>comps[pos+1].real>>comps[pos+1].imag;
                    sum 
            += comps[pos].real*comps[pos+1].real + comps[pos].imag*comps[pos+1].imag;
                    pos
            +=2;
                }

                cout
            <<"sum is "<<sum<<endl;
                
            return 0;
            }

            另外一種方法是,在main函數(shù)中申請(qǐng)空間,而不是在create函數(shù)中。
            看下面的例子
            bool isDigit(char ch)
            {
                
            return ch>='0'&&ch<='9';
            }


            int maxContinueNum(const char * inputstr, char * outputstr)
            {
                
            int i,j;
                
            int maxlen=0;
                
            int start;
                i
            =0;
                
            while(inputstr[i]!='\0')
                
            {
                    
            if(isDigit(inputstr[i]))
                    
            {
                        j
            =i+1;
                        
            while(inputstr[j]!='\0' && isDigit(inputstr[j]))
                            j
            ++;
                        
            if(j-i>maxlen)
                        
            {
                            maxlen
            =j-i;
                            start
            =i;
                        }

                        i
            =j;
                    }

                    
            else i++;
                }

                
            for(i=0;i<maxlen;i++)
                    outputstr[i]
            =inputstr[i+start];
                outputstr[i]
            ='\0';
                
            return maxlen;
            }

            int main()
            {
                
            char input[]={"abcd12345ed125ss123456789"};
                
            char *output = new char[100];
                cout
            << maxContinueNum(input, output) << endl;
                
                cout
            <<output<<endl;
            }

            outputstr在main函數(shù)中申請(qǐng)內(nèi)存,在maxContinueNum函數(shù)中更改其中的值。

            posted on 2011-05-04 23:43 baby-fly 閱讀(655) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): Effective STL / C++
            久久精品卫校国产小美女| 亚洲精品97久久中文字幕无码| 久久亚洲精品中文字幕| 99精品国产99久久久久久97| 亚洲色大成网站www久久九| 久久国产精品国产自线拍免费| 国产成人精品综合久久久| 人妻无码精品久久亚瑟影视| 久久99国产综合精品女同| 日本精品久久久久久久久免费| 亚洲国产另类久久久精品 | 久久久婷婷五月亚洲97号色| 国产一区二区精品久久岳| 久久婷婷国产综合精品| 亚洲国产小视频精品久久久三级 | 久久精品国产亚洲av水果派| 四虎影视久久久免费观看| 久久亚洲精品国产精品| 久久综合成人网| 99久久国产热无码精品免费久久久久| 久久精品国产99国产精品亚洲| 久久国产成人| 久久国产精品-久久精品| 色综合久久久久久久久五月| 综合久久一区二区三区| 久久久久综合中文字幕| 国产日韩久久免费影院| 色成年激情久久综合| 99久久婷婷国产综合亚洲| 精品久久久久香蕉网| 欧美亚洲色综久久精品国产| 欧美熟妇另类久久久久久不卡| 久久丫忘忧草产品| 久久无码AV中文出轨人妻| 91麻豆国产精品91久久久| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 色妞色综合久久夜夜| 久久久久久亚洲精品影院| 久久国内免费视频| 久久婷婷激情综合色综合俺也去| 久久国产色AV免费观看|