題目內容:
編寫程序,在主函數中輸入兩個正整數 a,b,調用兩個函數 fun1() 和 fun2(),分別求 a 和 b 的最大公約數和最小公倍數,在主函數中輸出結果。
輸入格式:
兩個正整數
輸出格式:
最大公約數和最小公倍數
輸入樣例:
12,40[回車]
輸出樣例:
最大公約數:4[回車]
最小公倍數:120[回車]
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int LCM(int x, int y); 5 int GCD(int x, int y); 6 int main() 7 { 8 int a,b; 9 scanf("%d,%d",&a,&b); 10 printf("最大公約數:%d\n",GCD(a,b)); 11 printf("最小公倍數:%d",LCM(a,b)); 12 return 0; 13 } 14 15 int LCM(int x, int y){ 16 return x/GCD(x,y)*y; 17 } 18 int GCD(int x, int y){ 19 return y == 0 ? x : GCD(y, x%y); 20 }
題目內容:
編寫程序,在主函數中定義一個有10個元素的整型一維數組,用戶輸入9個數據,調用函數,對數組元素進行從小到大排序后,在函數中輸入一個數,插入到數組中正確的位置,並輸出。
輸入格式:
9個整數和1個整數
輸出格式:
10個整數
輸入樣例:
2,3,1,5,6,76,89,31,90[回車]
7[回車]
輸出樣例:
1,2,3,5,6,7,31,76,89,90[回車]
1 #include<stdio.h> 2 void sortArr(int *arr, int n, int x) 3 { 4 arr[n-1] = x; 5 //插入排序 6 int i; 7 for(i=1; i<n; ++i){ 8 int j=i-1,key=arr[i]; 9 while(j>=0 && key<arr[j]){ 10 arr[j+1]=arr[j]; 11 j--; 12 } 13 arr[j+1]=key; 14 } 15 } 16 int main() 17 { 18 int arr[10] = {0},i,x; 19 for(i=0; i<8; ++i) 20 scanf("%d,",&arr[i]); 21 scanf("%d%d",&arr[i],&x); 22 23 sortArr(arr,10,x); 24 for(i=0; i<9; ++i) 25 printf("%d,",arr[i]); 26 printf("%d\n",arr[i]); 27 return 0; 28 }
3、奇數求和
題目內容:
用遞歸算法實現,輸入整數n(n>0), 求1+3+5+7….+(2*n-1) 的和
輸入格式:
輸入整數n
輸出格式:
輸出和
輸入樣例:
5[回車]
輸出樣例:
25[回車]
1 #include<stdio.h> 2 3 int sum(int n); 4 int main() 5 { 6 int n; 7 scanf("%d",&n); 8 printf("%d\n",sum(n)); 9 return 0; 10 } 11 12 int sum(int n) 13 { 14 if(n == 1) 15 return 1; 16 return 2*n-1 + sum(n-1); 17 }
4、巧算自然數
題目內容:
編程實現輸入一個自然數,若為偶數,則把它除以2;若為奇數,則把它乘以3加1。經過如此有限次運算后,總可以得到自然數值1。輸出經過多少次可以得到自然數1和每次得到的值。
輸入格式:
輸入一個自然數
輸出格式:
輸出經過多少次可以得到自然數1和每次得到的值
輸入樣例:
22[回車]
輸出樣例:
22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1[回車]
step=16[回車]
1 #include <stdio.h> 2 int main() 3 { 4 int n, step=1; 5 scanf("%d",&n); 6 while(n!=1) 7 { 8 printf("%d,",n); 9 step++; 10 if(n%2==0) 11 n /= 2; 12 else if(n%2) 13 n = n*3+1; 14 } 15 printf("1\n"); 16 printf("step=%d\n", step); 17 return 0; 18 }
5、賣鴨子
題目內容:
編程調用遞歸函數。一個人趕着鴨子去每個村庄賣,每經過一個村子賣去所趕鴨子的一半又一只。這樣他經過了7個村子后還剩2只鴨子,問他出發時共趕多少只鴨子?經過每個村子時依次賣出多少只鴨子?
輸入格式:
無
輸出格式:
出發時總鴨子數
每個村子賣出鴨子數
輸入樣例:
無
輸出樣例:(不是結果,僅表示格式)
sum=25[回車]
sell=8,sell=4,[回車]
1 #include <stdio.h> 2 int fun(int arr[],int n,int m) 3 { 4 if(n==0){ 5 arr[n] = m/2+1; 6 return m; 7 } 8 arr[n] = m/2+1; 9 return fun(arr, n-1,m=2*(m+1)); 10 } 11 int main() 12 { 13 int arr[7] = {0}; 14 printf("sum=%d\n", fun(arr,7,2)); 15 for(int i=0; i<7; ++i) 16 printf("sell=%d,", arr[i]); 17 return 0; 18 }
6、輸出各位數字之和
題目內容:
編程調用遞歸函數,求輸入一個數,輸出這個數的各位數字之和。
輸入格式:
輸入一個數
輸出格式:
輸出這個數的各位數字之和
輸入樣例:
2354[回車]
輸出樣例:
14[回車]
1 #include <stdio.h> 2 int fun(int n) 3 { 4 if(n==0) 5 return 0; 6 return n%10 + fun(n/10); 7 } 8 int main() 9 { 10 int n; 11 scanf("%d",&n); 12 printf("%d\n", fun(n)); 13 return 0; 14 }
7、排序
題目內容:
用指針方法,將一維數組int a[10] 中元素按從小到大順序輸出。
輸入格式:
10個整數,空格隔開
輸出格式:
排序后的10個數,逗號隔開
輸入樣例:
12 34 56 43 7 89 81 11 33 90[回車]
輸出樣例:
7,11,12,33,34,43,56,81,89,90[回車]
1 #include <stdio.h> 2 #include <string.h> 3 #define N 1000 4 5 int main() 6 { 7 int arr[N] = {0},x; 8 for(int i=0; i<10; ++i){ 9 scanf("%d",&x); 10 arr[x]++; 11 } 12 int flag = 1; 13 for(int j=0; j<N; ++j){ 14 while(arr[j]){ 15 if(!flag) 16 printf(","); 17 printf("%d",j); 18 flag = 0; 19 arr[j]--; 20 } 21 } 22 return 0; 23 }
1 #include <stdio.h> 2 #include <string.h> 3 #define N 1000 4 5 int main() 6 { 7 int arr[N] = {0}; 8 for(int i=0; i<10; ++i) 9 scanf("%d",&arr[i]); 10 //選擇(選一個最小的) 11 for(int *p = arr; p<arr+9; ++p){ 12 for(int *q = p+1; q<arr+10; ++q){ 13 if(*q<*p){ 14 int t = *q; 15 *q = *p; 16 *p = t; 17 } 18 } 19 } 20 21 int flag = 0; 22 for(int *p = arr; p<arr+10; ++p){ 23 if(flag) 24 printf(","); 25 flag = 1; 26 printf("%d",*p); 27 } 28 return 0; 29 }
1 #include <stdio.h> 2 #include <string.h> 3 #define N 1000 4 5 int main() 6 { 7 int arr[N] = {0}; 8 for(int i=0; i<10; ++i) 9 scanf("%d",&arr[i]); 10 //冒泡 11 for(int i=0; i<10; ++i){ 12 for(int j=9; j>i; --j){ 13 if(arr[j-1]>arr[j]){ 14 int t = arr[j-1]; 15 arr[j-1] = arr[j]; 16 arr[j] = t; 17 } 18 } 19 } 20 21 for(int k=0; k<10; ++k){ 22 if(k) 23 printf(","); 24 printf("%d",arr[k]); 25 } 26 return 0; 27 }
8、成績排序
題目內容:
從鍵盤輸入3個同學4門課的成績,將其按平均分從高到低排序輸出。
輸入格式:
輸入3個同學4門課的成績
輸出格式:
按平均分從高到低排序輸出
輸入樣例:
89 78 80 76 88 78 75 90 99 92 100 89[回車]
輸出樣例:
99,92,100,89[回車]
88,78,75,90[回車]
89,78,80,76[回車]
1 #include <stdio.h> 2 struct student{ 3 int a,b,c,d; 4 double avg; 5 }; 6 void swap(struct student *a,struct student *b){ 7 struct student t = *a; 8 *a = *b; 9 *b = t; 10 } 11 int main() 12 { 13 struct student s[3]; 14 for(int i=0; i<3; ++i){ 15 scanf("%d%d%d%d",&s[i].a,&s[i].b,&s[i].c,&s[i].d); 16 s[i].avg = (s[i].a + s[i].b + s[i].c + s[i].d)/4.0; 17 } 18 19 if(s[0].avg<s[1].avg) 20 swap(&s[0],&s[1]); 21 if(s[0].avg<s[2].avg) 22 swap(&s[0],&s[2]); 23 if(s[1].avg<s[2].avg) 24 swap(&s[1],&s[2]); 25 26 for(int i=0; i<3;++i) 27 printf("%d,%d,%d,%d\n",s[i].a,s[i].b,s[i].c,s[i].d); 28 return 0; 29 }
9、輸出指定學生成績
題目內容:
從鍵盤輸入3個同學4門課的成績,輸出指定同學的成績和平均分。
輸入格式:
輸入3個同學4門課的成績
輸出格式:
輸出指定同學的成績和平均分
輸入樣例:
89 78 80 76 88 78 75 90 99 92 100 89[回車]
1[回車]
輸出樣例:
89 78 80 76[回車]
80.75[回車]
1 #include <stdio.h> 2 struct student{ 3 int a,b,c,d; 4 double avg; 5 }; 6 7 int main() 8 { 9 struct student s[3]; 10 for(int i=0; i<3; ++i){ 11 scanf("%d%d%d%d",&s[i].a,&s[i].b,&s[i].c,&s[i].d); 12 s[i].avg = (s[i].a + s[i].b + s[i].c + s[i].d)/4.0; 13 } 14 15 int i; 16 scanf("%d",&i); 17 printf("%d %d %d %d\n",s[i-1].a,s[i-1].b,s[i-1].c,s[i-1].d); 18 if(s[i-1].avg==(int)s[i-1].avg) 19 printf("%.f\n",s[i-1].avg); 20 else 21 printf("%.2f\n",s[i-1].avg); 22 return 0; 23 }
10、統計字符串出現次數
題目內容:
從鍵盤輸入兩個字符串,輸出第二個串在第一個串中出現的次數。如果沒有,輸出“No”。
輸入格式:
輸入兩個字符串
輸出格式:
輸出第二個串在第一個串中出現的次數。
如果沒有,輸出 No
輸入樣例1:
This is his book[回車]
is[回車]
輸出樣例1:
3[回車]
輸入樣例2:
This is my book[回車]
at[回車]
輸出樣例2:
No[回車]
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int KMPStrMatching(char* S, char* P, int *N, int start) 6 { 7 int j= 0; // 模式的下標變量 8 int i = start; // 目標的下標變量 9 int pLen = strlen(P); // 模式的長度 10 int tLen = strlen(S); // 目標的長度 11 if (tLen - start < pLen) // 若目標比模式短,匹配無法成功 12 return (-1); 13 while ( j < pLen && i < tLen) 14 { // 反復比較,進行匹配 15 if ( j == -1 || S[i] == P[j]) 16 i++, j++; 17 else j = N[j]; 18 } 19 if (j >= pLen) 20 return (i-pLen); // 注意仔細算下標 21 else return (-1); 22 } 23 24 int* findNext(char* P) 25 { 26 int j, k; 27 int m = strlen(P); // m為模式P的長度 28 int *next = (int*)malloc(m*sizeof(int)); 29 next[0] = -1; 30 j = 0; k = -1; 31 while (j < m-1) 32 { 33 if(k == -1 || P[k] == P[j]){ 34 j++; k++; next[j] = k; 35 } 36 else 37 k = next[k]; //不等則采用 KMP 遞歸找首尾子串 38 } 39 return next; 40 } 41 42 int main() 43 { 44 int count=0; 45 char s1[256]="",s2[256]=""; 46 gets(s1); 47 gets(s2); 48 int len1 = strlen(s1); 49 int len2 = strlen(s2); 50 int ret = 0; 51 for(int i=0; i<len1; i=ret+len2) 52 { 53 ret = KMPStrMatching(s1,s2,findNext(s2),i); 54 if(ret == -1) 55 break; 56 count++; 57 } 58 if(count) 59 printf("%d\n",count); 60 else 61 printf("No\n"); 62 return 0; 63 }
11、成績統計
題目內容: 有如下學生成績表,第一列前4行為學生姓名,第一列最后一行為平均分,表中給定數據為學生成績(每一列為同一門課)
編程輸出:學生姓名,每個學生的平均分,及各門課程平均分
輸入格式:
無
輸出格式:
學生姓名,每個學生的平均分(按行輸出)各門課程平均分(按列輸出)(若平均分為整數,則輸出整數,若平均分為小數,則保留兩位)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 static float av[4]; 6 7 struct student{ 8 char *s; 9 float score[4]; 10 }stu[4]; 11 12 void fun1(struct student *stu) 13 { 14 float sum=0; 15 for(int i=0; i<4; ++i) 16 { 17 av[i]+=stu->score[i]; 18 sum += stu->score[i]; 19 } 20 if((int)sum/4==sum/4) 21 printf("%s,%.f\n",stu->s,sum/4); 22 else 23 printf("%s,%.2f\n",stu->s,sum/4); 24 } 25 26 void fun2(float av) 27 { 28 if((int)(av/4)==av/4) 29 printf("%.f",av/4); 30 else 31 printf("%.2f",av/4); 32 } 33 34 int main() 35 { //0 36 stu[0].s = "wanglei",stu[0].score[0] = 78,stu[0].score[1] = 90,stu[0].score[2] = 87,stu[0].score[3] = 92; 37 fun1(&stu[0]); 38 //1 39 stu[1].s = "lihong",stu[1].score[0] = 88,stu[1].score[1] = 91,stu[1].score[2] = 89,stu[1].score[3] = 78; 40 fun1(&stu[1]); 41 //2 42 stu[2].s = "zhangli",stu[2].score[0] = 84,stu[2].score[1] = 76,stu[2].score[2] = 83,stu[2].score[3] = 75; 43 fun1(&stu[2]); 44 //3 45 stu[3].s = "liuming",stu[3].score[0] = 88,stu[3].score[1] = 90,stu[3].score[2] = 71,stu[3].score[3] = 83; 46 fun1(&stu[3]); 47 //4 48 printf("AVERAGE:"); 49 for(int i=0; i<4; ++i){ 50 if(i) 51 printf(","); 52 fun2(av[i]); 53 } 54 return 0; 55 }
12、輸出菱形
題目內容:
輸入數字和大寫字母,輸出由字母組成的菱形。
例如:3 B
B
C C
D D
E E
F
輸入格式:
數字和大寫字母
輸出格式:
由字母組成的菱形
輸入樣例:
3[空格]B[回車]
輸出樣例:
[空格][空格]B[回車]
[空格]C[空格]C[回車]
D[空格][空格][空格]D[回車]
[空格]E[空格]E[回車]
[空格][空格]F[回車]
1 #include <stdio.h> 2 int main() 3 { 4 char start; 5 int high,i,j; 6 scanf("%d %c",&high,&start); 7 //上半部 8 for (i=0; i<high; i++) 9 { 10 //空格 11 for (j=1;j<high-i;j++) 12 printf (" "); 13 //字符、空格、字符 14 printf ("%c",start); 15 for (j=0;j<2*i-1;j++) 16 printf (" "); 17 if (i) 18 printf ("%c",start); 19 start++; 20 //換行 21 printf ("\n"); 22 } 23 24 //下半部 25 for (i=high-1; i>0 ;i--) 26 { 27 //空格 28 for (j=0;j<high-i;j++) 29 printf (" "); 30 //字符、空格、字符 31 printf ("%c",start); 32 for (j=0;j<2*i-3;j++) 33 printf (" "); 34 if (i-1){ 35 printf ("%c",start); 36 } 37 start++; 38 //換行 39 printf ("\n"); 40 } 41 return 0; 42 }
13、最大數
題目內容:
任意輸入一個自然數,輸出該自然數的各位數字組成的最大數。例如,輸入 1593 ,則輸出為 9531。
輸入格式:
自然數 n
輸出格式:
各位數字組成的最大數
輸入樣例:
1593[回車]
輸出樣例:
9531[回車]
1 #include <stdio.h> 2 int main() 3 { 4 int n; 5 scanf("%d",&n); 6 int key[10] = {0}; 7 while(n) 8 { 9 key[n%10]++; 10 n /= 10; 11 } 12 for(int i=9; i>=0; --i){ 13 while(key[i]){ 14 printf("%d",i); 15 key[i]--; 16 } 17 } 18 return 0; 19 }
14、字符串刪除
題目內容:
輸入一個字符串,找出其中相同的字符(不區分大小寫),並刪除,輸出剩余的部分。
例如:輸入:eye 輸出:y
輸入格式:
字符串
輸出格式:
刪除相同字符之后的字符串
輸入樣例:
eye[回車]
輸出樣例:
y[回車]
1 #include<stdio.h> 2 #define N 256 3 4 int main() 5 { 6 /* 輸入一個字符串 */ 7 char s[N] = ""; 8 gets(s); 9 char *p = s; 10 /* 判斷是否重復 */ 11 int key[N] = {0}; 12 while(*p) 13 { 14 key[*p++]++; 15 } 16 /* 輸出不重復的字符 */ 17 for(int i=0; s[i]; ++i) 18 if(key[s[i]]==1) 19 printf("%c",s[i]); 20 return 0; 21 }
15、字母存儲
題目內容:
使用單項鏈表存儲一組字母{a, b, c, d, c, b , a},輸入序號輸出字母,輸入字母輸出最后一次出現的序號,越界則輸出N。
輸入格式:
序號或字母
輸出格式:
字母或序號
輸入樣例1:
2[回車]
輸出樣例1:
c[回車]
輸入樣例2:
c[回車]
輸出樣例2:
4[回車]
輸入樣例3:
8[回車]
輸出樣例3:
N[回車]
1 #include <stdio.h> 2 #include <ctype.h> 3 #include <malloc.h> 4 5 typedef struct node *PNODE; 6 typedef struct node NODE; 7 struct node{ 8 char ch; 9 PNODE next; 10 }; 11 12 /* void print(PNODE head){ 13 while(head){ 14 printf("%c",head->ch); 15 head = head->next; 16 } 17 printf("\n"); 18 } */ 19 20 void print(PNODE head,int ch){ 21 if(!isalpha(ch)&&ch>'7'){ 22 printf("N\n"); 23 return; 24 } 25 int index = -1,t = -1;//元素從0開始計算 26 while(head){ 27 index++; //元素計數 28 if(head->ch==ch)//是字母 29 t = index + '0';//序號轉字符 30 else if(index==ch-'0')//是數字 31 t = head->ch; //保存字母 32 head = head->next; 33 } 34 printf("%c\n",t);//打印字母(數字) 35 } 36 37 int main() 38 { 39 /* 創建頭結點 */ 40 PNODE head,rear; 41 head = rear = (PNODE)malloc(sizeof(NODE)); 42 head->next = NULL; 43 /* 字符串 */ 44 char s[] = "abcdcba"; 45 char*p = s; 46 while(*p) 47 { 48 PNODE s = (PNODE)malloc(sizeof(NODE)); 49 s->ch = *p++; 50 s->next = NULL; 51 rear->next = s;//尾插法 52 rear = s; 53 } 54 /* print(head->next); */ 55 int ch; 56 ch = getchar(); 57 print(head->next,ch); 58 59 return 0; 60 }
16、鏈表合並
題目內容:
實現兩個由單項鏈表存儲的有序字母數據的合並,如有重復的則只保留一個。
例如:給定{a, c ,f}, { b, e, g}合並后結果為{a, b, c , e , f , g}。
輸入格式:
兩個有序字母數據
輸出格式:
合並后的字母數據
輸入樣例1:
a b c[回車]
d e f[回車]
輸出樣例1:
a b c d e f[回車]
輸入樣例2:
e f g[回車]
e g m[回車]
輸出樣例2:
e f g m[回車]
1 #include <stdio.h> 2 #include <malloc.h> 3 typedef struct node NODE; 4 typedef struct node* Linklist; 5 struct node{ 6 char ch; 7 Linklist next; 8 }; 9 Linklist mergelist(Linklist LA,Linklist LB) 10 { 11 /* 12 LA的頭指針為新鏈表的頭、尾指針 13 LB的頭結點釋放 14 */ 15 Linklist head = LA,rear = LA,temp = LB; 16 LA = LA->next; 17 LB = LB->next; 18 free(temp); 19 /* 逐個結點鏈接到新鏈表 */ 20 while(LA && LB) 21 { 22 if(LA->ch == LB->ch) 23 { 24 Linklist p = LB; 25 LB = LB->next; 26 free(p); //刪除重復內容的結點 27 continue; 28 } 29 else if(LB->ch<LA->ch) 30 { 31 rear->next = LB;//尾插結點 32 LB = LB->next; 33 } 34 else if(LA->ch<LB->ch) 35 { 36 rear->next = LA;//尾插結點 37 LA = LA->next; 38 } 39 rear = rear->next;//更新尾結點 40 } 41 if(LA) 42 rear->next = LA;//尾插剩余結點 43 if(LB) 44 rear->next = LB; 45 return head; 46 } 47 Linklist create(){ 48 Linklist L,p,r; 49 L = r = (Linklist)malloc(sizeof(NODE)); 50 L->next = NULL; 51 char ch; 52 while((ch=getchar())!='\n')//空格結束 53 { 54 if(ch!=' '){ //去除空格 55 p = (Linklist)malloc(sizeof(NODE)); 56 p->ch = ch; 57 p->next = NULL; 58 r->next = p; 59 r = p; 60 } 61 } 62 return L; 63 } 64 void print(Linklist L){ 65 int flag = 0; 66 while(L->next) 67 { 68 if(flag) 69 printf(" "); 70 flag = 1; 71 L = L->next; 72 printf("%c",L->ch); 73 } 74 printf("\n"); 75 } 76 int main() 77 { 78 Linklist L1 = create(); 79 Linklist L2 = create(); 80 Linklist L3 = mergelist(L1,L2); 81 print(L3); 82 return 0; 83 }
17、解析字符串
題目內容:
輸入一個字符串,要求將其中的字母‘n’理解為回車符號’\n’,模擬文件緩沖區讀取的數據,並按替換后的數據流解析出其中包括的字符串。(即通過'n'分割兩個字符串)
輸入格式:
一個字符串
輸出格式:
其中包括的字符串
輸入樣例:
abcnde[回車]
輸出樣例:
abc[回車]
de[回車]
1 #include <stdio.h> 2 #define N 256 3 int main() 4 { 5 char str[N]="",*p; 6 p = str; 7 gets(str); 8 while(*p) 9 { 10 if(*p=='n'){ 11 if(*(p-1)!='n') 12 putchar('\n'); 13 } 14 else 15 putchar(*p); 16 p++; 17 } 18 return 0; 19 }
18、字符串的輸入與反向顯示
題目內容:
請用標准設備文件的方式完成字符串的輸入與反向顯示。
輸入格式:
字符串
輸出格式:
字符串
輸入樣例:
abc[回車]
輸出樣例:
cba[回車]
1 #include <stdio.h> 2 #include<string.h> 3 #define N 256 4 int main() 5 { 6 char str[N]="",*p = str; 7 gets(str); 8 p += strlen(str) - 1; 9 while(*p) 10 putchar(*p--); 11 return 0; 12 }
19、基本四則運算表達式
題目內容:
請結合C語言語法知識以及對編譯過程的理解,完成一個僅含一個運算符的基本四則運算表達式字符串的計算。
輸入格式:
基本四則運算表達式字符串
輸出格式:
運算結果
輸入樣例:
1+2
輸出樣例:
3
1 #include <stdio.h> 2 int main() 3 { 4 char op; 5 int i,a,b; 6 scanf("%d%c%d",&a,&op,&b); 7 switch(op) 8 { 9 case '+':printf("%d",a+b);break; 10 case '-':printf("%d",a-b);break; 11 case '*':printf("%d",a*b);break; 12 case '/':printf("%d",a/b);break; 13 } 14 return 0; 15 }
20、遞歸的方法計算含多個運算符的四則運算表達式字符串的值
題目內容:
請在上一題的基礎上,采用遞歸的方法,計算含多個運算符的四則運算表達式字符串的值(無括號,但要考慮優先級)
輸入格式:
多個運算符的四則運算表達式字符串
輸出格式:
運算結果
輸入樣例:
3*2+3
輸出樣例:
9
1 #include <stdio.h> 2 #include <ctype.h> 3 double expression_value();//求一個表達式的值 4 double term_value();//求一個項的值 5 double factor_value();//求一個因子的值 6 double expression_value() 7 { 8 double result=term_value(); 9 while(1) 10 { 11 char op = getc(stdin); 12 ungetc(op, stdin); 13 if(op=='+'||op=='-') 14 { 15 getchar(); 16 double value=term_value(); 17 if(op=='+') 18 result+=value; 19 else 20 result-=value; 21 } 22 else 23 break; 24 } 25 return result; 26 } 27 double term_value() 28 { 29 double result = factor_value(); 30 while(1) 31 { 32 char c= getc(stdin); 33 ungetc(c, stdin); 34 if(c=='*'||c=='/') 35 { 36 getchar(); 37 double value = factor_value(); 38 if(c=='*') 39 result*=value; 40 else 41 result/=value; 42 } 43 else 44 break; 45 } 46 return result; 47 } 48 double factor_value() 49 { 50 double result=0; 51 char c= getc(stdin); 52 ungetc(c, stdin); 53 if(c=='(') 54 { 55 getchar(); 56 result = expression_value(); 57 getchar(); 58 } 59 else//因子本身是一個數字(整個遞歸的出口) 60 { 61 while(isdigit(c))//處理整數部分 62 { 63 result = 10*result+c-'0'; 64 getchar(); 65 c = getc(stdin); 66 ungetc(c, stdin); 67 } 68 if(c=='.')//處理小數部分 69 { 70 getchar(); 71 c = getc(stdin); 72 ungetc(c, stdin); 73 double t=0.1; 74 while(isdigit(c)) 75 { 76 result += (c-'0')*t; 77 t*=0.1; 78 getchar(); 79 c = getc(stdin); 80 ungetc(c, stdin); 81 } 82 } 83 } 84 return result; 85 } 86 int main() 87 { 88 printf("%.f\n",expression_value()); 89 return 0; 90 }
21、從鍵盤輸入一個字符串,該串中包含字母和數字,然后將數字排在最前面,字母 排在后面,不改變原有數字或字母間的順序。
1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 char a[256]="",b[256]=""; 6 char *p,*q,*ptr; 7 p = q = a; 8 ptr = b; 9 gets(a); 10 while(*p) 11 { 12 if(*p>='0'&&*p<='9'){ 13 *ptr++ = *p; 14 } 15 else 16 *q++ = *p; 17 p++; 18 } 19 *q = '\0'; 20 strcat(ptr,a); 21 printf("%s\n",b); 22 return 0; 23 }
22、給定一個數,進行因式分解,刪除重復的因子后,剩余因子相乘后輸出
1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 int n,i=2,result=1,flag=1; 6 scanf("%d",&n); 7 while(i<=n) 8 { 9 if(n%i==0){ 10 n/=i; 11 if(flag) 12 result *=i; 13 flag=0; 14 } 15 else { 16 ++i; 17 flag=1; 18 } 19 } 20 printf("%d\n",result); 21 return 0; 22 }
1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 int n,result=1,m=2; 6 scanf("%d",&n); 7 while(m<=n) 8 { 9 if(n%m==0){ 10 result *=m; 11 while(n%m==0) 12 n /= m; 13 } 14 m++; 15 } 16 printf("%d\n",result); 17 return 0; 18 }
23、)輸入行數 n,及首個小寫字母,輸出三角形字母圖 形,如果輸出超過 z,再回到 a,依次循環下去。
1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 int n,i,j,k; 6 char c; 7 scanf("%d,%c",&n,&c); 8 for(i=0; i<n-1; ++i) 9 { 10 for(j=n-i-1; j>0; --j){ 11 printf("-"); 12 } 13 if(c>'z') c = 'a'; 14 putchar(c++); 15 if(i){ 16 for(int k=(i-1)*2+1;k>0; k--) 17 printf("-"); 18 if(c>'z') c = 'a'; 19 putchar(c++); 20 } 21 printf("\n"); 22 } 23 for(int m=0; m<n+n-1;++m){ 24 if(c>'z') c = 'a'; 25 putchar(c++); 26 } 27 return 0; 28 }
24、Z型字母圖形
題目內容:
輸入行數n,及首個小寫字母,輸出Z型字母圖形,如果輸出超過z,再回到a,依次循環下去。
輸入格式:
3,b
輸出格式:
bcd
e
fgh【回車】
輸入樣例:
3,b
輸出樣例:
bcd
e
fgh
1 #include <stdio.h> 2 int main() 3 { 4 int n, i, j; 5 char c; 6 scanf("%d,%c", &n, &c); 7 for(i=0; i<n; i++) 8 { 9 for(j=0; j<n; j++) 10 { 11 if(i==0 || i==n-1) 12 printf("%c",c++); 13 else if(i+j==n-1) 14 printf("%c",c++); 15 else if(i+j<n-1) 16 printf(" "); 17 if(c>'z') c='a'; 18 } 19 printf("\n"); 20 } 21 return 0; 22 }
25、特殊數字
題目內容:
給定一個區間,輸出其中前半部分數字之和等於后半部分數字之和的數,沒有則輸出No output。
輸入格式:
120 130
輸出格式:
121【回車】
輸入樣例:
120 130
輸出樣例:
121
1 #include <stdio.h> 2 3 int divide(int i,int *num) 4 { 5 int index = 0; 6 while(i) 7 { 8 num[index++] = i%10; 9 i = i/10; 10 } 11 return index; 12 } 13 14 int isEqual(int *num,int index) 15 { 16 int i,sum1,sum2; 17 sum1 = sum2 = 0; 18 for(i=0; i<index/2; i++) 19 { 20 sum1 += num[i]; 21 sum2 += num[index-i-1]; 22 } 23 return (sum1 == sum2); 24 } 25 26 int main() 27 { 28 int a=120,b=130,index,num[12],flag=0; 29 scanf("%d%d",&a,&b); 30 for(int i=a;i<=b;i++) 31 { 32 index = divide(i,num); 33 if(isEqual(num,index)) 34 { 35 flag = 1; 36 printf("%d\n",i); 37 } 38 } 39 if(!flag) 40 puts("No output"); 41 return 0; 42 }
26、連續子串
題目內容:
從鍵盤輸入一個字符串,按從左向右順序分解成連續字符組成的幾個子串,並順序輸出這些子串
輸入格式:
bxyzacdefgh345x
輸出格式:
xyz
cdefgh
345【回車】
輸入樣例:
bxyzacdefgh345x
輸出樣例:
xyz
cdefgh
345
1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 char s[256] = ""; 6 int count=0; 7 gets(s); 8 for( int i=0; s[i]; ++i) 9 { 10 if(s[i+1]&&s[i+1]==s[i]+1) count++; 11 else if(count>=1) 12 { 13 char t[256] = ""; 14 printf("%s\n",strncpy(t,&s[i-count],count+1)); 15 count=0; 16 } 17 } 18 return 0; 19 }
1 #include <stdio.h> 2 //#include <string.h> 3 int main() 4 { 5 char s[256] = ""; 6 char t[256] = ""; 7 int count=0; 8 gets(s); 9 for( int i=0; s[i]; ++i) 10 { 11 if(s[i+1]&&s[i+1]==s[i]+1) t[count++] = s[i]; 12 else if(count>=1) 13 { 14 t[count++] = s[i]; 15 t[count] = '\0'; 16 printf("%s\n",t); 17 count=0; 18 } 19 } 20 return 0; 21 }
27、
下面程序的功能是將字符串s的所有字符傳送到字符串t中,要求每傳遞三個字符后再存放一個空格,例如字符串s為"abcdefg",則字符串t為"abc def g"。
1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 int j, k=0; 6 char s[60], t[100], *p; 7 p=s; 8 gets(p); 9 while(*p) 10 { 11 for (j=1; j<=3 && *p; p++,k++,j++) t[k]=*p; 12 13 if (j==4) { t[k]=' '; k++;}//*p 14 15 } 16 t[k]='\0'; 17 puts(t); 18 return 0; 19 }
28、下面程序的功能是實現數組元素中值的逆轉
1 #include <stdio.h> 2 #include <string.h> 3 void invert(int *s,int num) 4 { 5 int *t,k; 6 t=s+num; 7 while(s<t) 8 { 9 k=*s; 10 *s=*t; 11 *t=k; 12 s++; 13 t--; 14 } 15 } 16 int main() 17 { 18 int i,n=10,a[10]={1,2,3,4,5,6,7,8,9,10}; 19 invert(a,n-1); 20 for(i=0; i<10; i++) 21 printf("%4d",a[i]); 22 printf("\n"); 23 return 0; 24 }
29、建立一個包含30個元素的循環鏈表
1 #include <stdio.h> 2 #include <string.h> 3 #include <malloc.h> 4 5 struct node{ 6 int no; 7 struct node* next; 8 }; 9 10 int main() 11 { 12 int i, k; struct node *head, *p, *q; 13 head = (struct node *)malloc(sizeof(struct node)); 14 head->no = -1; 15 head->next = head; 16 for ( i=30; i>0; i-- ) /* 生成循環鏈表 */ 17 { 18 p = (struct node *)malloc(sizeof(struct node)); 19 p->next = head->next; p->no = i; head->next = p; 20 } 21 22 q = head->next; 23 while(q->no!=-1) 24 { 25 printf("%d ",q->no); 26 q = q->next; 27 } 28 return 0; 29 }
30、下面程序的功能是將磁盤上的一個文件復制到另一個文件中,兩個文件名在命令行中給出(假定給定的文件名無誤)
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(int argc,char *argv[]) 4 { 5 FILE *fin,*fout; 6 if(argc< 3 ) 7 { 8 printf("The command line error! "); 9 exit(0); 10 } 11 12 fin=fopen(argv[1], "r"); 13 14 fout=fopen(argv[2], "w"); 15 16 while(!feof(fin)) 17 fputc(fgetc(fin), fout ); 18 fclose(fin); 19 fclose(fout); 20 return 0; 21 }