日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

面试题08(C++)

發布時間:2024/4/17 c/c++ 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 面试题08(C++) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
編寫一個 C 函數,該函數在一個字符串中找到可能的最長的子字符串,且該字符串是由同一字符組成的。
char * search(char *cpSource, char ch)
{
??????????? char *cpTemp=NULL, *cpDest=NULL;
??????????? int iTemp, iCount=0;
??????????? while(*cpSource)
??????????? {
??????????????????? if(*cpSource == ch)
??????????????????? {
???????????????????????????? iTemp = 0;
???????????????????????????? cpTemp = cpSource;
???????????????????????????? while(*cpSource == ch)
++iTemp, ++cpSource;
???????????????????????????? if(iTemp > iCount)
iCount = iTemp, cpDest = cpTemp;
?????????? if(!*cpSource)
break;
??????????????????? }
??????????????????? ++cpSource;
}
return cpDest;
}??????
2。請編寫一個 C 函數,該函數在給定的內存區域搜索給定的字符,并返回該字符所在位置索引值。
int search(char *cpSource, int n, char ch)
{
??????????? int i;
??????????? for(i=0; i<n && *(cpSource+i) != ch; ++i);
??????????? return i;
}

一個單向鏈表,不知道頭節點,一個指針指向其中的一個節點,問如何刪除這個指針指向的節點?
將這個指針指向的next節點值copy到本節點,將next指向next->next,并隨后刪除原next指向的節點。


#include <stdio.h>
void foo(int m, int n)
{
?????? printf("m=%d, n=%d\n", m, n);
}

int main()
{
?????? int b = 3;
?????? foo(b+=3, ++b);
?????? printf("b=%d\n", b);
return 0;
}
輸出:m=7,n=4,b=7(VC6.0)
這種方式和編譯器中得函數調用關系相關即先后入棧順序。不過不同
編譯器得處理不同。也是因為C標準中對這種方式說明為未定義,所以
各個編譯器廠商都有自己得理解,所以最后產生得結果完全不同。
因為這樣,所以遇見這種函數,我們首先要考慮我們得編譯器會如何處理
這樣得函數,其次看函數得調用方式,不同得調用方式,可能產生不同得
結果。最后是看編譯器優化。


2.寫一函數,實現刪除字符串str1中含有的字符串str2.
第二個就是利用一個KMP匹配算法找到str2然后刪除(用鏈表實現的話,便捷于數組)


/*雅虎筆試題(字符串操作)
給定字符串A和B,輸出A和B中的最大公共子串。
比如A="aocdfe" B="pmcdfa" 則輸出"cdf"
*/
//Author: azhen
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char *commanstring(char shortstring[], char longstring[])
{
int i, j;

char *substring=malloc(256);

if(strstr(longstring, shortstring)!=NULL)???????????????? //如果……,那么返回shortstring
return shortstring;??

for(i=strlen(shortstring)-1;i>0; i--)??????????????????? //否則,開始循環計算
{
for(j=0; j<=strlen(shortstring)-i; j++){
memcpy(substring, &shortstring[j], i);
substring[i]='\0';
if(strstr(longstring, substring)!=NULL)
return substring;
}
}
return NULL;
}


main()
{
char *str1=malloc(256);
char *str2=malloc(256);
char *comman=NULL;

gets(str1);
gets(str2);

if(strlen(str1)>strlen(str2))??????????????????????????? //將短的字符串放前面
comman=commanstring(str2, str1);
else
comman=commanstring(str1, str2);

printf("the longest comman string is: %s\n", comman);
}


11.寫一個函數比較兩個字符串str1和str2的大小,若相等返回0,若str1大于
str2返回1,若str1小于str2返回-1
int strcmp ( const char * src,const char * dst)
{
?????????? int ret = 0 ;
?????????? while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
{
?????????????????? ++src;
++dst;
}
?????????? if ( ret < 0 )
?????????????????? ret = -1 ;
?????????? else if ( ret > 0 )
?????????????????? ret = 1 ;
?????????? return( ret );
}

3,求1000!的未尾有幾個0(用素數相乘的方法來做,如72=2*2*2*3*3);
求出1->1000里,能被5整除的數的個數n1,能被25整除的數的個數n2,能被125整除的數的個數n3,
能被625整除的數的個數n4.
1000!末尾的零的個數=n1+n2+n3+n4;
#include<stdio.h>
#define NUM 1000

int find5(int num){
int ret=0;
while(num%5==0){
num/=5;
ret++;
}
return ret;
}
int main(){
int result=0;
int i;
for(i=5;i<=NUM;i+=5)
{
result+=find5(i);
}
printf(" the total zero number is %d\n",result);
return 0;
}


1. 有雙向循環鏈表結點定義為:
struct node
{ int data;
struct node *front,*next;
};
有兩個雙向循環鏈表A,B,知道其頭指針為:pHeadA,pHeadB,請寫一函數將兩鏈表中data值相同的結點刪除
BOOL DeteleNode(Node *pHeader, DataType Value)
{
if (pHeader == NULL) return;

BOOL bRet = FALSE;
Node *pNode = pHead;
while (pNode != NULL)
{
if (pNode->data == Value)
{
if (pNode->front == NULL)
{
pHeader = pNode->next;
pHeader->front = NULL;
}
else
{
if (pNode->next != NULL)
{
pNode->next->front = pNode->front;
}
pNode->front->next = pNode->next;
}

Node *pNextNode = pNode->next;
delete pNode;
pNode = pNextNode;

bRet = TRUE;
//不要break或return, 刪除所有
}
else
{
pNode = pNode->next;
}
}

return bRet;
}

void DE(Node *pHeadA, Node *pHeadB)
{
if (pHeadA == NULL || pHeadB == NULL)
{
return;
}

Node *pNode = pHeadA;
while (pNode != NULL)
{
if (DeteleNode(pHeadB, pNode->data))
{
if (pNode->front == NULL)
{
pHeadA = pNode->next;
pHeadA->front = NULL;
}
else
{
pNode->front->next = pNode->next;
if (pNode->next != NULL)
{
pNode->next->front = pNode->front;
}
}
Node *pNextNode = pNode->next;
delete pNode;
pNode = pNextNode;
}
else
{
pNode = pNode->next;
}
}
}
2. 編程實現:找出兩個字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串為"cad"
int GetCommon(char *s1, char *s2, char **r1, char **r2)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int maxlen = 0;

for(int i = 0; i < len1; i++)
{
for(int j = 0; j < len2; j++)
{
if(s1[i] == s2[j])
{
int as = i, bs = j, count = 1;
while(as + 1 < len1 && bs + 1 < len2 && s1[++as] == s2[++bs])
count++;

if(count > maxlen)
{
maxlen = count;
*r1 = s1 + i;
*r2 = s2 + j;
}
}
}
}
3. 編程實現:把十進制數(long型)分別以二進制和十六進制形式輸出,不能使用printf系列庫函數
char* test3(long num) {
char* buffer = (char*)malloc(11);
buffer[0] = '0';
buffer[1] = 'x';
buffer[10] = '\0';

char* temp = buffer + 2;
for (int i=0; i < 8; i++) {
temp[i] = (char)(num<<4*i>>28);
temp[i] = temp[i] >= 0 ? temp[i] : temp[i] + 16;
temp[i] = temp[i] < 10 ? temp[i] + 48 : temp[i] + 55;
}
return buffer;
}


輸入N, 打印 N*N 矩陣
比如 N = 3,打印:

1???? 2???? 3
8???? 9???? 4
7???? 6???? 5

N = 4,打印:

1????? 2????? 3????? 4
12???? 13???? 14???? 5
11???? 16???? 15???? 6
10???? 9????? 8????? 7
解答:
1 #define N 15
int s[N][N];
void main()
{
int k = 0, i = 0, j = 0;
int a = 1;
for( ; k < (N+1)/2; k++ )
{
while( j < N-k ) s[i][j++] = a++; i++; j--;
while( i < N-k ) s[i++][j] = a++; i--; j--;
while( j > k-1 ) s[i][j--] = a++; i--; j++;
while( i > k )????? s[i--][j] = a++; i++; j++;
}
for( i = 0; i < N; i++ )
{
for( j = 0; j < N; j++ )
cout << s[i][j] << '\t';
cout << endl;
}
}
2 define MAX_N???? 100
int matrix[MAX_N][MAX_N];

/*
*(x,y):第一個元素的坐標
* start:第一個元素的值
* n:矩陣的大小
*/
void SetMatrix(int x, int y, int start, int n) {
?????? int i, j;

?????? if (n <= 0)?????? //遞歸結束條件
?????????? return;
?????? if (n == 1) {???? //矩陣大小為1時
?????????? matrix[x][y] = start;
?????????? return;
?????? }
?????? for (i = x; i < x + n-1; i++)????? //矩陣上部
?????????? matrix[y][i] = start++;

?????? for (j = y; j < y + n-1; j++)????? //右部
?????????? matrix[j][x+n-1] = start++;

?????? for (i = x+n-1; i > x; i--)??????? //底部
?????????? matrix[y+n-1][i] = start++;

?????? for (j = y+n-1; j > y; j--)??????? //左部
?????????? matrix[j][x] = start++;

?????? SetMatrix(x+1, y+1, start, n-2);????? //遞歸
}

void main() {
????? int i, j;
????? int n;

????? scanf("%d", &n);
????? SetMatrix(0, 0, 1, n);
???
????? //打印螺旋矩陣
????? for(i = 0; i < n; i++) {
???????? for (j = 0; j < n; j++)
printf("%4d", matrix[i][j]);
???????? printf("\n");
????? }
}


斐波拉契數列遞歸實現的方法如下:
int???? Funct( int n )
{
????? if(n==0) return 1;
????? if(n==1) return 1;
????? retrurn???? Funct(n-1) + Funct(n-2);
}
請問,如何不使用遞歸,來實現上述函數?
請教各位高手!
解答:int???? Funct( int n )???? //???? n 為非負整數
{
????? int a=0;
????? int b=1;
????? int c;
????? if(n==0) c=1;
????? else if(n==1) c=1;
????? else for(int i=2;i<=n;i++)???? //應該n從2開始算起
????? {
??????? c=a+b;
??????? a=b;
??????? b=c;
????? }
????? return c;
}
解答:
現在大多數系統都是將低字位放在前面,而結構體中位域的申明一般是先聲明高位。
100???? 的二進制是 001 100 100
低位在前????? 高位在后??
001----s3
100----s2
100----s1
所以結果應該是 1
如果先申明的在低位則:
001----s1
100----s2
100----s3
結果是 4
1、原題跟little-endian,big-endian沒有關系
2、原題跟位域的存儲空間分配有關,到底是從低字節分配還是從高字節分配,從Dev C++和VC7.1上看,都是從低字節開始分配,并且連續分配,中間不空,不像譚的書那樣會留空位
3、原題跟編譯器有關,編譯器在未用堆棧空間的默認值分配上有所不同,Dev C++未用空間分配為
01110111b,VC7.1下為11001100b,所以在Dev C++下的結果為5,在VC7.1下為1。

注:PC一般采用little-endian,即高高低低,但在網絡傳輸上,一般采用big-endian,即高低低高,華為是做網絡的,所以可能考慮big-endian模式,這樣輸出結果可能為4

判斷一個字符串是不是回文
int IsReverseStr(char *aStr)
{
int i,j;
int found=1;
if(aStr==NULL)
return -1;
j=strlen(aStr);
for(i=0;i<j/2;i++)
if(*(aStr+i)!=*(aStr+j-i-1))
{
found=0;
break;
}
return found;
}

Josephu 問題為:設編號為1,2,… n的n個人圍坐一圈,約定編號為k(1<=k<=n)的人從1開始報數,數到m 的那個人出列,它的下一位又從1開始報數,數到m的那個人又出列,依次類推,直到所有人出列為止,由此產生一個出隊編號的序列。

數組實現:
#include <stdio.h>
#include <malloc.h>
int Josephu(int n, int m)
{
???? int flag, i, j = 0;
???? int *arr = (int *)malloc(n * sizeof(int));
???? for (i = 0; i < n; ++i)
?????? arr[i] = 1;
???? for (i = 1; i < n; ++i)
???? {
?????? flag = 0;
?????? while (flag < m)
?????? {
???????? if (j == n)
?????????? j = 0;
???????? if (arr[j])
?????????? ++flag;
???????? ++j;
?????? }
?????? arr[j - 1] = 0;
?????? printf("第%4d個出局的人是:%4d號\n", i, j);
???? }
???? free(arr);
???? return j;
}
int main()
{
???? int n, m;
???? scanf("%d%d", &n, &m);
???? printf("最后勝利的是%d號!\n", Josephu(n, m));
???? system("pause");
???? return 0;
}
鏈表實現:
#include <stdio.h>
#include <malloc.h>
typedef struct Node
{
???? int index;
???? struct Node *next;
}JosephuNode;
int Josephu(int n, int m)
{
???? int i, j;
???? JosephuNode *head, *tail;
???? head = tail = (JosephuNode *)malloc(sizeof(JosephuNode));
???? for (i = 1; i < n; ++i)
???? {
?????? tail->index = i;
?????? tail->next = (JosephuNode *)malloc(sizeof(JosephuNode));
?????? tail = tail->next;
???? }
???? tail->index = i;
???? tail->next = head;
??
???? for (i = 1; tail != head; ++i)
???? {
?????? for (j = 1; j < m; ++j)
?????? {
???????? tail = head;
???????? head = head->next;
?????? }
?????? tail->next = head->next;
?????? printf("第%4d個出局的人是:%4d號\n", i, head->index);
?????? free(head);
?????? head = tail->next;
???? }
???? i = head->index;
???? free(head);
???? return i;
}
int main()
{
???? int n, m;
???? scanf("%d%d", &n, &m);
???? printf("最后勝利的是%d號!\n", Josephu(n, m));
???? system("pause");
???? return 0;
}

已知strcpy函數的原型是:
?????????? char * strcpy(char * strDest,const char * strSrc);
?????? 1.不調用庫函數,實現strcpy函數。
?????? 2.解釋為什么要返回char *。
?????? 解說:
?????? 1.strcpy的實現代碼
?????????? char * strcpy(char * strDest,const char * strSrc)
?????????? {
?????????????????? if ((strDest==NULL)||(strSrc==NULL))
file://[/1]
?????????????????????????? throw "Invalid argument(s)"; //[2]
?????????????????? char * strDestCopy=strDest;??
file://[/3]
?????????????????? while ((*strDest++=*strSrc++)!='\0');
file://[/4]
?????????????????? return strDestCopy;
?????????? }
?????? 錯誤的做法:
?????? [1]
?????? (A)不檢查指針的有效性,說明答題者不注重代碼的健壯性。
?????? (B)檢查指針的有效性時使用((!strDest)||(!strSrc))或(!(strDest&&strSrc)),說明答題者對C語言中類型的隱式轉換沒有深刻認識。在本例中char *轉換為bool即是類型隱式轉換,這種功能雖然靈活,但更多的是導致出錯概率增大和維護成本升高。所以C++專門增加了bool、true、false三個關鍵字以提供更安全的條件表達式。
?????? (C)檢查指針的有效性時使用((strDest==0)||(strSrc==0)),說明答題者不知道使用常量的好處。直接使用字面常量(如本例中的0)會減少程序的可維護性。0雖然簡單,但程序中可能出現很多處對指針的檢查,萬一出現筆誤,編譯器不能發現,生成的程序內含邏輯錯誤,很難排除。而使用NULL代替0,如果出現拼寫錯誤,編譯器就會檢查出來。
?????? [2]
?????? (A)return new string("Invalid argument(s)");,說明答題者根本不知道返回值的用途,并且他對內存泄漏也沒有警惕心。從函數中返回函數體內分配的內存是十分危險的做法,他把釋放內存的義務拋給不知情的調用者,絕大多數情況下,調用者不會釋放內存,這導致內存泄漏。
?????? (B)return 0;,說明答題者沒有掌握異常機制。調用者有可能忘記檢查返回值,調用者還可能無法檢查返回值(見后面的鏈式表達式)。妄想讓返回值肩負返回正確值和異常值的雙重功能,其結果往往是兩種功能都失效。應該以拋出異常來代替返回值,這樣可以減輕調用者的負擔、使錯誤不會被忽略、增強程序的可維護性。
?????? [3]
?????? (A)忘記保存原始的strDest值,說明答題者邏輯思維不嚴密。
?????? [4]
?????? (A)循環寫成while (*strDest++=*strSrc++);,同[1](B)。
?????? (B)循環寫成while (*strSrc!='\0') *strDest++=*strSrc++;,說明答題者對邊界條件的檢查不力。循環體結束后,strDest字符串的末尾沒有正確地加上'\0'。多謝樓主???? 辛苦了

正方形4個頂點;
每邊的中點,共4個;
兩條對角線的交點,1個。
總共9個點。請用一筆將9個點連起來?

轉載于:https://www.cnblogs.com/happylife/archive/2009/09/26/1574598.html

總結

以上是生活随笔為你收集整理的面试题08(C++)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。