題目51:寫一個函數找出一個整數數組中,第二大的數(microsoft)
const int MINNUMBER = -32767 ;
int find_sec_max( int data[] , int count)
{
int maxnumber = data[0] ;
int sec_max = MINNUMBER ;
for ( int i = 1 ; i < count ; i++)
{
if ( data[i] > maxnumber )
{
sec_max = maxnumber ;
maxnumber = data[i] ;
}
else
{
if ( data[i] > sec_max )
sec_max = data[i] ;
}
}
return sec_max ;
}
題目52:輸入一個字符串,將其逆序後輸出
#include <iostream>
using namespace std;
void main()
{
char a[50];memset(a,0,sizeof(a));
int i=0,j;
char t;
cin.getline(a,50,'\n');
for(i=0,j=strlen(a)-1;i<strlen(a)/2;i++,j--)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
cout<<a<<endl;
}
第二種:string str;
cin>>str;
str.replace;
cout<<str;
題目53:下列表達式正確的是:
B:(x+y)++
C:c+++c+++c++
D:++(a-b--)
題目54:編程實現:找出兩個字符串中最大公共子字符串,如"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;
}
}
}
}
題目55:test.c文件中包括如下語句,文件中定義的四個變量,哪個變量不是指針類型?
#define INT_PTR int*
typedef int*int_ptr;
INT_PTR a,b;
int_ptr c,d;
A:a B:b C:c D:d E:都是指針 F:都不是指針題目56:針對以下代碼,判斷下列說法哪個是正確的()
const char str1[] = "abc";
const char str2[] = "abc";
const char *p1 = "abc";
const char *p2 = "abc";
A:str1和str2地址不同,P1和P2地址相同B:str1和str2地址相同,P1和P2地址相同
C:str1和str2地址不同,P1和P2地址不同
D:str1和str2地址相同,P1和P2地址不同
E:4個地址都相同
解析:選A。str1和str2是棧空間中的兩個字符數組,地址不同
題目57:下列 C 代碼中,不屬於未定義行爲的有___
B:char *p=”hello”;p[1]=’E’;
C:char *p=”hello”;char ch=*p++;
D:int i=0;printf(“%d%d\n”,i++,i--);
E:都是未定義行爲
F:都不是未定義行爲
題目58:程序出錯在什麼階段__?
int main(void) {
http://www.taobao.com
cout << "welcome to taobao" << endl;
}
A:預處理階段出錯B:編譯階段出錯
C:彙編階段出錯
D:鏈接階段出錯
E:運行階段出錯
F:程序運行正常
解析:F。雙斜槓之後的www.csdn.net被當做註釋了,那麼前面的http:是否合法?這就是C++中一個幾乎不會被用到的語法,標籤。帶標籤的語句是一種特殊的語句,在語句前面有一個標識符(即標籤,上段代碼中的http)和一個冒號。使用goto label就可以跳到標籤處執行,比如可以在代碼中寫goto http,這樣就會執行cout語句了。
題目59:給出以下定義,下列哪些操作是合法的?
char a[] = "hello";
char b[] = "world";
const char *p1 = a;
char* const p2 = b;
A:p1++B:p1[2]='w';
C:p2[2]='l';
解析:A,C。p1是指向常量的指針,所指向的內容不能變,所以改變p1指向的值是不對的。
題目60: 字符串比較的KMP算法
void Next(char str[], int len)
{
next[0]=-1;
for(int i=1;i<len;i++)
{
if(next[i-1]==-1&&str[i]==str[0])
next[i]=0;
else if(next[i-1]>=0&&str[next[i-1]+1]==str[i])
next[i]=next[i-1]+1;
else
next[i]=-1;
}
}
int KMP(char source[],int lenS,char pattern[],int lenP)
{
int source_index=0;
int pattern_index=0;
Next(pattern,lenP);
while(source_index<lenS && pattern_index<lenP)
{
if(source[source_index]==pattern[pattern_index])
{
++source_index;
++pattern_index;
}
else
{
if(pattern_index==0)
source_index++;
else
pattern_index = next[pattern_index-1]+1;
}
}
if(pattern_index == lenP)
return source_index-lenP;
else
return -1;
}
明天努力更新!