【问题标题】:c++ program for rotation of matrixc++ 矩阵旋转程序
【发布时间】:2019-06-18 01:18:19
【问题描述】:

如果我的问题不好请见谅,我是新来的

任务: 将给定的矩阵旋转 180 度

输入:

1
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

输出:

16 15 14 13
12 11 10 9
8  7  6 5
4 3 2 1

我的尝试

#include <iostream>
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int a[15][15];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                cin >> a[i][j];
        for (int i = 0; i < n; i++) {
            int p = n - 1;
            for (int j = 0; j <= (n - 1) / 2; j++)
                swap(a[i][j], a[i][p--]);
        }
        for (int j = 0; j < n; j++) {
            int p = n - 1;
            for (int i = 0; i < (n - 1) / 2; i++)
                swap(a[i][j], a[p--][j]);
        }
        cout << "checking if printing all row or not" << endl;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
    }
    return 0;
}

测试用例失败

输入:

10

    220 892 951 241 739 884 66 815 904 660

    85 784 379 346 598 873 716 535 422 902

    579 872 41 870 827 406 910 583 349 896

    771 817 361 591 212 74 199 421 820 176

    496 771 558 954 889 628 126 250 58 156

    683 88 339 58 436 176 57 459 22 394

    43 280 839 618 688 573 493 178 941 504

    420 676 888 935 95 593 616 833 115 210

    339 798 694 515 7 831 103 947 992 724

    195 904 864 51 174 980 960 4 974 509

它的正确输出是:

    509 974 4 960 980 174 51 864 904 195

    724 992 947 103 831 7 515 694 798 339

    210 115 833 616 593 95 935 888 676 420

    504 941 178 493 573 688 618 839 280 43

    394 22 459 57 176 436 58 339 88 683

    156 58 250 126 628 889 954 558 771 496

    176 820 421 199 74 212 591 361 817 771

    896 349 583 910 406 827 870 41 872 579

    902 422 535 716 873 598 346 379 784 85

    660 904 815 66 884 739 241 951 892 220

网上评委这么说

你的代码的输出是:

509 974 4 960 980 174 51 864 904 195

724 992 947 103 831 7 515 694 798 339

210 115 833 616 593 95 935 888 676 420

504 941 178 493 573 688 618 839 280 43

156 58 250 126 628 889 954 558 771 496

但是当我调试我的程序时,我发现整个旋转矩阵是正确的。 我在 C++ 中做错了吗?我是 C++ 新手,请帮忙。

对于输入输出约束: Question Link

【问题讨论】:

  • 阅读ericlippert.com/2014/03/05/how-to-debug-small-programs 了解调试代码的技巧。
  • 请注意,如果您的任务是打印旋转后的矩阵,那么您实际上不必旋转矩阵。您可以使用您现在使用的相同索引转换来直接打印它旋转
  • 为了确定,当您发送代码给在线法官时,您是否删除了:cout &lt;&lt; "checking if printing all row or not" &lt;&lt; endl;?你应该!您的代码应该只打印任务中描述的内容。
  • 最后一个" " 是否可能不应该是输出的一部分?我曾经在这样一个“问题”上浪费了一些时间,只是为了得出一个结论,即在线法官不会教你任何东西,而是如何让在线法官开心

标签: c++ matrix


【解决方案1】:
for (int j = 0; j < n; j++) {
            int p = n - 1;
            for (int i = 0; i < (n - 1) / 2; i++)
                swap(a[i][j], a[p--][j]);
        }

在上面的内部 for 循环中,您错过了“
for (int i = 0; i <= (n - 1) / 2; i++)

您的整个代码将是

#include <iostream>
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int a[15][15];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                cin >> a[i][j];
        for (int i = 0; i < n; i++) {
            int p = n - 1;
            for (int j = 0; j <= (n - 1) / 2; j++)
                swap(a[i][j], a[i][p--]);
        }
        for (int j = 0; j < n; j++) {
            int p = n - 1;
            for (int i = 0; i <= (n - 1) / 2; i++)
                swap(a[i][j], a[p--][j]);
        }
        cout << "checking if printing all row or not" << endl;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    相关资源
    最近更新 更多