例题7-1:
#includeint main(void){ int count[9]; int i,response; for(i=1;i<=8;i++) count[i]=0; for(i=1;i<=10;i++){ printf("input your response:"); scanf("%d",&response); if(response<1||response>8) printf("this is a bad response:%d\n',response:%d\n",response); else count[response]++; } printf("result:\n"); for(i=1;i<=8;i++) printf("%4d%4d\n",i,count[i]); return 0;}
例题7-2:
#includeint main(void){ int i; int fib[10]={ 1,1}; for(i=2;i<10;i++) fib[i]=fib[i-1]+fib[i-2]; for(i=0;i<10;i++){ printf("%6d",fib[i]); if((i+1)%5 == 0) printf("\n"); } return 0;}
例题7-3:
#includeint main(void){ int i,flag,x; int a[5]; printf("Enter 5 integers:"); for(i=0;i<5;i++) scanf("%d",&a[i]); printf("Enter x:"); scanf("%d",&x); flag=0; for(i=0;i<5;i++) if(a[i] == x){ printf("Index is %d\n",i); flag = 1; break; } if(flag == 0) printf("Not Found\n"); return 0;}
运行结果1:
运行结果2:
例题7-4:
#includeint main(void){ int i,index,n; int a[10]; printf("Enter n:"); scanf("%d",&n); printf("Enter %d integers:",n); for(i=0;i
例题7-5:
#includeint main(void){ int i,index,k,n,temp; int a[10]; printf("Enter n:"); scanf("%d",&n); printf("Enter %d integers:",n); for(i=0;i
例题7-6:
#includeint main(void){ int a[10] = { 1,2,3,4,5,6,7,8,9,10}; int low,high,mid,n = 10,x; printf("Enter x: "); scanf("%d",&x); low=0;high=n-1; while(low<=high) { mid = (low+high)/2; if(x == a[mid]) break; else if(x
运行结果1:
运行结果2:
例题7-7:
#includeint main(void){ int col,i,j,row; int a[3][2]; printf("Enter 6 inegers:\n"); for(i=0;i<3;i++) for(j=0;j<2;j++) scanf("%d",&a[i][j]); for(i=0;i<3;i++){ for(j=0;j<2;j++) printf("%4d",a[i][j]); printf("\n"); } row=col=0; for(i=0;i<3;i++) for(j=0;j<2;j++) if(a[i][j]>a[row][col]){ row=i; col=j; } printf("max=a[%d][%d]=%d\n",row,col,a[row][col]); return 0;}
例题7-8:
#includeint main(void){ int i,j; int a[3][2]; for(i=0;i<3;i++) for(j=0;j<2;j++) a[i][j]=i+j; for(i=0;i<3;i++){ for(j=0;j<2;j++) printf("%4d",a[i][j]); printf("\n"); } return 0;}
例题7-9:
#includeint main(void){ int i,j,n,temp; int a[6][6]; printf("Enter n:"); scanf("%d",&n); for(i=0;i
例题7-11:
#includeint main(void){ int i,n,m; char s[10]; printf("Enter a string: "); i=0; while((s[i]=getchar( ))!='\n') i++; s[i]='\0'; for(n=0,m=i-1;n =m) printf("It is a plalindrome\n"); else printf("It is nor a plalindrome\n"); return 0;}
运行结果1:
运行结果2:
例题7-12:
#includeint main(void){ int i,number; char str[10]; printf("Enter a string:"); i=0; while((str[i]=getchar( ))!='\n') i++; str[i]='\0'; number=0; for(i=0;str[i]!='\0';i++) if(str[i]>='0' && str[i]<='9') number=number*10+str[i]-'0'; printf("digit=%d\n",number); return 0;}
例题7-13:
#includeint main(void){ int count,i; char str[80]; printf("Enter a string:"); i=0; while((str[i]=getchar( ))!='\n') i++; str[i]='\0'; count=0; for(i=0;str[i]!='\0';i++) if(str[i]<='9'&&str[i]>='0') count++; printf("count = %d\n",count); return 0;}
例题7-14:
#includeint main(void){ int i,k; char str1[80],str2[80]; long number; printf("Enter a string:"); i=0; while((str1[i]=getchar())!='\n') i++; str1[i]='\0'; k=0; for(i=0;str1[i]!='\0';i++) if (str1[i]>='0'&&str1[i]<='9'||str1[i]>='a'&&str1[i]<='f'||str1[i]>='A'&&str1[i]<='F') { str2[k]=str1[i]; k++; } str2[k]='\0'; number=0; for(i=0;str2[i]!='\0';i++) if(str2[i]>='0'&&str2[i]<='9') number=number*16+str2[i]-'0'; else if(str2[i]>='a'&&str2[i]<='f') number=number*16+str2[i]-'a'+10; else if(str2[i]>='A'&&str2[i]<='F') number=number*16+str2[i]-'A'+10; printf("New string:"); for(i=0;str2[i]!='\0';i++) putchar(str2[i]); printf("\n"); printf("number=%1d\n",number); return 0;}