旋转二维整数数组

时间:2014-12-04 18:56:15

标签: c++ arrays for-loop matrix cstdio

我的IT学校的任务有问题。问题是 : 将数组的框架向左转。 输入: 首先得到测试次数(t)。然后对于每个测试得到l和k(行和列),3< = l,k< = 100.然后用来自用户的数字填充矩阵。

Input:
1
3 3
1 2 3
4 5 6 
7 8 9 

Output:
2 3 6 
1 5 9
4 7 8 

到目前为止我的代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>

int main()
{
    int t, w, k;
    int tab[101][101];
    int t1[101], t2[101], t3[101], t4[101];
    scanf_s("%d", &t);
    for (int i = 0; i < t; i++) {
        scanf_s("%d %d", &w, &k);
        for (int j = 0; j < w; j++) {
            for (int x = 0; x < k; x++) {
                scanf_s("%d", &tab[j][x]);
                if (j == 0) {                   //1 linia
                    t1[x] = tab[j][x];          
                }
                if (j + 1 == w) {               //3 linia
                    t2[x] = tab[j][x];
                }
                if (x == 0) {                   //2 linia
                    t3[j] = tab[j][x];
                }
                if (x + 1 == k) {               //4 linia
                    t4[j] = tab[j][x];
                }
            }
        }
    printf("\n");
    }

    for (int j = 0; j < w; j++) {
        for (int x = 0; x < k; x++) {

            if (j == 0) {
                if (x == 0) {
                    tab[j][x] = t3[1];
                }
                else if (x + 1 != k-1) {
                    tab[j][x] = t1[j + 1];
                }
                else if (x + 1 == k-1) {
                    tab[j][x] = t4[1];
                }
            }
            if (j + 1 == w) {
                if (x == 0) {
                    tab[j][x] = t3[k - 2];
                }
                else if (x + 1 == k - 1) {
                    tab[j][x] = t4[w-2];
                }
                else if (x + 1 != k-1) {
                    tab[j][x] = t2[x + 1];
                }
            }
        }
    }

    for (int j = 0; j < w; j++) {
        for (int x = 0; x < k; x++) {
            printf("%d ", tab[j][x]);
        }
        printf("\n");
    }
    printf("\n");
    system("pause");
    return 0;
}

我知道我正在重新定位错误。我现在尝试了5种不同的方法。如果有人会告诉我一种迭代表格向左移动值的方法。我会很感激。还要记住,l不必等于k。

2 个答案:

答案 0 :(得分:0)

你用C ++标记标记了这个问题,虽然我没有看到C ++,除了一些未使用的标题。:)

所以我在C中写了我的示范程序。

如果我理解正确,您需要以下内容。只有我没有输入数组的值。最初设置数组。

#include <stdio.h>

#define N   3

int main( void ) 
{
    int a[N][N] =
    {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    };
    size_t i;

    for ( i = 0; i < N; i++ )
    {
        size_t j;
        for ( j = 0; j < N; j++ ) printf( "%d ", a[i][j] );
        printf( "\n" );
    }

    printf( "\n" );

    int tmp = a[0][0];

    for ( i = 1; i < N; i++ ) a[0][i-1] = a[0][i];
    for ( i = 1; i < N; i++ ) a[i-1][N-1] = a[i][N-1];
    for ( i = 1; i < N; i++ ) a[N-1][N-i] = a[N-1][N-i-1];
    for ( i = 1; i < N - 1; i++ ) a[N-i][0] = a[N-i-1][0];
    a[N-i][0] = tmp;

    for ( i = 0; i < N; i++ )
    {
        size_t j;
        for ( j = 0; j < N; j++ ) printf( "%d ", a[i][j] );
        printf( "\n" );
    }

    return 0;
}

输出

1 2 3 
4 5 6 
7 8 9 

2 3 6 
1 5 9 
4 7 8 

如果您需要,那么您可以根据您的要求修改程序。:)

答案 1 :(得分:0)

这将完成工作,无需存储整个矩阵。 (如果您正在阅读数字,请替换nextNum):

int main() {
    int m;      // # of matrices to read/shift/print

    // Get # of matrices
    m = nextNum();
    for ( ; m>0; m-- ) {    
        int h, w;   // dimensions of matrix
        int s1;     // value read from first column of one row to be printed in next row
        int i, j;   // index variables
        int r[101]; // space to save a row

        // Get matrix dimensions
        h = nextNum();
        w = nextNum();

        // Read top-left value
        s1 = nextNum();

        // Read rest of first row & print it; since we skipped 1st value, is shifted left
        for ( i=1; i<w; i++ ) {
            int n = nextNum();
            printf( "%d ", n );
        }

        // Process each remaining row of input
        for ( i=1; i<h; i++ ) {
            int last = i==h-1 ? 1 : 0;  // = 1 if last row, 0 o/w

            // Read in the row
            for ( j=0; j<w; j++ )
                r[j] = nextNum();

            // Print end of it to finish off previous row;
            //  print start of next w/ value save from start of previous row
            printf( "%d\n%d ", r[w-1], s1 );

            // Print current row (if last row, include the 1st column)
            for ( j=1-last; j<w-1-last; j++ )
                printf( "%d ", r[j] );
            s1 = r[0];
        }
        // Print the second-to-last item of the last row read, 
        printf( "%d\n", r[w-2] ); 
    }
}