1.猜数字游戏
#include<stdio.h>#include<stdlib.h>#include<time.h>int main(){ int ret; int num=0; srand((unsigned int)time(NULL)); ret=rand()%100; scanf("%d",&num); while(num!=ret) { if(num>ret) { printf("too big!\n"); } if(num<ret) { printf("too small!\n"); } scanf("%d",&num); } printf("right!"); return 0;}2.binsearch#include<stdio.h>int binsearch(int a,int arr[],int left,int right)
{ int mid; mid=left-(left-right)/2; while(left!=right) { if(a<=arr[mid]) { if(a==arr[mid]) { return mid; } else right=mid-1; } else if(a>arr[mid]) { left=mid+1; } mid=left-(left-right)/2; } if(a==arr[mid]) return mid; else return -1;}int main(){ int arr[]={1,3,5,7,8,10}; int left=0; int right=sizeof(arr)/sizeof(arr[0])-1; int temp=binsearch(3,arr,left,right); printf("%d ",temp); return 0;}3.输出行号#include<stdio.h>int main()
{ char ch; int i=1;while((ch=getchar())!=EOF){ printf("%d ",i++); while((ch=getchar())!='\n') { putchar(ch); } printf("\n");} return 0;}4.找出最大和最小值#include<stdio.h>int main()
{ int arr[]={1,2,3,4,5,6,7,8,9,10}; int max; int min; int i; max=arr[0]; min=arr[0]; for(i=1;i<10;i++) { if(max<=arr[i]) max=arr[i]; } for(i=1;i<10;i++) { if(min>=arr[i]) min=arr[i]; } printf("max=%d min=%d",max,min); return 0;}5.组合无重复数#include<stdio.h>int main()
{ int i,j,m; for(i=1;i<5;i++) { for(j=1;j<5;j++) { if(j==i) continue; for(m=1;m<5;m++) {if(m==i||m==j) continue; printf("%d ",i*100+j*10+m); } } } return 0;}