小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

筆試試題

 lhzstudio 2012-05-15

最近,接到了XXX公司的面試!有幾道比較基本的算法試題如下

1、將一個單向列表逆序

2、統(tǒng)計在一個字符串中,字符出現(xiàn)次數(shù)最多的一個字符和次數(shù)

3、將一個數(shù)值轉(zhuǎn)換為任意進制的格式數(shù),并且以字符串的格式保存

下面是我自己寫的,留著以后再用


  1. typedef struct node  
  2. {  
  3.     int data;  
  4.     node* pnext;  
  5.   
  6. }*lpNode, Node;  
  7.   
  8. /* 
  9.     函數(shù)介紹:將單向列表逆序 
  10.     參數(shù)介紹: 
  11.              -pHeader[in]:頭節(jié)點指針 
  12.     返回值: 
  13.            逆序后的頭節(jié)點指針     
  14. */  
  15. lpNode Reverse(lpNode pHeader)  
  16. {  
  17.     lpNode pT,pT1,pT2;  
  18.     pT1 = pHeader;  
  19.     pT2 = pHeader->pnext;  
  20.     while(pT2)  
  21.     {  
  22.         pT = pT2->pnext;  
  23.         pT2->pnext = pT1;  
  24.         pT1 = pT2;  
  25.         pT2 = pT;  
  26.     }  
  27.   
  28.     pHeader ->pnext = NULL;  
  29.   
  30.     pHeader = pT1;  
  31.   
  32.     return pHeader;  
  33. }  
  34.   
  35. void PrintfNode(lpNode pHeader)  
  36. {  
  37.     lpNode pT = pHeader;  
  38.     while(pT)  
  39.     {  
  40.         std::cout << pT->data << std::endl;  
  41.         pT = pT->pnext;  
  42.     }  
  43. }  
  44.   
  45. /* 
  46.     函數(shù)介紹:統(tǒng)計連續(xù)出現(xiàn)字符次數(shù)最多的字符和次數(shù)("aaabbbbbcccc" b出現(xiàn)5次) 
  47.     參數(shù)介紹: 
  48.              -str[in]:待統(tǒng)計的字符串指針 
  49.              -pcount[in,out]:統(tǒng)計次數(shù)的指針 
  50.     返回值: 
  51.            出現(xiàn)字符最多的字符     
  52. */  
  53. char CountChar(char *str,int* pcount)  
  54. {  
  55.     int iLen = strlen(str);  
  56.     *pcount = 0;  
  57.     char cOut = ' ';  
  58.   
  59.     for(int i = 0; i < iLen; i++)  
  60.     {  
  61.         int iCount = 1;  
  62.         for(int j = i; j < iLen; j++)  
  63.         {  
  64.             if(str[j] == str[j + 1])  
  65.                 iCount++;  
  66.             else  
  67.                 break;  
  68.         }  
  69.   
  70.         if(iCount > *pcount)  
  71.         {  
  72.             *pcount = iCount;  
  73.             cOut = str[i];  
  74.         }  
  75.     }  
  76.   
  77.     return cOut;  
  78. }  
  79.   
  80. /* 
  81.     函數(shù)介紹:將數(shù)值轉(zhuǎn)換為任意進制的格式字符輸出 
  82.     參數(shù)介紹: 
  83.              -iValue[in]:待轉(zhuǎn)換的數(shù)值 
  84.              -szBuf[in,out]:保存轉(zhuǎn)后的字符 
  85.              -uiDecimal[in]:要換行的進制 
  86.     返回值: 
  87.            無     
  88. */  
  89. void FormatValueToString(int iValue,char *szBuf,unsigned int uiDecimal)  
  90. {  
  91.     if(szBuf == NULL) return;  
  92.   
  93.     int i = 0;  
  94.     while(iValue)  
  95.     {  
  96.         //sprintf(szBuf + i++,"%d",iValue%uiDecimal);   
  97.         szBuf[i++] = iValue%uiDecimal + 48;//'0'的值為48   
  98.         iValue /=uiDecimal;  
  99.     }  
  100.   
  101.     //調(diào)整高低位的位置   
  102.     int iLen = strlen(szBuf);  
  103.     for(i = 0; i < iLen/2;i++)  
  104.     {  
  105.         char ctemp = szBuf[i];  
  106.         szBuf[i] = szBuf[iLen -1 - i ];  
  107.         szBuf[iLen -1 - i] = ctemp;  
  108.     }  
  109. }  
  110.   
  111. int _tmain(int argc, _TCHAR* argv[])  
  112. {  
  113.     Node node_9 ={9,NULL};  
  114.     Node node_8 ={8,&node_9};  
  115.     Node node_7 ={7,&node_8};  
  116.     Node node_6 ={6,&node_7};  
  117.     Node node_5 ={5,&node_6};  
  118.     Node node_4 ={4,&node_5};  
  119.     Node node_3 ={3,&node_4};  
  120.     Node node_2 ={2,&node_3};  
  121.     Node node_1 ={1,&node_2};  
  122.     Node node_0 ={0,&node_1};  
  123.   
  124.     std::cout << "****************before******************"<<std::endl;  
  125.     PrintfNode(&node_0);  
  126.     std::cout << "****************after*******************"<<std::endl;  
  127.     PrintfNode(Reverse(&node_0));  
  128.   
  129.     std::cout << "****************CountChar***************"<<std::endl;  
  130.     int iCount = 0;;  
  131.     std::cout <<CountChar("abcdefghijklmnopqrstuvwxyz",&iCount)<< ":";  
  132.     std::cout<< iCount << std::endl;  
  133.   
  134.     std::cout << "************FormatValueToString*********"<<std::endl;  
  135.     char buff[32] ={0};  
  136.     FormatValueToString(65535,buff,8);  
  137.     std::cout << buff <<std::endl;  
  138.   
  139.     return 0;  
  140. }  

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多