7-1:
#include <stdio.h> #define Maxs 1001 int findmax(int a[], int N); int main() { int count[Maxs] = {0}; int n; scanf("%d", &n); int x; while ((scanf("%d", &x))!=EOF) { count[x]++; } int maxi = findmax(count, n + 1); for (int i = 0; i < n + 1; i++) { if (count[maxi] == count[i]) { printf("%d %d\n", i, count[i]); } } return 0; } int findmax(int a[], int N) { int maxx = 0; for (int i = 0; i < N; i++) { if (a[maxx] < a[i]) { maxx = i; } } return maxx; }
7-3:错因:只会转一圈,不会往里拐
#include <stdio.h> #include <stdlib.h> int main() { int n,ii,jj; scanf("%d",&n); int k=1; int a[20][20]; int i0,i1,j0,j1,x; i0=0;i1=n-1; j0=0;j1=n-1; for(x=0;x<=n/2;x++) { for(ii=i0,jj=j1;ii<=i1;ii++) { a[ii][jj]=k; k++; } for(jj=j1-1,ii=i1;jj>=j0;jj--) { a[ii][jj]=k; k++; } for(ii=i1-1,jj=j0;ii>=i0;ii--) { a[ii][jj]=k; k++; } for(ii=i0,jj=j0+1;jj<=j1-1;jj++) { a[ii][jj]=k; k++; } i0=i0+1,i1=i1-1,j0=j0+1,j1=j1-1; } for(ii=0;ii<n;ii++) { for(jj=0;jj<n;jj++) printf("%4d",a[ii][jj]); printf("\n");} return 0; }