1

我的 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。

4

2 回答 2

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 

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

于 2014-12-04T19:27:02.540 回答
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] ); 
    }
}
于 2014-12-04T19:40:37.210 回答