參考編程題庫.doc_第1頁
參考編程題庫.doc_第2頁
參考編程題庫.doc_第3頁
參考編程題庫.doc_第4頁
參考編程題庫.doc_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、 刪除一個字符串中所有的空格1函數(shù)delspace的功能是刪除一個字符串中所有的空格。例如,輸入字符串為This is a string,則輸出結果為Thisisastring。測試用主函數(shù)如下所示,請編制函數(shù)delspace。 #include #include void main()char *delspace(char *str);char s81,*ds;gets(s);ds=delspace(s);printf(nResult: %sn, ds);char *delspace(char *str)char *p=str; while(*p)if(*p= )strcpy(p,p+1);elsep+;return str;I am a student.Result: Iamastudent.Press any key to continue2、 求3位數(shù)2. 已知兩個3位數(shù)abc和cba之和為1333(即abc+cba=1333),編程序求出3個數(shù)字a、b和c。#include void main()int n,a,b,c;for(n=100;n1000;n+)a=n/100;b=n/10%10;c=n%10;if(a*100+b*10+c+c*100+b*10+a=1333)printf(abc is: %d%d%dn,a,b,c);abc is: 419abc is: 518abc is: 617abc is: 716abc is: 815abc is: 914Press any key to continue3、 求同構數(shù)3.若一個數(shù)出現(xiàn)在自己平方數(shù)的右邊,則稱該數(shù)為同構數(shù)。如,因有5*5=25,25*25=625,所以5和25都是同構數(shù);請編制程序找出1100之間的全部同構數(shù)。#includevoid main() int check(int n); int n; for(n=1;n=100;n+) if(check(n) printf(%d,%dn,n,n*n); int check(int n) int k;k=n*n;if(k%10=n|k%100=n)return 1;elsereturn 0;1,15,256,3625,62576,5776Press any key to continue4、 有序數(shù)組插入元素4.編程序實現(xiàn)功能:在一個元素值按升序存放的整型數(shù)組中插入一個數(shù),使得插入后的數(shù)組元素仍然有序#include#define N 5void main() int aN+1,x,i,k; for(i=0;iN;i+) scanf(%d,&ai); scanf(%d,&x); for(i=0;aixⅈk-) ak=ak-1; ai=x; for(i=0;i=N;i+) printf(%5d,ai); putchar(n); 34 678 89 124 36789 34 89 678 89 124 367Press any key to continue#include#define N 5void main() void insert(int v,int n,int x);int aN+1,x,i; for(i=0;iN;i+) scanf(%d,&ai); scanf(%d,&x); insert(a,N,x); for(i=0;i=N;i+) printf(%5d,ai); putchar(n); void insert(int v,int n,int x) int i,k; for(i=0;vixⅈk-) vk=vk-1; vi=x; 23 45 67 89 99989 23 45 67 89 89 999Press any key to continue5、 求滿足條件最小正整數(shù)5. 編程序實現(xiàn)功能:一個正整數(shù)與3的和是5的倍數(shù),與3的差是6的倍數(shù),求出符合此條件的最小正整數(shù)。#includevoid main() int i; for(i=9;i+) if(i+3)%5=0&(i-3)%6=0) printf(%dn,i); break; 27Press any key to continue6、 顛倒字符串6. 函數(shù)reverse的原型為:void reverse(char s);,其功能是將字符串s中的字符位置顛倒。例如,字符串“abcdefg”中的字符位置顛倒后變?yōu)椤癵fedcba”。編制函數(shù)reverse并用相應的主函數(shù)進行測試。#include#includevoid reverse(char s) int i,j; char c; for(i=0,j=strlen(s)-1;ij;i+,j-) c=si; si=sj; sj=c; void main()char s100;puts(input a string:);gets(s);reverse(s);puts(s);input a string:Great Wall ChinaanihC llaW taerGPress any key to continue7、 求3位整數(shù)7.編程序實現(xiàn)功能:求出所有各位數(shù)字的立方和等于1099的3位整數(shù)。#includevoid main() int a,b,c; for(a=1;a=9;a+) for(b=0;b=9;b+) for(c=0;c=9;c+) if(a*a*a+b*b*b+c*c*c=1099) printf(%d%d%dn,a,b,c); 379397739793937973Press any key to continue#includevoid main() int a,b,c,n; for(n=100;n1000;n+) a=n/100;b=n%100/10; c=n%10; if(a*a*a+b*b*b+c*c*c=1099) printf(%d%d%dn,a,b,c); 379397739793937973Press any key to continue8、 截子串8. 函數(shù)cut的原型為:char *cut(char *s,int m,int n);,其功能是:從字符串s中第m個位置開始,截取含有n個字符的子串;若從m開始剩余的字符不足n個,則截取剩余的所有字符;函數(shù)返回所截子串的首地址。編制函數(shù)reverse并用相應的主函數(shù)進行測試。#include char *cut(char *,int,int);void main() char s=goodmorning; char *p; p=cut(s,10,4); printf(%sn,p);char *cut(char *s,int m,int n)int i;static char sub20; for(i=0;in;i+)subi=sm+i-1; subi=0; return sub;ngPress any key to continue9、 求階乘的累加和9. 求階乘的累加和(即求1!+2!+3!+4!+5!+20!)代碼:#include int main() double s=0,t=1; int n; for (n=1;n=20;n+) t=t*n; s=s+t; printf(1!+2!+.+20!=%22.15en,s); return 0;結果:1!+2!+.+20!=2.561327494111820e+018Press any key to continue10、 求水仙花數(shù)求出所有水仙花數(shù),所謂水仙花數(shù)是指一個三位數(shù),其各位數(shù)字立方和等于該本身。例如:153是一個水仙花數(shù),因為153=13+53+33。代碼:#include int main() int i,j,k,n; printf(parcissus numbers are ); for (n=100;n1000;n+) i=n/100; j=n/10-i*10; k=n%10; if (n=i*i*i + j*j*j + k*k*k) printf(%d ,n); printf(n); return 0;結果:parcissus numbers are 153 370 371 407Press any key to continue11、 求素數(shù)用篩法求100之內的素數(shù)。代碼:#include #include int main()int i,j,n,a101; for (i=1;i=100;i+) ai=i; a1=0; for (i=2;isqrt(100);i+) for (j=i+1;j=100;j+) if(ai!=0 & aj!=0) if (aj%ai=0) aj=0; printf(n);for (i=2,n=0;i=100;i+) if(ai!=0)printf(%5d,ai);n+;if(n=10)printf(n);n=0; printf(n);return 0;結果: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97Press any key to continue12、 選擇法排序用選擇法對10個整數(shù)從小到大排序。代碼:#include int main()int i,j,min,temp,a11; printf(enter data:n); for (i=1;i=10;i+) printf(a%d=,i); scanf(%d,&ai); printf(n); printf(The orginal numbers:n); for (i=1;i=10;i+) printf(%5d,ai); printf(n); for (i=1;i=9;i+) min=i; for (j=i+1;jaj) min=j; temp=ai; ai=amin; amin=temp; printf(nThe sorted numbers:n); for (i=1;i=10;i+) printf(%5d,ai); printf(n); return 0; 結果:enter data:a1=34a2=86a3=92a4=16a5=69a6=62a7=29a8=95a9=60a10=8The orginal numbers: 34 86 92 16 69 62 29 95 60 8The sorted numbers: 8 16 29 34 60 62 69 86 92 95Press any key to continue13、 輸出楊輝三角形輸出以下的楊輝三角形(要求打印出10行)。1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 代碼:#include #define N 10int main() int i,j,aNN; for (i=0;iN;i+) aii=1; ai0=1; for (i=2;iN;i+) for (j=1;j=i-1;j+) aij=ai-1j-1+ai-1j; for (i=0;iN;i+) for (j=0;j=i;j+) printf(%6d,aij); printf(n); printf(n); return 0;結果: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1Press any key to continue14、 起泡法排序寫一個函數(shù),用“起泡法”對輸入的10個字符按由小到大的順序排列。代碼:#include #include #define N 10char strN;int main()void sort(char );int i,flag;for (flag=1;flag=1;)printf(input string:n);scanf(%s,&str);printf(Inputed string is: %sn,str);if (strlen(str)N)printf(string too long,input again!);elseflag=0;sort(str);printf(string sorted:n);for (i=0;iN;i+)printf(%c,stri);printf(n);return 0;void sort(char str)int i,j;char t;for(j=1;jN;j+)for (i=0;(istri+1)t=stri;stri=stri+1;stri+1=t;結果:input string:GoodbyeInputed string is: Goodbyestring sorted:15、 字符串排序用指向指針的指針的方法對5個字符串排序輸出。代碼:#include #include #define LINEMAX 20 /*定義字符串的最大長度*/int main() int sort(char *p);int i; char *p,*pstr5,str5LINEMAX; for (i=0;i5;i+) pstri=stri; /*將第i個字符串的首地址賦予指針數(shù)組 pstr 的第i個元素*/ printf(input 5 strings:n); for (i=0;i5;i+) scanf(%s,pstri); p=pstr; sort(p); printf(strings sorted:n); for (i=0;i5;i+) printf(%sn,pstri);int sort(char *p) /*冒泡法對5個字符串排序函數(shù)*/int i,j;char *temp;for (i=0;i5;i+)for (j=i+1;j0) /*比較后交換字符串地址*/temp=*(p+i);*(p+i)=*(p+j);*(p+j)=temp;return 0;結果:input 5 strings:wangzhuliumingxuestrings sorted:liumingwangxuezhuPress any key to continue16、 打印學生數(shù)據編寫一個函數(shù)print,打印一個學生的成績數(shù),該數(shù)組中有5個學生的數(shù)據記錄,每個記錄包括num、name、sore3,用主函數(shù)輸入這些記錄,用print函數(shù)輸出這些記錄。代碼:#include #define N 5struct student char num6; char name8; int score4;stuN;int main()void print(struct student stu6);int i,j;for (i=0;iN;i+)printf(ninput score of student %d:n,i+1); printf(NO.: ); scanf(%s,stui.num); printf(name: ); scanf(%s,); for (j=0;j3;j+) printf(score %d:,j+1); scanf(%d,&stui.scorej); printf(n);print(stu);return 0;void print(struct student stu6)int i,j;printf(n NO. name score1 score2 score3n);for (i=0;iN;i+)printf(%5s%10s,stui.num,);for (j=0;j3;j+)printf(%9d,stui.scorej);printf(n);結果:input score of student 1:NO.: 110name: Liuscore 1:34score 2:56score 3:67input score of student 2:NO.: 120name: Wangscore 1:88score 2:87score 3:95input score of student 3:NO.: 130name: Mingscore 1:83score 2:89score 3:97input score of student 4:NO.: 140name: Zhuscore 1:46score 2:35score 3:68input score of student 5:NO.: 150name: Weiscore 1:56score 2:86score 3:93 NO. name sc

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論