為自己鼓掌高一作文
1 寫出程序把一個鏈表中的接點順序倒排

typedef struct linknode
{
int data;
struct linknode *next;
}node;
/pic/p>
node *reverse(node *head)
{
node *p,*q,*r;
p=head;
q=p->next;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=NULL;
head=p;
return head;
}
2 寫出程序刪除鏈表中的所有接點
void del_all(node *head)
{
node *p;
while(head!=NULL)
{
p=head->next;
free(head);
head=p;
}
cout<<”釋放空間成功!”< }
3兩個字符串,s,t;把t字符串插入到s字符串中,s字符串有足夠的空間存放t字符串
void insert(char *s, char *t, int i)
{
char *q = t;
char *p =s;
if(q == NULL)return;
while(*p!=’\0′)
{
p++;
}
while(*q!=0)
{
*p=*q;
p++;
q++;
}
*p = ‘\0′;
}
分析下面的代碼:
char *a = “hello”;
char *b = “hello”;
if(a= =b)
printf(“YES”);
else
printf(“NO”);
這個簡單的面試題目,我選輸出 no(對比的應該是指針地址吧),可在VC是YES 在C是NO
lz的呢,是一個常量字符串。位于靜態存儲區,它在程序生命期內恒定不變。如果編譯器優化的話,會有可能a和b同時指向同一個hello的。則地址相同。如果編譯器沒有優化,那么就是兩個不同的地址,則不同
寫一個函數,功能:完成內存之間的拷貝
memcpy source code:
270 void* memcpy( void *dst, const void *src, unsigned int len )
271 {
272 register char *d;
273 register char *s;
27
275 if (len == 0)
276 return dst;
277
278 if (is_overlap(dst, src, len, len))
279 complain3(“memcpy”, dst, src, len);
280
281 if ( dst > src ) {
282 d = (char *)dst + len – 1;
283 s = (char *)src + len – 1;
284 while ( len >= 4 ) {
285 *d– = *s–;
286 *d– = *s–;
287 *d– = *s–;
288 *d– = *s–;
289 len -= 4;
290 }
291 while ( len– ) {
292 *d– = *s–;
293 }
294 } else if ( dst < src ) {
295 d = (char *)dst;
296 s = (char *)src;
297 while ( len >= 4 ) {
298 *d++ = *s++;
299 *d++ = *s++;
300 *d++ = *s++;
301 *d++ = *s++;
302 len -= 4;
303 }
304 while ( len– ) {
305 *d++ = *s++;
306 }
307 }
308 return dst;
309 }
公司考試這種題目主要考你編寫的代碼是否考慮到各種情況,是否安全(不會溢出)
各種情況包括:
1、參數是指針,檢查指針是否有效
2、檢查復制的源目標和目的地是否為同一個,若為同一個,則直接跳出
3、讀寫權限檢查
4、安全檢查,是否會溢出
memcpy拷貝一塊內存,內存的大小你告訴它
strcpy是字符串拷貝,遇到’\0′結束
/* memcpy ─── 拷貝不重疊的內存塊 */
void memcpy(void* pvTo, void* pvFrom, size_t size)
{
void* pbTo = (byte*)pvTo;
void* pbFrom = (byte*)pvFrom;
ASSERT(pvTo != NULL && pvFrom != NULL); /pic/p>
ASSERT(pbTo>=pbFrom+size || pbFrom>=pbTo+size);/pic/p>
while(size–>0)
*pbTo++ == *pbFrom++;
return(pvTo);
}
華為面試題:怎么判斷鏈表中是否有環?
bool CircleInList(Link* pHead)
{
if(pHead = = NULL || pHead->next = = NULL)/pic/p>
return (false);
if(pHead->next = = pHead)/pic/p>
return (true);
Link *pTemp1 = pHead;/pic/p>
Link *pTemp = pHead->next;/pic/p>
while(pTemp != pTemp1 && pTemp != NULL && pTemp->next != NULL)
{
pTemp1 = pTemp1->next;
pTemp = pTemp->next->next;
}
if(pTemp = = pTemp1)
return (true);
return (false);
}
兩個字符串,s,t;把t字符串插入到s字符串中,s字符串有足夠的空間存放t字符串
void insert(char *s, char *t, int i)
{
memcpy(&s[strlen(t)+i],&s[i],strlen(s)-i);
memcpy(&s[i],t,strlen(t));
s[strlen(s)+strlen(t)]=’\0′;
}
1。編寫一個 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 return i;
}
一個單向鏈表,不知道頭節點,一個指針指向其中的一個節點,問如何刪除這個指針指向的節點?
將這個指針指向的next節點值copy到本節點,將next指向next->next,并隨后刪除原next指向的節點。
#include
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”
*/
/pic/p>
#include
#include
#include
char *commanstring(char shortstring[], char longstring[])
{
int i, j;
char *substring=malloc(256);
if(strstr(longstring, shortstring)!=NULL) /pic/p>
return shortstring;
for(i=strlen(shortstring)-1;i>0; i–) /pic/p>
{
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)) /pic/p>
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
#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;
/pic/p>
}
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) /pic/p>
return;
if (n == 1) { /pic/p>
matrix[x][y] = start;
return;
}
for (i = x; i < x + n-1; i++) /pic/p>
matrix[y][i] = start++;
for (j = y; j < y + n-1; j++) /pic/p>
matrix[j][x+n-1] = start++;
for (i = x+n-1; i > x; i–) /pic/p>
matrix[y+n-1][i] = start++;
for (j = y+n-1; j > y; j–) /pic/p>
matrix[j][x] = start++;
SetMatrix(x+1, y+1, start, n-2); /pic/p>
}
void main() {
int i, j;
int n;
scanf(“%d”, &n);
SetMatrix(0, 0, 1, n);
/pic/p>
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 ) /pic/p>
{
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++) /pic/p>
{
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 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
#include
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
#include
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:/pic/1]
throw “Invalid argument(s)”; /pic/p>
char * strDestCopy=strDest; file:/pic/3]
while ((*strDest++=*strSrc++)!=’\0′); file:/pic/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′。
【為自己鼓掌高一作文】相關文章:
為自己鼓掌07-04
為自己的鼓掌作文06-02
(精選)為自己鼓掌作文09-12
為自己鼓掌作文04-26
為自己鼓掌作文[必備]09-14
為自己鼓掌作文[精品]09-14
為自己鼓掌作文【精品】11-16
我為自己鼓掌作文12-14
為自己鼓掌學生作文02-27
我為自己鼓掌作文05-09
- 相關推薦