華為C語言面試題
寫一個程序, 要求功能:求出用1,2,5這三個數不同個數組合的和為100的組合個數。
如:100個1是一個組合,5個1加19個5是一個組合。。。。 請用C++語言寫。
答案:最容易想到的算法是:
設x是1的個數,y是2的個數,z是5的個數,number是組合數
注意到0<=x<=100,0<=y<=50,0<=z=20,所以可以編程為:
number=0; for (x=0; x<=100; x++) for (y=0; y<=50; y++) for (z=0; z<=20; z++) if ((x+2*y+5*z)==100) number++; cout<<number<<endl;
上面這個程序一共要循環100*50*20次,效率實在是太低了
事實上,這個題目是一道明顯的數學問題,而不是單純的編程問題。我的解法如下:
因為x+2y+5z=100
所以x+2y=100-5z,且z<=20 x<=100 y<=50
所以(x+2y)<=100,且(x+5z)是偶數
對z作循環,求x的可能值如下:
z=0, x=100, 98, 96, ... 0
z=1, x=95, 93, ..., 1
z=2, x=90, 88, ..., 0
z=3, x=85, 83, ..., 1
z=4, x=80, 78, ..., 0
......
z=19, x=5, 3, 1
z=20, x=0
因此,組合總數為100以內的偶數+95以內的奇數+90以內的偶數+...+5以內的奇數+1,
即為:
(51+48)+(46+43)+(41+38)+(36+33)+(31+28)+(26+23)+(21+18)+(16+13)+(11+8)+(6+3)+1
某個偶數m以內的偶數個數(包括0)可以表示為m/2+1=(m+2)/2
某個奇數m以內的奇數個數也可以表示為(m+2)/2
所以,求總的組合次數可以編程為:
number=0; for (int m=0;m<=100;m+=5) { number+=(m+2)/2; } cout<<number<<endl;
這再一次證明了:計算機程序=數據結構+算法,而且算法是程序的靈魂,對任何工程問題,當用軟件來實現時,必須選取滿足當前的資源限制,用戶需求限制,開發時間限制等種種限制條件下的最優算法。而絕不能一拿到手,就立刻用最容易想到的算法編出一個程序了事——這不是一個專業的研發人員的行為。
那么,那種最容易想到的算法就完全沒有用嗎?不,這種算法正好可以用來驗證新算法的正確性,在調試階段,這非常有用。在很多大公司,例如微軟,都采用了這種方法:在調試階段,對一些重要的需要好的算法來實現的程序,而這種好的算法又比較復雜時,同時用容易想到的算法來驗證這段程序,如果兩種算法得出的結果不一致(而最容易想到的算法保證是正確的),那么說明優化的算法出了問題,需要修改。可以舉例表示為:
#ifdef DEBUG int simple(); #end if int optimize(); ...... in a function: { result=optimize(); ASSERT(result==simple()); }
一個學生的信息是:姓名,學號,性別,年齡等信息,用一個鏈表,把這些學生信息連在一起, 給出一個age, 在些鏈表中刪除學生年齡等于age的學生信息。
#include "stdio.h" #include "conio.h" struct stu{ char name[20]; char sex; int no; int age; struct stu * next; }*linklist; struct stu *creatlist(int n) { int i; //h為頭結點,p為前一結點,s為當前結點 struct stu *h,*p,*s; h = (struct stu *)malloc(sizeof(struct stu)); h->next = NULL; p=h; for(i=0;i<n;i++) { s = (struct stu *)malloc(sizeof(struct stu)); p->next = s; printf("Please input the information of the student: name sex no age \n"); scanf("%s %c %d %d",s->name,&s->sex,&s->no,&s->age); s->next = NULL; p = s; } printf("Create successful!"); return(h); } void deletelist(struct stu *s,int a) { struct stu *p; while(s->age!=a) { p = s; s = s->next; } if(s==NULL) printf("The record is not exist."); else { p->next = s->next; printf("Delete successful!"); } } void display(struct stu *s) { s = s->next; while(s!=NULL) { printf("%s %c %d %d\n",s->name,s->sex,s->no,s->age); s = s->next; } } int main() { struct stu *s; int n,age; printf("Please input the length of seqlist:\n"); scanf("%d",&n); s = creatlist(n); display(s); printf("Please input the age:\n"); scanf("%d",&age); deletelist(s,age); display(s); return 0; }
實現一個函數,把一個字符串中的字符從小寫轉為大寫。
#include "stdio.h" #include "conio.h" void uppers(char *s,char *us) { for(;*s!='\0';s++,us++) { if(*s>='a'&&*s<='z') *us = *s-32; else *us = *s; } *us = '\0'; } int main() { char *s,*us; char ss[20]; printf("Please input a string:\n"); scanf("%s",ss); s = ss; uppers(s,us); printf("The result is:\n%s\n",us); getch(); }