最近,接到了XXX公司的面试!有几道比较基本的算法试题如下
1、将一个单向列表逆序
2、统计在一个字符串中,字符出现次数最多的一个字符和次数
3、将一个数值转换为任意进制的格式数,并且以字符串的格式保存
下面是我自己写的,留着以后再用
- typedef struct node
- {
- int data;
- node* pnext;
-
- }*lpNode, Node;
-
-
-
-
-
-
-
-
- lpNode Reverse(lpNode pHeader)
- {
- lpNode pT,pT1,pT2;
- pT1 = pHeader;
- pT2 = pHeader->pnext;
- while(pT2)
- {
- pT = pT2->pnext;
- pT2->pnext = pT1;
- pT1 = pT2;
- pT2 = pT;
- }
-
- pHeader ->pnext = NULL;
-
- pHeader = pT1;
-
- return pHeader;
- }
-
- void PrintfNode(lpNode pHeader)
- {
- lpNode pT = pHeader;
- while(pT)
- {
- std::cout << pT->data << std::endl;
- pT = pT->pnext;
- }
- }
-
-
-
-
-
-
-
-
-
- char CountChar(char *str,int* pcount)
- {
- int iLen = strlen(str);
- *pcount = 0;
- char cOut = ' ';
-
- for(int i = 0; i < iLen; i++)
- {
- int iCount = 1;
- for(int j = i; j < iLen; j++)
- {
- if(str[j] == str[j + 1])
- iCount++;
- else
- break;
- }
-
- if(iCount > *pcount)
- {
- *pcount = iCount;
- cOut = str[i];
- }
- }
-
- return cOut;
- }
-
-
-
-
-
-
-
-
-
-
- void FormatValueToString(int iValue,char *szBuf,unsigned int uiDecimal)
- {
- if(szBuf == NULL) return;
-
- int i = 0;
- while(iValue)
- {
-
- szBuf[i++] = iValue%uiDecimal + 48;
- iValue /=uiDecimal;
- }
-
-
- int iLen = strlen(szBuf);
- for(i = 0; i < iLen/2;i++)
- {
- char ctemp = szBuf[i];
- szBuf[i] = szBuf[iLen -1 - i ];
- szBuf[iLen -1 - i] = ctemp;
- }
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- Node node_9 ={9,NULL};
- Node node_8 ={8,&node_9};
- Node node_7 ={7,&node_8};
- Node node_6 ={6,&node_7};
- Node node_5 ={5,&node_6};
- Node node_4 ={4,&node_5};
- Node node_3 ={3,&node_4};
- Node node_2 ={2,&node_3};
- Node node_1 ={1,&node_2};
- Node node_0 ={0,&node_1};
-
- std::cout << "****************before******************"<<std::endl;
- PrintfNode(&node_0);
- std::cout << "****************after*******************"<<std::endl;
- PrintfNode(Reverse(&node_0));
-
- std::cout << "****************CountChar***************"<<std::endl;
- int iCount = 0;;
- std::cout <<CountChar("abcdefghijklmnopqrstuvwxyz",&iCount)<< ":";
- std::cout<< iCount << std::endl;
-
- std::cout << "************FormatValueToString*********"<<std::endl;
- char buff[32] ={0};
- FormatValueToString(65535,buff,8);
- std::cout << buff <<std::endl;
-
- return 0;
- }
|