1.冒泡
#include<stdio.h> void sort(int a[],int n); int main(void) { int n,a[8]; int i; printf("enter n(n<=8):"); scanf("%d",&n); printf("enter a [%d]:",n); for(i=0;i<n;i++) scanf("%d",&a[i]); sort(a,n); printf("after sorted,a[%d]=",n); for(i=0;i<n;i++) printf("%3d",a[i]); return 0; } void sort(int a[],int n) { int i,j,t; for(i=1;i<n;i++) for(j=0;j<n-i;j++) if(a[j]>a[j+1]){ t=a[j];a[j]=a[j+1];a[j+1]=t; } }
2.查找字符
#include <stdio.h> int search(int list[],int n,int x) { int i; for(i=0;i<n;i++) if(list[i]==x) return i; return -1; } int main(void) { int list[10],n,x,i; printf("enter a n(1<=n<=10):"); scanf("%d",&n); printf("enter %d numbes: ",n); for(i=0;i<n;i++) scanf("%d",&list[i]); printf("enter x:"); scanf("%d",&x); if(search(list,n,x)==-1) printf("no found!! "); else printf("脚标为%d ",search(list,n,x)); }
3.游戏
#include<stdio.h> int main(void) { int count,i,m,n,no; int num[50]; int *p; printf("enter n,m:"); scanf("%d%d",&n,&m); for(i=0;i<n;i++) num[i]=i+1; p=num; count=no=0; while(no<n-1) { if(*p!=0)count++; if(count==m) { no++; printf("退出的人%d:%d ",no,*p); *p=0; count=0; } p++; if(p==num+n) p=num; } p=num; while(*p==0) p++; printf("最后一个人:%d ",*p); }