雅虎2

給定字符串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系列庫函數
#include <stdio.h>
#include <stdlib.h>

void test1(long num);
char* test2(long num) ;

void main()
{
long n;
printf("輸入long型變量n:");
scanf("%ld", &n);
printf("%ld轉換爲 2進製爲:", n); test1(n);
printf("%ld轉換爲16進製爲:%s\n", n, test2(n));
// printf("%s\n", test2(16L));
}

void test1(long num)
{
long mask = 0x1<<(8*sizeof(long)-1);
int i = sizeof(long)*8;

while (i)
{
if (num & mask)
putchar('1');
else
putchar('0');
mask = mask >> 1;
i--;
}
printf("\n");
}


char* test2(long num) //10-->16 
{
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'。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章