




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、整理了網(wǎng)上機(jī)試題,近幾十個(gè)網(wǎng)頁的資料,同時(shí)本人對(duì)每道題均進(jìn)行了驗(yàn)證。并且對(duì)一些題目進(jìn)行了更好的修正。希望對(duì)后來者有所幫助。1第一題的題目大概是輸入整型數(shù)組求數(shù)組的最小數(shù)和最大數(shù)之和,例如輸入 1,2,3,4 則輸出為 5,當(dāng)輸入只有一個(gè)數(shù)的時(shí)候,則最小數(shù)和最大數(shù)都是該數(shù),例如只輸入 1,則輸出為 2;另外數(shù)組的長度不超過 50#includemain()min_num=num0;max_num=num0;num50=0;i,n;for(j=0;jn;j+)if(max_numnumj)min_num=numj;prf(請(qǐng)輸入整型數(shù)組的長度(150):);scanf(%d,&n);prf(請(qǐng)輸入
2、整型數(shù)組的元素:); for (i=0;i0&len_num20;len_num1-,len_nu m2-)sumlen_max-=(num1len_num1-1-0)+(num2len_num2-1-0);if(len_num10)sumlen_max-=num1len_num1 - 1 -0; len_num1-;if(len_num20)sumlen_max-=num1len_num2 - 1-0; len_num2-;#include#include #include main()char *num1,*num2; char *sum;/兩個(gè)長長整型數(shù)據(jù)/temp;len_num1,l
3、en_num2; / len_max,len_min;兩個(gè)長長整型數(shù)據(jù)的長度num1=(char*)malloc(sizeof(char);num2=(char*)malloc(sizeof(char); prf(輸入兩個(gè)長長整型數(shù)據(jù):); scanf(%s,num1);prf(輸入兩個(gè)長長整型數(shù)據(jù):); scanf(%s,num2);len_num1=strlen(num1); len_num2=strlen(num2);len_max=(len_num1=len_num2)? len_num1:len_num2;len_min=(len_num1=0;j-) /實(shí)現(xiàn)進(jìn)位操作for(/tem
4、p=sumj-0;if(sumj=10)sumj-1+=sumj/10; sumj%=10;for(m=0;mlen_max1;j+,m+)outsumm=sumj+0;outsumm=0;prf(輸出兩長長整型數(shù)據(jù)之和:%sn,outsum); return 0;char *outsum=(char*)malloc(sizeof(char)*len_max1); j=0;while(sumj=0)/跳出頭部 0 元素j+;#include #include #includevoid stringFilter(const char *p_str, long len, char *p_outst
5、r)array256=0;const char *tmp = p_str; for(j=0;jlen;j+)if(arraytmpj=0)*p_outstr+= tmpj; arraytmpj+;*p_outstr = 0;void main()char*str = cccddecc; len = strlen(str);char * outstr = (char *)malloc(len*sizeof(char); stringFilter(str,len,outstr);prf(%sn,outstr); free(outstr);outstr = NULL;4.通過鍵盤輸入一串小寫字母(a
6、z)組成的字符串。請(qǐng)編寫一個(gè)字符串壓縮程序,將字符串中連續(xù)出席的重復(fù)字母進(jìn)行壓縮,并輸出壓縮后的字符串。壓縮規(guī)則:3.通過鍵盤輸入一串小寫字母(az)組成的字符串。請(qǐng)編寫一個(gè)字符串過濾程序,若字符串中出現(xiàn)多個(gè)相同的字符,將非首次出現(xiàn)的字符過濾掉。比如字符串“abacacde”過濾結(jié)果為“abcde”。要求實(shí)現(xiàn)函數(shù):void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);【輸入】 pInputStr: 輸入字符串lInputLen: 輸入字符串長度【輸出】 pOutputStr: 輸出字符串,空間已經(jīng)開
7、辟好,與輸入字符串等長;#include #include #include*p_outstr+ =p_stri;count = 1;/注意其位置void stringZip(const char *p_str, long len, char *p_outstr)count=1;*p_outstr = 0;for(i=0;i1)*p_outstr+ = count +0;*p_outstr+ =p_stri;elsevoid main()char *str = cccddecc;prf(壓縮之前的字符串為:%sn,str); len = strlen(str);char * outstr =
8、(char*)malloc(len*sizeof(char); stringZip(str,len,outstr);prf(壓縮之后的字符串為:%sn,outstr); free(outstr);outstr = NULL;#include 5.通過鍵盤輸入 100 以內(nèi)正整數(shù)的加、減運(yùn)算式,請(qǐng)編寫一個(gè)程序輸出運(yùn)算結(jié)果字符串。輸入字符串的格式為:“操作數(shù) 1 運(yùn)算符 操作數(shù) 2”,“操作數(shù)”與“運(yùn)算符”之間以一個(gè)空格隔開。補(bǔ)充說明:操作數(shù)為正整數(shù),不需要考慮計(jì)算結(jié)果溢出的情況。若輸入算式格式錯(cuò)誤,輸出結(jié)果為“0”。要求實(shí)現(xiàn)函數(shù):void arithmetic(const char *pInpu
9、tStr, long lInputLen, char *pOutputStr);【輸入】 pInputStr: 輸入字符串lInputLen: 輸入字符串長度【輸出】 pOutputStr: 輸出字符串,空間已經(jīng)開辟好,與輸入字符串等長;僅壓縮連續(xù)重復(fù)出現(xiàn)的字符。比如字符串a(chǎn)bcbc由于無連續(xù)重復(fù)字符,壓縮后的字符串還是abcbc.壓縮字段的格式為字符重復(fù)的次數(shù)+字符。例如:字符串yyyyyyz壓縮后就成為3x6yz要求實(shí)現(xiàn)函數(shù):void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);【輸入】 pInput
10、Str: 輸入字符串lInputLen: 輸入字符串長度【輸出】 pOutputStr: 輸出字符串,空間已經(jīng)開辟好,與輸入字符串等長;#include#includevoid arithmetic(const char *input, long len, char *output)char s110; char s210; char s310;cnt = 0;len_input=strlen(input); for(i=0;ilen_input;+i)if(inputi= ) cnt+;if(cnt!=2)*output+ = 0;*output = 0; return;sscanf(inp
11、ut,%s %s %s,s1,s2,s3);if(strlen(s2)!=1|(s20!=+&s20!=-)*output+ = 0;*output = 0; return;len_s1=strlen(s1);for(i=0;ilen_s1;i+)if(s1i9)*output+ = 0;*output = 0; return;len_s3=strlen(s3);for(i=0;ilen_s3;i+)if(s3i9)*output+ = 0;*output = 0; return;x = atoi(s1);y = atoi(s3); if(s20=+)result = x+y; itoa(re
12、sult,output,10);else if(s20=-)result = x-y;itoa(result,output,10);else*output+ = 0;*output = 0; return;void main()char str = 10 - 23; char outstr10;len = strlen(str);arithmetic(str,len,outstr); prf(%sn,str);prf(%sn,outstr);6.一組人(n 個(gè)),圍成一圈,從開始數(shù)到第三個(gè)的人出列,再接著從下一個(gè)人開始數(shù),最終輸出最終出列的人(環(huán)是一個(gè)數(shù)學(xué)的應(yīng)用問題:已知 n 個(gè)人(以1,2,
13、3.n 分別表示)圍坐在一張圓桌周圍。從為 k的人開始報(bào)數(shù),數(shù)到 m 的那個(gè)人出列;他的下一個(gè)人又從 1 開始報(bào)數(shù),數(shù)到 m 的那個(gè)人又出列;依此規(guī)律重復(fù)下去,直到圓桌周圍的人全部出列。)#include #include #include#includetypedef struct Nodedata;struct Node *next;LinkList;LinkList *create(n)LinkList *p,*q,*head;i=1;p=(LinkList*)malloc(sizeof(LinkList); p-data=i;he;for(i=1;idata=i+1;p-next=q;
14、 p=q;p-next=head;/使鏈表尾連接鏈表頭,形成循環(huán)鏈表 return head;free(p); p=NULL;free(q);q=NULL;void deletefun(LinkList *L,LinkList *p,*q,*temp; i;p=L;m)while(p-next!=p)for(i=1;inext;prf(%5d,p-data); temp=p;q-next=p-next;p=p-next;free(temp);prf(%5dn,p-data);main()n=7,m=3;LinkList *head1; head1=create(n); deletefun(he
15、ad1,m);return 0;7.輸入一串字符,只包含“0-10”和“,”找出其中最小的數(shù)字和最大的數(shù)字(可能不止一個(gè)),輸出最后剩余數(shù)字個(gè)數(shù)。如 輸入 “3,3,4,5,6,7,7”#include #include#includevoid main()char str100;prf(輸入一組字符串:n); scanf(%s,&str);len=strlen(str);array100; count=0;for(i=0;i=0&stri=9)arraycount+=stri-0;arraycount=0; result=count; min=array0; max=array0;for(j
16、=0;jcount;j+)if(maxarrayj)min=arrayj;for(k=0;kcount;k+)if(arrayk=min) result-;if(arrayk=max) result-;prf(%dn,result);8.輸入一組身高在 170 到 190 之間(5 個(gè)身高),比較身高差,選出身高差最小的兩個(gè)身高;若身高差相同,選平均身高高的那兩個(gè)身高;從小到大輸出;如 輸入 170 181 173 186 190 輸出 170 173#include #include #define N 5main()HeightN;dmin; H1,H2;i,j,temp;prf(請(qǐng)輸入一
17、組身高在 170 到 190 之間的數(shù)據(jù)(共 5 個(gè)):n);for(k=0;kN;k+)scanf(%d,&Heightk);prf(n);for(i=0;iN;i+)for(j=1;jHeightj;j+)temp=Heightj-1;Heightj-1=Heightj; Heightj=temp;H1=Height0;H2=Height1; dmin=H2-H1;for(m=2;mN;m+)if(Heightm-Heightm-1=dmin)H1=Heightm-1; H2=Heightm;dmin=Heightm-Heightm-1;prf(身高差最小的兩個(gè)身高為:n); prf(%d
18、,%dn,H1,H2);return 0;9. 刪除子串,只要是原串中有相同的子串就刪掉,不管有多少個(gè),返回子串個(gè)數(shù)。#include #include #include #include delete_sub_str(const char *str,const char *sub_str,char *result)assert(str != NULL & sub_str != NULL); const char *p,*q;char *t,*temp; p = str;q = sub_str; t = result;n,count = 0; n = strlen(q);temp = (cha
19、r *)malloc(n+1); memset(temp,0 x00,n+1); while(*p)memcpy(temp,p,n);if(strcmp(temp,q) = 0 )count+;memset(temp,0 x00,n+1); p = p + n;else*t = *p; p+; t+;memset(temp,0 x00,n+1);free(temp); return count;void main()char s100 = 0;num = delete_sub_str(“123abc12de234fg1hi34j123k”,”123”,s);prf(“The number of
20、 sub_str is %drn”,m); prf(“The result string is %srn”,s);10. 要求編程實(shí)現(xiàn)上述高精度的十進(jìn)制加法。要求實(shí)現(xiàn)函數(shù):void add (const char *num1, const char *num2, char *result)【輸入】num1:字符串形式操作數(shù) 1,如果操作數(shù)為負(fù),則 num10為符號(hào)位- num2:字符串形式操作數(shù) 2,如果操作數(shù)為負(fù),則num20為符號(hào)位-【輸出】result:保存加法計(jì)算結(jié)果字符串,如果結(jié)果為負(fù),則result0為符號(hào)位。#include#include #includelength)/移除字
21、母前的-符號(hào)void move(char *str,if(str0 != -)return;i;for(i = 0; i 0; i-)/從最后開始移除 0,直到遇到非 0 數(shù)字,只對(duì)最初位置上的 0 不予判斷if(resulti = 0)resulti = 0; count+;elsereturn length-count;return length - count;/將字符串倒轉(zhuǎn)void reverse(char *result,char temp;length)for(i = 0; i = 0 & j = 0; i-, j-)n1 = str1i - 0;n2 = str2j - 0;cu
22、rSum = n1 + n2 + another;resultcur_rs+ = curSum % 10 + 0; another = curSum / 10;if(j = 0)/遍歷 str1 剩余各位n1 = str1i- - 0;curSum = n1 + another;resultcur_rs+ = curSum % 10 + 0; another = curSum / 10;if(another != 0)/如果還有進(jìn)位未加上resultcur_rs+ = another + 0;elsewhile(j = 0)n2 = str2j- - 0;curSum = n2 + anoth
23、er;resultcur_rs+ = curSum % 10 + 0; another = curSum / 10;if(another != 0)resultcur_rs+ = another + 0;resultcur_rs = 0;cur_rs = remove_zero(result, cur_rs);if(!flag)resultcur_rs+ = -; resultcur_rs = 0;reverse(result, strlen(result); return cur_rs;real_minus(char *str1, char *str2, char *result)/使用 s
24、tr1 減去 str2char big100, small100;big_len, sml_len;len1 = strlen(str1);len2 = strlen(str2); bool flag = false;/用于標(biāo)記 str2 是否比str1 大if(len1 len2)flag = true;else if(len1 = len2)if(strcmp(str1, str2) = 0)result0 = 0;result1 = 0; return 1;else if(strcmp(str1,str2) =0 & j=0; i-,j-)n1 = str1i - 0;n2 = str2
25、j - 0;if(n1 = n2+another)resultcur_rs+ = (n1-n2-another) +0; another = 0;elseresultcur_rs+ = (n1+10-n2-another) + 0; another = 1;while(i = 0)n1 = str1i- - 0;if(another != 0)n1 -= another; another = 0;resultcur_rs+ = n1 + 0;resultcur_rs = 0;cur_rs = remove_zero(result, cur_rs); if(flag)resultcur_rs+
26、= -; resultcur_rs = 0;reverse(result, cur_rs);return cur_rs;void addi(const char *num1, const char *num2, char *result)len1 = strlen(num1); len2 = strlen(num2);rs_len;if(!len1 | !len2)return;char str1100, str2100; strncpy(str1, num1, len1); str1len1 = 0; strncpy(str2, num2, len2);str2len2 = 0;if(str
27、10 = - & str20 = -)move(str1, len1); move(str2, len2);rs_len = real_add(str1, str2, result, false);else if(str10 = -)move(str1, len1);rs_len = real_minus(str2, str1, result);else if(str20 = -)move(str2, len2);rs_len = real_minus(str1, str2, result);elsers_len = real_add(str1, str2, result, true);/ma
28、in(main()argc, char *argv)char num1100,num2100;prf(請(qǐng)輸入兩個(gè)整型數(shù)據(jù):n); scanf(%s%s,num1,num2);char result100;memset(result, 0, 100); addi(num1,num2, result); prf(%sn, result);return 0;11. 描述:10 個(gè)學(xué)生考完期末評(píng)卷完成后,A 老師需要?jiǎng)澇黾案窬€,要求如下:(1)(2)(3)及格線是 10 的倍數(shù);保證至少有 60%的學(xué)生及格;如果所有的學(xué)生都高于 60 分,則及格線為 60 分輸入:輸入 10 個(gè)整數(shù),取值 0100輸
29、出:輸出及格線,10 的倍數(shù)#includevoid bubblesort(i,j,temp;arr)for(i=0;i10;i+)for(j=0;jarrj+1;j+)temp=arrj; arrj=arrj+1; arrj+1=temp;GetPassLine(a)bubblesort(a);if(a0=60)return 60;elsereturn ()a4/10)*10);main()a10=0;result;prf(請(qǐng)隨機(jī)輸入 10 個(gè)成績(0-100):n);scanf(%d%d%d%d%d%d%d%d%d%d,&a0,&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8
30、,&a9);prf(n);result=GetPassLine(a);prf(及格線為:%dn,result); return 1;12. 描述:一條長廊里依次裝有 n(1 n 65535)盞電燈,從頭到尾拉線開關(guān)控制。開始,電燈全部關(guān)著。1、2、3、n-1、n。每盞電燈由一個(gè)有 n 個(gè)學(xué)生從長廊穿過。第一個(gè)學(xué)生把號(hào)碼凡是 1 的倍數(shù)的電燈的開關(guān)拉一下;接著第二個(gè)學(xué)生把號(hào)碼凡是 2 的倍數(shù)的電燈的開關(guān)拉一下;接著第三個(gè)學(xué)生把號(hào)碼凡是 3 的倍數(shù)的電燈的開關(guān)拉一下;如此繼續(xù)下去,最后第 n 個(gè)學(xué)生把號(hào)碼凡是 n 的倍數(shù)的電燈的開關(guān)拉一下。n 個(gè)學(xué)生按此規(guī)定走完后,長廊里電燈有幾盞亮著。注:電燈數(shù)
31、和學(xué)生數(shù)一致。輸入:電燈的數(shù)量輸出:亮著的電燈數(shù)量樣例輸入:3樣例輸出:1#include#define Max_Bubl_Num 65535GetLightLampNum(n)BublNumMax_Bubl_Num=0;/0 表示燈滅,1 表示燈亮unsignedunsignedi,j;count=0;for(i=1;i=n;i+)for(j=i;j=n&j%i=0;j+)BublNumj-1+=1;BublNumj-1=BublNumj-1%2;for(k=0;kn;k+)if(BublNumk=1) count+;return count;main()n,result;prf(請(qǐng)輸入燈的
32、數(shù)量(1-65535):n); scanf(%d,&n);result=GetLightLampNum(n);prf(最后亮燈的數(shù)量為:%dn,result); return 0;13. 描述:已知 2 條地鐵線路,其中 A 為環(huán)線,B 為東西向線路,線路都是雙向的。經(jīng)過的站點(diǎn)名分別如下,兩條線交叉的換乘點(diǎn)用 T1、T2 表示。編寫程序,任意輸入兩個(gè)站點(diǎn)名稱,輸出乘坐地鐵最少需要經(jīng)過的車站數(shù)量(含輸入的起點(diǎn)和終點(diǎn),換乘站點(diǎn)只計(jì)算一次)。地鐵線 A(環(huán)線)經(jīng)過車站:A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A
33、18地鐵線 B(直線)經(jīng)過車站:B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15輸入:輸入兩個(gè)不同的站名輸出:輸出最少經(jīng)過的站數(shù),含輸入的起點(diǎn)和終點(diǎn),換乘站點(diǎn)只計(jì)算一次輸入樣例:A1 A3輸出樣例:3 #include #include #include#includeusing namespatd;#define MAX 35#define SUBWAY_A 20#define SUBWAY_B 15 typedef struct nodeadjvex;struct node *next;edgenode;typedef str
34、uctchar name10; bool flag;edgenode *link;vexnode;constcharsubway_name1SUBWAY_A10=A1,A2,A3,A4,A5,A6,A7,A8,A9,T1,A10,A11,A12,A13,T2,A14,A15,A16,A17,A18;constcharsubway_name2SUBWAY_B10=B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,B11,B12,B13,B14,B15;void creat(vexnode ga)i;edgenode *p;for(i=0;iMAX;i+)gai.link=NULL;
35、gai.flag=true;if(iSUBWAY_A) strcpy(,subway_name1i); else strcpy(,subway_name2i-20);/A 地鐵建鄰接表for(i=1;iadjvex=i-1;p-next=NULL; gai.link=p;p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=i+1;p-next=NULL; gai.link-next=p; if(i=9)p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=SUBWAY_A+4;p-
36、next=NULL;gai.link-next-next=p;p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=SUBWAY_A+5;p-next=NULL;gai.link-next-next-next=p;else if(i=14)p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=SUBWAY_A+9;p-next=NULL;gai.link-next-next=p;p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=SUBWAY_A+10;p-next=NUL
37、L;gai.link-next-next-next=p;p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=SUBWAY_A-1;p-next=NULL; ga0.link=p;p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=1;p-next=NULL;ga0.link-next=p;p=(edgenode*)malloc(sizeof(edgenode);p-adjvex=SUBWAY_A-2; p-next=NULL; gaSUBWAY_A-1.link=p;p=(edgenode*)malloc(si
38、zeof(edgenode); p-adjvex=0;p-next=NULL;gaSUBWAY_A-1.link-next=p;/B 地鐵建鄰接表for(i=1;iadjvex=SUBWAY_A+i-1;p-next=NULL;gai+SUBWAY_A.link=p;p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=SUBWAY_A+i+1;p-next=NULL;gai+SUBWAY_A.link-next=p;p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=SUBWAY_A+3;p-next=NULL
39、;gaSUBWAY_A+4.link=p;p=(edgenode*)malloc(sizeof(edgenode);p-adjvex=9;p-next=NULL; gaSUBWAY_A+4.link-next=p;p=(edgenode*)malloc(sizeof(edgenode);p-adjvex=9;p-next=NULL; gaSUBWAY_A+5.link=p;p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=SUBWAY_A+6;p-next=NULL;gaSUBWAY_A+5.link-next=p;p=(edgenode*)mall
40、oc(sizeof(edgenode);p-adjvex=SUBWAY_A+8; p-next=NULL; gaSUBWAY_A+9.link=p;p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=14;p-next=NULL;gaSUBWAY_A+9.link-next=p;p=(edgenode*)malloc(sizeof(edgenode);p-adjvex=14; p-next=NULL;gaSUBWAY_A+10.link=p;p=(edgenode*)malloc(sizeof(edgenode); p-adjvex=SUBWAY_A+
41、11;p-next=NULL;gaSUBWAY_A+10.link-next=p;p=(edgenode*)malloc(sizeof(edgenode);p-adjvex=SUBWAY_A+1; p-next=NULL; gaSUBWAY_A.link=p;p=(edgenode*)malloc(sizeof(edgenode);p-adjvex=SUBWAY_A+SUBWAY_B-2; p-next=NULL; gaSUBWAY_A+SUBWAY_B-1.link=p;/打印各鄰接節(jié)點(diǎn)for(i=0;i%s,); s=s-next;prf(n);main()v
42、exnode gaMAX; creat(ga);i;char str210;while(scanf(%s%s,str0,str1)!=EOF)temp=0;for(i=0;iMAX;i+)gai.flag=true;if(!strcmp(str0,) temp=i;queueq; q.push(gatemp); gatemp.flag=false;count=0; start=0; end=1;bool find_flag=false; while(!q.empty()if(find_flag) break; count+;prf(*n); prf(第%d 層搜索:,count
43、);temp_end=end; while(startadjvex.flag)q.push(gas-adjvex); gas-adjvex.flag=false; end+;/prf(%s ,);s=s-next;q.pop(); start+;prf(n);prf(%dn,count);return 0;14. 字串轉(zhuǎn)換問題描述:將輸入的字符串(字符串僅包含小寫字母a到z),按照如下規(guī)則,循環(huán)轉(zhuǎn)換后輸出:a-b,b-c,y-z,z-a;若輸入的字符串連續(xù)出現(xiàn)兩個(gè)字母相同時(shí),后一個(gè)字母需要連續(xù)轉(zhuǎn)換 2 次。例如:aa相同字母超過兩個(gè)時(shí),第三個(gè)出現(xiàn)的字母按第一次出現(xiàn)
44、算。要求實(shí)現(xiàn)函數(shù):轉(zhuǎn)換為bc,zz轉(zhuǎn)換為ab;當(dāng)連續(xù)void convert(char *inphar* output)輸入的字符串,輸出的字符串【輸入】【輸出】【返回】char *input ,char *output無#include#include #includevoid convert(char *inpif(input=NULL) return;char temp=0;har* output)len_input=strlen(input);i;flag=0;for(i=0;ilen_input;i+)if(inputi!=temp)outputi=(inputi-a+1)%26+a
45、; temp=inputi;flag=1;elseif(flag=1)outputi=(inputi-a+2)%26+a;temp=inputi; flag=0;elseoutputi=(inputi-a+1)%26+a;temp=inputi; flag=1;outputi=0;void main()char *input=xyz; char output256;/scanf(%s,input); convert(input,output); prf(%sn,output);15. 在給定字符串中找出單詞( “單詞”由大寫字母和小寫字母字符,其他非字母字符視為單詞的間隔,如空格、問號(hào)、數(shù)字等
46、等;另外單個(gè)字母不算單詞);找到單詞后,按照長度進(jìn)行降序排序,(排序時(shí)如果長度相同,則按出現(xiàn)的順序進(jìn)行排列),然后輸出到一個(gè)新的字符串中;如果某個(gè)單詞重復(fù)出現(xiàn)多次,則只輸出一次;如果整個(gè)輸入的字符串中沒有找到單詞,請(qǐng)輸出空串。輸出的單詞之間使用一個(gè)“空格”隔開,最后一個(gè)單詞后不加空格。要求實(shí)現(xiàn)函數(shù):void my_word(charinput, char output)【輸入】【輸出】【返回】char input, 輸入的字符串char output,輸出的字符串無#include #include #include void my_word(char input,char output)ch
47、ar *p; char *temp;char *word10;len_input=strlen(input); i,j;char except = ,;char *bl= ;i=0;for (i=0;ilen_input;i+)if (inputiZ&inputiz)inputi=,;j=0;/*保存取出的單詞*/ p= strtok(input,except); while(NULL!=p)wordj+=p;p= strtok(NULL,except);for(i=0;i5;i+)prf(%s,wordi);/*對(duì)單詞按照長度降序排序,冒泡法*/ for (i=0;i5;i+)for (j=
48、1;j5-i;j+)if(strlen(wordj-1)strlen(wordj)temp=wordj; wordj=wordj-1; wordj-1=temp;/*刪除相同單詞*/ for (i=0;i5;i+)for(j=i+1;j5;j+)if(strcmp(wordi,wordj)=0) wordj=0;/*將單詞連接起來輸出*/ for (j=0;j5;j+)if (0=j)strncpy(output,wordj,strlen(wordj)+1);elsestrcat(output,bl);strcat(output,wordj);return ;main()char input
49、=some local buses, some1234123drivers;prf(篩選之前的字符串:%sn,input); char output30;my_word(input,output);prf(篩選之后的字符串:%s,output); prf(n);return 0;. 數(shù)組中數(shù)字都兩兩相同,只有一個(gè)不同,找出該數(shù)字:findUnique(* a,len)i = 1;temp = a0; for(; i len; i+)temp = temp ai;prf(%d , temp);17. 題目二:數(shù)組中數(shù)字兩兩相同,有兩個(gè)不同,找出這兩個(gè):#include a = 1,1,2,4,3
50、,3,2,5;findXorSum(* a,len)i = 0;temp = 0;for(; i 1;count+;return count;isBit1(a,count)a = a count-1;return (a & 1);voidfindTwoUnique(* a,len)i = 0;m = 0, n = 0;temp = findXorSum(a, len);count = find for(; i len; i+)Bit1(temp);if(isBit1(ai,count)m = m ai;elsen = n ai;prf(%d, %d, m, n);main()findTwoUn
51、ique(a,8);18. 鏈表翻轉(zhuǎn)。給出一個(gè)鏈表和一個(gè)數(shù) k,比如鏈表 123456,k=2,則翻轉(zhuǎn)后 214365,若 k=3,翻轉(zhuǎn)后 321654,若 k=4,翻轉(zhuǎn)后 432156,用程序?qū)崿F(xiàn):采用遍歷鏈表,分成length/k 組,對(duì)每組進(jìn)行逆轉(zhuǎn),逆轉(zhuǎn)的同時(shí)要將逆轉(zhuǎn)后的尾和頭連接起來/#include stdafx.h #include stdio.h #include stdlib.h#includetypedef struct Nodevalue; Node* next;LinkList;void Converse(LinkList* pPre,LinkList* pCur) /鏈
52、表逆轉(zhuǎn)LinkList* p = NULL;LinkList* pNext = NULL; p = pPre-next;LinkList* p1= NULL; if(pCur!=NULL)pNext = pCur-next;while( p!=pNext)p1 = p-next;p-next = pPre; pPre = p;p = p1;main()count = 0, k,i=0,j=0,flag = 1,length=0,groups = 0;scanf(%d,&k);LinkList* pPre = (LinkList*)malloc(sizeof(LinkList); LinkLis
53、t* pCur = (LinkList*)malloc(sizeof(LinkList); LinkList* pNext = (LinkList*)malloc(sizeof(LinkList); LinkList* head = NULL;LinkList* pTempTail = NULL; /指向逆轉(zhuǎn)之后的尾部LinkList* pTempHead = NULL;pCur-value = 1;pPre = pCur;/創(chuàng)建初始鏈表 for(i=2;ivalue = i; pCur-next = node; pCur = node;pCur-next = NULL;/最后一定要置 NUL
54、L,c+中用 new 則無須置 NULLpCur = pPre;while(pCur!=NULL)length+;pCur = pCur-next;i=0;groups = length/k; /分成 K 段pCur = pPre; while(i=groups)count = 0;while(countk-1 & inext; count+;if(inext;pTempHead = pCur;/*沒做翻轉(zhuǎn)之前的頭部,變成了翻轉(zhuǎn)之后的尾部*/ if(flag = 0)pTempTail-next = pTempHead;pTempTail = pPre; Converse(pPre,pCur)
55、;/pTempTail = pPre; if(flag=1)head = pCur; flag = 0;pCur = pNext;elsepTempTail-next = pNext;pPre = pCur; i+;pCur = head;while(jvalue); pCur = pCur-next;prf(n);/system(pause); return 0;19. 鏈表相鄰元素翻轉(zhuǎn),如 a-b-c-d-e-f-g,翻轉(zhuǎn)后變?yōu)椋篵-a-d-c-f-e-g #include #include #include typedef struct nodechar val;struct node*
56、 pNext;Node;Node* Creaist(n);void Traverslist(Node* pHead);Node* TransNeighbor(Node* pHead);main()Node* pHead = Creaist(7);prf(before transformn);Traverslist(pHead); TransNeighbor(pHead);prf(nafter transformn); Traverslist(pHead);getchar();return 1;/創(chuàng)建新鏈表 Node* Creaist(n)Node* pHead = (Node*)malloc(
57、sizeof(Node);Node* pTail = pHead; pTail-pNext=NULL;i;for(i=0; i val = a+i;pTail-pNext = pNew;pNew-pNext = NULL; pTail = pNew;return pHead;void Traverslist(Node* pHead)Node* p = pHead-pNext;is= 0;while(p!= NULL)if(is=0)priselseprf(%c,p-val);=1;f(-%c,p-val);p = p-pNext;return;Node* TransNeighbor(Node*
58、 pHead)Node* p = pHead-pNext;while(p-pNext!=NULL & p-pNext-pNext!=NULL)char value = p-val;p-val=p-pNext-val; p-pNext-val=value; p=p-pNext-pNext;return pHead;20. 輸入一串字符串,其中有普通的字符與括號(hào)組成(包括(、)、,),要求驗(yàn)證括號(hào)是否匹配,如果匹配 則輸出 0、否則輸出 1.#include #include/#define MAX 100main()char a100,c=(1+2);i=0,j=0;flag=0;while(c
59、i!=NULL&flag=0)switch(ci)case():case():aj+=ci;break;case():if(aj-1=()aj-1=0; j-;elseflag=1; break;case():if(aj-1=)aj-1=0; j-;elseflag=1; break;i+;if(j!=0) flag=1;prf(%dn,flag); return 0;方法 2:#include #include#include / !分配內(nèi)存頭文件 #define m 20typedef char ElemType; typedef structElemType stackm; top;st
60、acknode;stacknode *sp;Init(stacknode *st)st-top=0; return 0;void Push(stacknode *sif(st-top=m)emType x)prf(The stack is overflow!n);elsest-top=st-top+1; st-stackst-top=x;void Pop(stacknode *st)st-top=st-top-1;main()char sm=(); i;prf(Creat a stack!n);sp = (stacknode *)malloc(sizeof(stacknode); Init(s
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年哈爾濱城市職業(yè)學(xué)院單招職業(yè)技能測(cè)試題庫審定版
- Unit7 What's the highest mountain in the world.寫作課教學(xué)設(shè)計(jì)-2024-2025學(xué)年人教版英語八年級(jí)下冊(cè)
- 第一單元寫作教學(xué)設(shè)計(jì) 2024-2025學(xué)年七年級(jí)上冊(cè)同步
- 高端裝備數(shù)字化項(xiàng)目目標(biāo)與愿景
- 2025至2030年中國廣東點(diǎn)心數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 二零二五年度新員工入職勞動(dòng)合同簽訂與員工培訓(xùn)計(jì)劃協(xié)議
- 2025年度車輛保險(xiǎn)責(zé)任協(xié)議書及年度安全評(píng)估
- 二零二五年度政府信息保密承諾書
- 第二單元第4節(jié) 云服務(wù) 教學(xué)設(shè)計(jì)-2023-2024學(xué)年粵教清華版初中信息技術(shù)七年級(jí)上冊(cè)
- 二零二五年度委托代理人才招聘與薪酬福利管理合同
- 骶髂關(guān)節(jié)損傷郭倩課件
- 教學(xué)課件-電力系統(tǒng)的MATLAB-SIMULINK仿真與應(yīng)用(王晶)
- GB/T 26189.2-2024工作場(chǎng)所照明第2部分:室外作業(yè)場(chǎng)所的安全保障照明要求
- 新教科版一年級(jí)科學(xué)下冊(cè)第一單元《身邊的物體》全部課件(共7課時(shí))
- 鹽城江蘇鹽城市住房和城鄉(xiāng)建設(shè)局直屬事業(yè)單位市政府投資工程集中建設(shè)管理中心招聘4人筆試歷年參考題庫附帶答案詳解
- 預(yù)防感冒和流感的方法
- 2024年黑龍江職業(yè)學(xué)院高職單招語文歷年參考題庫含答案解析
- 醫(yī)院教學(xué)秘書培訓(xùn)
- 2025江蘇常州西太湖科技產(chǎn)業(yè)園管委會(huì)事業(yè)單位招聘8人歷年高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025年北京控股集團(tuán)有限公司招聘筆試參考題庫含答案解析
- 小學(xué)教室衛(wèi)生管理
評(píng)論
0/150
提交評(píng)論