1、C语言基础题四十一.在N个学生姓名中查找输入的姓名。#include#includeint main(void) char name10020,find20; int N,count=0,i; printf(Please input int N:); scanf(%d,&N); printf(Please input N names:n); for(i=0;i=N;i+) gets(namei); printf(Please input the name whitch you want to find:n); gets(find); for(i=0;i=N;i+) if(strcmp(find
2、,namei)=0) printf(Find it!nThe number is %dn,i); count+; if(count=0) printf(Not found!Please check your input!n); return -1; return 0;四十二、使用选择排序对数组int aN进行排序并输出。#include void bubble_sort(int *a,int N);int main (void) int a100; int N,i; printf(Please input the value of N:n); scanf(%d,&N); printf (ple
3、ase input int aN:n); for (i = 0; i N; i+) scanf (%d, &ai); bubble_sort(a,N); printf(The result isn); for (i = 0; i N; i+) printf (%d , ai); printf (n); return 0;void bubble_sort(int *a,int N) int i,j,box; for(i = 0; i N-1; i+) for (j = 0; j aj+1) box = aj+1; aj+1 = aj; aj = box; 四十三、使用冒泡排序对数组int aN进
4、行排序并输出。#includevoid select_sort(int *a,int n);int main(void) int N,i; int a100; printf(Please input the value of N:n); scanf(%d,&N); printf(Please input your number:n); for(i=0;iN;i+) scanf(%d,&ai); select_sort(a,N); printf(The result isn); for(i=0;iN;i+) printf(%d ,ai); printf(n); return 0;void sel
5、ect_sort(int *a,int n) int i,m,x; int min,min_m; for(m=0;mn-1;m+) min=am; min_m=m; for(i=m;in;i+) if(aimin) min=ai; min_m=i; x=amin_m; amin_m=am; am=x; 44、使用插入排序对数组int aN进行排序并输出。#includevoid insert_sort(int *a,int n);int main(void) int a100,i,N; printf(Please input the value of N:n); scanf(%d,&N); p
6、rintf(Please input your number:n); for(i=0;iN;i+) scanf(%d,&ai); insert_sort(a,N); printf(The result isn); for(i=0;iN;i+) printf(%d ,ai); printf(n); return 0;void insert_sort(int *a,int n) int i,j; int t; for(i = 1; i 0 & *(a + j - 1) t;j-) *(a + j) = *(a + j - 1); *(a + j) = t; 45、将N个国家的英文名字,按字典顺序输
7、出。#include#includevoid sort(char *a,int n);int main(void) int i,N; char name10030,*p100; printf(Please input int N:); scanf(%d,&N); printf(Please input N names:n); for(i=0;i=N;i+) gets(namei); pi=namei; sort(p,N); printf(The result of sort is); for(i=0;i=N;i+) printf(%sn,pi); return 0;void sort(char
8、 *a,int n) int i,j,k; char *t; for(i = 0; i = n-1; i+) for (j = 0; j 0) t = aj+1; aj+1 = aj; aj = t; 46、在有序(升序)数组int aN插入一个输入的整数num,使数组仍有序。#include void insert_sort(int *a,int N);void insert(int *a,int N,int x);int main (void) int a100; int N,i,num; printf(Please input the value of N:n); scanf(%d,&N
9、); printf (please input int aN:n); for (i = 0; i N; i+) scanf (%d, &ai); insert_sort(a,N); printf(The result isn); for (i = 0; i N; i+) printf (%d , ai); printf (n); printf(Please input the int number whitch you want to insertn); scanf(%d,&num); insert(a,N,num); printf(The result isn); for (i = 0; i
10、 = N; i+) printf (%d , ai); return 0;void insert_sort(int *a,int n) int i,j; int t; for(i = 1; i 0 & *(a + j - 1) t;j-) *(a + j) = *(a + j - 1); *(a + j) = t; void insert(int *a,int N,int x) aN=x; insert_sort(a,N+1);47、删除字符串中指定的字符。#include#includeint main(void) int count=0,i,j,k,l1,l2; char c20,s200
11、; puts(Input string); gets(s); puts(Input the characters which you want to delete); gets(c); l1=strlen(s); l2=strlen(c); for(i=0;il1-count;i+) for(j=0;jl2;j+) if(cj=si) for(k=i+1;kl1-count;k+,i+) si=sk; count+; i=0; sl1-count=0; printf(The result isn%sn,s); return 0;48、在有序数组int aN中折半查找输入的整数num。#incl
12、udeint main(void) int a100,N,x,i,top,end,count=0; printf(Input int Nn); scanf(%d,&N); printf(Please input N numbersn); for(i=0;iN;i+) scanf(%d,&ai); printf(please enter the number which you want to findn); scanf(%d,&x); for(top=0,end=N-1;topai) end = i-1; else top=i+1; if(count=0) printf(Not found!n
13、); return -1; return 0;49、编写函数计算字符串长度。#includeint slen(char *s);int main(void) char s20; printf(Input string:n); scanf(%s,s); printf(Length is %dn,slen(s); return 0;int slen(char *s) int i=0,n=0; while(si+!=0) n+; return n;50、编写函数实现两个字符串连接。#includevoid cats(char *s1,char *s2);int main(void) char s11
14、00,s2100; printf(Input string 1n); scanf(%s,s1); printf(Input string 2n); scanf(%s,s2); cats(s1,s2); printf(The result is n%sn,s1); return 0;void cats(char *s1,char *s2) while(*s1) *s1+; while(*s1+=*s2+);51、编写函数实现字符串复制。#includevoid scopy(char *s1,char *s2);int main(void) char s1100,s2100; printf(Inp
15、ut stringn); scanf(%s,s2); scopy(s1,s2); printf(The result isn%sn,s1); return 0;void scopy(char *s1,char *s2)while(*s1+=*s2+);52、编写函数两个字符串比较。#includeint scmp(char *s1,char *s2);int main(void) char s1100,s2100,c; int r; printf(Input string 1n); scanf(%s,s1); printf(Input string 2n); scanf(%s,s2); r=s
16、cmp(s1,s2); switch(r) case -1:c=;break; printf(The result isnstring 1 %c string 2n,c); return 0;int scmp(char *s1,char *s2) int i; while(*s1&*s2) if(*s1*s1) return 1; else if(*s1*s1) return 1; else if(*s1*s2) return -1;53、删除字符串中指定的一个字符。#include#includeint main(void) int count=0,i,j,l; char c,s20; pu
17、ts(Input string); gets(s); puts(Input the character which you want to delete); scanf(%c,&c); l=strlen(s); for(i=0;il-count;i+) if(c=si) for(j=i+1;jl-count;j+,i+) si=sj; count+; i=0; sl-count=0; printf(The result isn%sn,s); return 0;54、判断一个字符串是否为回文(顺读和倒读相同)。#include#includeint main(void) char s120,s2
18、20; int i,j=0,l; puts(Input a string:); gets(s1); l=strlen(s1); for(i=l-1,j=0;i=0&jl;i-,j+) s2j=s1i; s2j=0; if(strcmp(s1,s2)=0) puts(The string is a palindrome); else puts(The string is not a palindrome); return 0;55、编写一个函数,将第一个字符串中有而第二个字符串中无的字串返回。#include#includechar *delret(char *s1,char *s2);int
19、main(void) char s120,s220,*s; puts(Input string 1); gets(s1); puts(Input string 2); gets(s2); s=delret(s1,s2); printf(The result isn%sn,s); return 0;char *delret(char *s1,char *s2) int count=0,i,j,k,l1,l2; l1=strlen(s1); l2=strlen(s2); for(i=0;il1-count;i+) for(j=0;jl2;j+) if(s2j=s1i) for(k=i+1;kl1-
20、count;k+,i+) s1i=s1k; count+; i=0; s1l1-count=0; return s1;56、编写一个函数,将两个字符串的字符排序后输出#include#includevoid strcat_sort(char *s1,char *s2);int main(void) char s120,s220; puts(Input string 1); gets(s1); puts(Input string 2); gets(s2); strcat_sort(s1,s2); return 0;void strcat_sort(char *s1,char *s2) int i
21、,j,N; char box; strcat(s1,s2); N=strlen(s1); for(i = 0; i N-1; i+) for (j = 0; j s1j+1) box = s1j+1; s1j+1 = s1j; s1j = box; puts(The result is:); puts(s1);57、统计字符串中的单词(单词之间用一个或多个空格分隔)。#include#includeint cnt_wrd( char *s);int main(void) char s100; puts(Input a string:); gets(s); printf(The quantity
22、 of words is %d,cnt_wrd(s); return 0;int cnt_wrd( char *s) int i=1,c=1; while(si) if(si= &si-1!= ) c+; i+; return c;58、找出二维数组的鞍点(鞍点是指行上最大,在列上最小的元素),如果有输出鞍点数据和位置。#includestruct saddle_point int flag; int i; int j; int value;int main(void) struct saddle_point sp; int a100100; int i,j,m,n,k; printf(请输入
23、行数和列数:); scanf(%d%*c%d,&m,&n); printf(请输入%d%d二维数组:n,m,n); for(i=0;im;i+) for(j=0;jn;j+) scanf(%d,&aij); for(i=0;im;i+) sp.value=ai0; sp.j=0; for(j=0;jsp.value) sp.value=aij; sp.j=j; sp.flag=1; for(k=0;kaksp.j) sp.flag=0; break; if(sp.flag=1) sp.i=i; printf(鞍点为a%d%d=%dn,sp.i+1,sp.j+1,sp.value); break
24、; if(sp.flag!=1) printf(没有鞍点n); return 0;59、实现int aMN矩阵的转置。#includeint main(void) int a100100; int M,N; int i,j,t; printf(Please input int M N:n); scanf(%d%*c%d,&M,&N); printf(Please input %d*%dMatrix:n,M,N); for(i=0;iM;i+) for(j=0;jN;j+) scanf(%d,&aij); for(i=0;iM;i+) for(j=i+1;jN;j+) t=aij; aij=aji; aji=t; printf(The result is:n); for(i=0;iN;i+) for(j=0;jM;j+) printf(%dt,aij); printf(n); return 0
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1