C语言基础编程试题案例.pdf
1、数组元素逆序输出;/1.#include void print(int*arr_p,int n)int i;for(i=n-1;i=0;i-)printf(%d ,arr_pi);int main()int n,i;scanf(%d,&n);int arrn;for(i=0;i n;i+)scanf(%d,&arri);print(arr,n);return 0;2、数组元素逆序保存;/2.#include void turn_save(int*arr_p,int len)int i,temp;for(i=0;i len/2;i+)temp=arr_pi;arr_pi=arr_plen-i-1;arr_plen-i-1=temp;int main()int n,i;scanf(%d,&n);int arrn;for(i=0;i n;i+)scanf(%d,&arri);turn_save(arr,n);for(i=0;i n;i+)printf(%d,arri);return 0;3、数组元素删除;/3.#include int delete(int*arr,int len,int delete_num)int i,j;for(i=0;i len;i+)if(delete_num=arri)for(j=i+1;j len;j+)arrj-1=arrj;len-;i-;return len;int main()int arr=-1,-1,1,1,3,4,4,5,7,9,9;int i;int len=sizeof(arr)/sizeof(arr0);int delete_num;scanf(%d,&delete_num);len=delete(arr,len,delete_num);for(i=0;i len;i+)printf(%d,arri);return 0;4、数组元素插入;4./notice#include void insert(int*arr,int len,int insert_num)int i,j;for(i=0;i len-1;i+)if(insert_num=i;j-)arrj+1=arrj;arri=insert_num;break;if(insert_num=arrlen-2)arrlen-1=insert_num;int main()int arr8=-2,1,3,5,7,9,11;int len=sizeof(arr)/sizeof(arr0);int i;int insert_num;scanf(%d,&insert_num);insert(arr,len,insert_num);for(i=0;i len;i+)printf(%d,arri);return 0;5 数组元素循环 n 个;5.#include void circulate(int*arr,int len,int n)int i,count;int temp;for(count=0;count=0;i-)arri+1=arri;arr0=temp;int main()int arr=1,2,3,4,5,6;int len=sizeof(arr)/sizeof(arr0);int i;int n;scanf(%d,&n);circulate(arr,len,n);for(i=0;i len;i+)printf(%d,arri);return 0;6.冒泡排序 6.#include void bubble_sort(int*arr,int len)int i,j;int temp;for(i=0;i len-1;i+)for(j=0;j arrj+1)temp=arrj+1;arrj+1=arrj;arrj=temp;int main()int arr=3,1,5,2,0;int len=sizeof(arr)/sizeof(arr0);bubble_sort(arr,len);int i;for(i=0;i len;i+)printf(%d,arri);return 0;8.选择排序 8.#include void shell_sort(int*arr,int len)int i,j,temp,min_index;for(i=0;i len;i+)min_index=i;for(j=i+1;j len;j+)if(arrjarrmin_index)min_index=j;temp=arri;arri=arrmin_index;arrmin_index=temp;int main()int arr=3,6,1,0,5;int len=sizeof(arr)/sizeof(arr0);shell_sort(arr,len);int i;for(i=0;i len;i+)printf(%d,arri);return 0;9.字符串 strcpy 9.#include void mystrcpy(char*dst,const char*src)int i=0;while(srci!=0)dsti=srci;i+;dsti=0;int main()char*src=hello world;char dst100=0;mystrcpy(dst,src);printf(%s,dst);return 0;10.字符串 strcmp 10.#include int mystrcmp(const char*s1,const char*s2)int i=1;while(s1i!=0&s2i!=0)if(s1i!=s2i)return s1i-s2i;i+;return s1i-s2i;int main()char*s1=hello world;char*s2=hello worldyou;printf(%d,mystrcmp(s1,s2);return 0;12、binshow 函数#include void binshow(int num,int len)int i;for(i=len-1;i=0;i-)printf(%d,0 x1&(numi);printf(n);int main()int num,len;scanf(%d%d,&num,&len);binshow(num,len);return 0;13.字符串 strchr#include char*mystrchr(const char*str,int c)while(*str!=0)if(*str=c)return str;str+;return NULL;int main()char*str=hello world;char key=l;char*p=mystrchr(str,key);printf(index=%ldn,p-str);return 0;14 字符串 strrchr 14.#include char*mystrrchr(const char*str,int c)char*p=NULL;while(*str!=0)if(*str=c)p=str;str+;return p;int main()char*str=hello world;char key=l;char*p=mystrrchr(str,key);printf(index=%ldn,p-str);return 0;