1、5*5数组中找出一行中最大的数,同时也是一列中最大的数。
#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 5 int main(){ srand((unsigned)time(NULL)); int i,j; int a[5][5]; int row; int col; int max; int found=0; do{ for(i=0;i<N;i++){ for(j=0;j<N;j++){ a[i][j]=rand()%100; } } for(i=0;i<N;i++){ for(j=0;j<N;j++){ printf("%4d",a[i][j]); } printf("\n"); } for(i=0;i<N;i++){ max=a[i][0]; row = i; col = 0; for(j = 0;j < N;j++){ if(max<a[i][j]){ max = a[i][j]; col = j; } } for(j=0;j<N;j++){ if(max>a[j][col]){ break; } } if(j == N){ found = 1; printf("num is %d, row = %d,col = %d\n",max,row,col); } }
}while(found == 0);//一直循环到找到saddle为止 if(found==0){ printf("not found saddle\n"); } return 0; }
2、字符串逆序
#include<stdio.h> int main(){ void verse(char *a); char a[20]; scanf("%[^\n]",a); verse(a); printf("%s\n",a); return 0; } void verse(char *a){ int i = 0; int j = 0; int t; while(a[i]!='\0'){ i++; } i--; while(j<i){ t = a[j]; a[j] = a[i]; a[i] = t; j++; i--; } }
3、整数转为任意进制的字符串输出。
#include<stdio.h> int main(){ void verse(char *a); char two(int n,char *a); char eight(int n,char *a); char ten(int n,char *a); char sixteen(int n,char *a); int n; char a[10]; char b[10]; char c[10]; char d[10]; printf("please input a number:"); scanf("%d",&n); two(n,a); eight(n,b); ten(n,c); sixteen(n,d); printf("%s\n",a); printf("%s\n",b); printf("%s\n",c); printf("%s\n",d); return 0; } void verse(char *a){ int i = 0; int j = 0; int t; while(a[i]!='\0'){ i++; } i--; while(j<i){ t = a[j]; a[j] = a[i]; a[i] = t; j++; i--; } } char two(int n,char *a){ int i = 0; while(n!=0){ a[i] = (n % 2) + '0'; i++; n = n / 2; } verse(a); } char eight(int n,char *a){ int i = 0; while(n != 0){ a[i] = (n % 8) + '0'; i++; n = n / 8; } verse(a); } char ten(int n,char *a){ int i = 0; while(n != 0){ a[i] = (n % 10) + '0'; i++; n = n / 10; } verse(a); } char sixteen(int n,char *a){ int i = 0; while(n != 0){ switch(n%16){ case 0: a[i] = 0+'0';break; case 1: a[i] = 1+'0';break; case 2: a[i] = 2+'0';break; case 3: a[i] = 3+'0';break; case 4: a[i] = 4+'0';break; case 5: a[i] = 5+'0';break; case 6: a[i] = 6+'0';break; case 7: a[i] = 7+'0';break; case 8: a[i] = 8+'0';break; case 9: a[i] = 9+'0';break; case 10: a[i] = 'A';break; case 11: a[i] = 'B';break; case 12: a[i] = 'C';break; case 13: a[i] = 'D';break; case 14: a[i] = 'E';break; case 15: a[i] = 'F';break; } i++; n = n / 16; } verse(a); }
4、
标签:int,while,C语言,char,verse,关于,printf,习题,include From: https://www.cnblogs.com/gunancheng/p/17429567.html