无聊的时候出道简单题,看看你们学的好不好
矩阵左转90°:例:
转置前:
1 2 3
4 5 6
7 8 9
转置后:
3 6 9
2 5 8
1 4 7
提示:(先看情况,再做决定是否要提示)
程序能有延展性,即:给一个n*n矩阵,都能实现矩阵左转90°
[ 本帖最后由 pcbaichi 于 2011-8-21 13:14 编辑 ]
#include <stdio.h> #include <string.h> int main(void) { int array[3][3] = {1, 2, 3, 4, 5, 6 ,7 ,8, 9}, tmp[3][3]; int i, j; for(i = 0; i < 3; i++) for(j = 0; j < 3; j++) tmp[2 - j][i] = array[i][j]; memcpy(array, tmp, sizeof(array)); for(i = 0; i < 3; i++) for(j = 0; j < 3 || !printf("\n"); j++) printf("%d ", array[i][j]); return 0; } /* Output: 3 6 9 2 5 8 1 4 7 */
#include<stdio.h> #include<string.h> #define N 4 int main(int argc,char *argv[]) { int a[N][N],temp[N][N]; int i,j; for(i=0;i<N;i++) for(j=0;j<N;j++) scanf("%d",&a[i][j]); for(i=0;i<N;i++) for(j=0;j<N;j++) temp[N-1-j][i]=a[i][j]; memcpy(a,temp,sizeof(a)); for(i=0;i<N;i++) for(j=0;j<N || !printf("\n");j++) printf(" %d",a[i][j]); return 0; }
#include <stdio.h> int main(int argc, char* argv[]) { int a[100][100] = {0}, m, n, i, j; printf("How many columns and rows does the matrix have? "); scanf("%d %d", &m, &n); printf("Input the matrix, seperate the elements with ' '\n"); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf("%d", &a[i][j]); } } printf("\nThe matrix after rotated 90 degrees conter-clockwise is:\n"); for (j = m - 1; j >= 0; j--) { for (i = 0; i < n; i++) { printf("%2d", a[i][j]); } printf("\n"); } return 0; }