5-矩阵实验

旋转的矩阵

旋转的矩阵 总提交数: 2756次通过数: 988次通过率: 35.85% 内存限制: 104857600(BYTE)时间限制: 1000(MS)输入限制: 1000(行)输出限制: 1000(行) 题目描述 给定一个n*m的矩阵,请以顺、逆时针交替旋转的方式打印出每个元素。    Input Format 第一行n m; 0<n,m<100 后n行,每行m个整数。 Output Format n*m个矩阵元素,空格隔开。 Example Input 4 4 1  2  3  4 12 13 16  5 11 14 15  6 10  9  8  7 Output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Input 3 4 1  2  3  4 10 11 12  5 9  8  7  6   Output 1 2 3 4 5 6 7 8 9 10 11 12 说明 样例输入输出 样例1 输入: 1 3 3 4 1 输出: 3 4 1 样例2 输入: 4 4 1  2  3  4 12 13 16  5 11 14 15  6 10  9  8  7 输出: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 样例3 输入: 3 1 6 5 7 输出: 6 5 7

//
// Created by w_xing on 2021/4/6.
//

#include <iostream>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    int num = n*m;
    int *matrix = new int [num];
    for (int i=0; i<num; i++) {
        cin >> matrix[i];
    }
    // 数组下标映射关系 int arr[i][j] = int arr[i*m + j]
    int count = 0;
    int i, j;
    int left = 0, right = m-1, top = 0, bottom = n-1; //定位
    while (count<num) {
        //顺时针输出
        if (left < right && top < bottom) {
           for (i=top, j=left; j<right; j++) { //从左到右
               cout << matrix[i*m + j] << " ";
               count++;
           }
            for (j=right; i<bottom; i++) { //从上到下
                cout << matrix[i*m + j] << " ";
                count++;
            }
            for (i=bottom; j>left; j--) { //从右到左
                cout << matrix[i*m + j] << " ";
                count++;
            }
            for (j=left; i>top; i--) { //从下到上
                cout << matrix[i*m + j] << " ";
                count++;
            }
           left++; right--; top++; bottom--;
        }
        //逆时针输出
        if (left < right && top < bottom) {
            //逆时针
            for (i=top, j=left; i<bottom; i++) { //从上到下
                cout << matrix[i*m + j] << " ";
                count++;
            }
            for (i=bottom; j<right; j++) { //从左到右
                cout << matrix[i*m + j] << " ";
                count++;
            }
            for (j=right; i>top; i--) { //从下到上
                cout << matrix[i*m + j] << " ";
                count++;
            }
            for (i=top; j>left; j--) { //从右到左
                cout << matrix[i*m + j] << " ";
                count++;
            }
            left++; right--; top++; bottom--;
        }
        if (left == right) {
            for (i=top,j=left; i<=bottom; i++) {
                cout << matrix[i*m + j] << " ";
                count++;
            }
        }
        if (top == bottom) {
            for (i=top,j=left; j<=right; j++) {
                cout << matrix[i*m + j] << " ";
                count++;
            }
        }
    }
    delete[] matrix;
    return 0;
}

封装成函数

#include <iostream>
using namespace std;

// 顺时针打印函数
void clockwise(int* data, int beginrow, int r, int c) {
    int left = beginrow, right = c - beginrow - 1;
    int top = beginrow, bottom = r - beginrow - 1;
    
    if (left > right || top > bottom) return;

    // 从左到右
    for (int i = left; i <= right; i++) {
        cout << data[top * c + i] << " ";
    }
    // 从上到下
    for (int i = top + 1; i <= bottom; i++) {
        cout << data[i * c + right] << " ";
    }
    // 从右到左(避免重复行)
    if (top < bottom) {
        for (int i = right - 1; i >= left; i--) {
            cout << data[bottom * c + i] << " ";
        }
    }
    // 从下到上(避免重复列)
    if (left < right) {
        for (int i = bottom - 1; i > top; i--) {
            cout << data[i * c + left] << " ";
        }
    }
}

// 逆时针打印函数
void anticlockwise(int* data, int beginrow, int r, int c) {
    int left = beginrow, right = c - beginrow - 1;
    int top = beginrow, bottom = r - beginrow - 1;
    
    if (left > right || top > bottom) return;

    // 从上到下
    for (int i = top; i <= bottom; i++) {
        cout << data[i * c + left] << " ";
    }
    // 从左到右
    for (int i = left + 1; i <= right; i++) {
        cout << data[bottom * c + i] << " ";
    }
    // 从下到上(避免重复列)
    if (left < right) {
        for (int i = bottom - 1; i >= top; i--) {
            cout << data[i * c + right] << " ";
        }
    }
    // 从右到左(避免重复行)
    if (top < bottom) {
        for (int i = right - 1; i > left; i--) {
            cout << data[top * c + i] << " ";
        }
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    int* data = new int[n * m];

    // 读取矩阵数据
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> data[i * m + j];
        }
    }

    int beginrow = 0;
    bool isClockwise = true;

    while (beginrow * 2 < n && beginrow * 2 < m) {
        if (isClockwise) {
            clockwise(data, beginrow, n, m);
        } else {
            anticlockwise(data, beginrow, n, m);
        }
        beginrow++;
        isClockwise = !isClockwise;
    }

    delete[] data;
    return 0;
}

判断布尔矩阵的奇偶性

题目描述

布尔矩阵是指由0和1组成的矩阵,一个布尔矩阵有奇偶均势特性,当且仅当矩阵的每行、每列得总和为偶数,即包含偶数个1,该矩阵就具有奇偶均势特性。如下面这个4*4的矩阵就具有奇偶均势特性:

1 0 1 0

0 0 0 0

1 1 1 1

0 1 0 1

每一行的和分别为2,0,4,2,每一列的和分别为2,2,2,2。

编写程序,读入一个n*n阶矩阵并检查它是否具有奇偶均势特性。如果没有,你的程序应当再检查一下它是否可以通过修改一位(把0改为1,把1改为0)来使它具有奇偶均势特性。如果不可能,这个矩阵就被认为是破坏了。

输入:

第一行是一个整数n ( 0< n < 100 ),代表该矩阵的大小。然后输入n 行,每行n个整数(0或1),由空格分隔。

输出:

如果矩阵具有奇偶均势特性,输出“OK”;如果能通过只修改该矩阵中的一位来使它具有奇偶均势特性,则输出“Change bit (i,j)”,这里i和j是被修改的元素的行与列(行,列号从1开始);否则,输出“Corrupt”  

#include <iostream>
using namespace std;

const int N = 105;
int a[N][N];

bool checkRow(int n) {
    for (int i = 0; i < n; i++) {
        int sum = 0;
        for (int j = 0; j < n; j++) {
            sum += a[i][j];
        }
        if (sum % 2 != 0) {
            return false;
        }
    }
    return true;
}

bool checkCol(int n) {
    for (int j = 0; j < n; j++) {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += a[i][j];
        }
        if (sum % 2 != 0) {
            return false;
        }
    }
    return true;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> a[i][j];
        }
    }

    if (checkRow(n) && checkCol(n)) {
        cout << "OK" << endl;
    } else {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                a[i][j] = 1 - a[i][j];
                if (checkRow(n) && checkCol(n)) {
                    cout << "Change bit (" << i + 1 << "," << j + 1 << ")" << endl;
                    return 0;
                }
                a[i][j] = 1 - a[i][j];
            }
        }
        cout << "Corrupt" << endl;
    }

    return 0;
}

三元组表示的稀疏矩阵的乘法运算

题目描述

对两个由三元组表示的稀疏矩阵进行乘法运算

输入:

第一行为三个整数row,column,count,用空格分隔,分别表示稀疏矩阵的行数、列数、非零元素的个数。

第二行开始输入该稀疏矩阵的三元组,每行三个整数,空格分隔,分别代表一个非零元素的行标、列标、值,共输入count行。

空一行后开始输入第二个稀疏矩阵的row,column,count,及三元组。

输出:

如果计算结果为零矩阵,输出“The answer is a Zero Matrix”,如果结果为非零矩阵,输出结果的三元组表示,要求输出三元组有序。如果无法计算矩阵乘法,输出“ERROR”。

#include <iostream>
#include <vector>
#include <map>

using namespace std;

struct Triple {
    int row, col, val;
};

vector<Triple> multiplySparseMatrices(int r1, int c1, vector<Triple>& mat1, int r2, int c2, vector<Triple>& mat2) {
    if (c1 != r2) {
        cout << "ERROR" << endl;
        exit(0);
    }
    
    map<pair<int, int>, int> result;
    map<int, vector<Triple>> mat2ByCol;
    
    for (const auto& t : mat2) {
        mat2ByCol[t.row].push_back(t);
    }
    
    for (const auto& t1 : mat1) {
        if (mat2ByCol.find(t1.col) != mat2ByCol.end()) {
            for (const auto& t2 : mat2ByCol[t1.col]) {
                result[{t1.row, t2.col}] += t1.val * t2.val;
            }
        }
    }
    
    vector<Triple> res;
    for (const auto& kv : result) {
        if (kv.second != 0) {
            res.push_back({kv.first.first, kv.first.second, kv.second});
        }
    }
    return res;
}

int main() {
    int r1, c1, count1, r2, c2, count2;
    cin >> r1 >> c1 >> count1;
    vector<Triple> mat1(count1);
    for (int i = 0; i < count1; i++) {
        cin >> mat1[i].row >> mat1[i].col >> mat1[i].val;
    }
    
    cin >> r2 >> c2 >> count2;
    vector<Triple> mat2(count2);
    for (int i = 0; i < count2; i++) {
        cin >> mat2[i].row >> mat2[i].col >> mat2[i].val;
    }
    
    vector<Triple> result = multiplySparseMatrices(r1, c1, mat1, r2, c2, mat2);
    
    if (result.empty()) {
        cout << "The answer is a Zero Matrix" << endl;
    } else {
        for (const auto& t : result) {
            cout << t.row << " " << t.col << " " << t.val << endl;
        }
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1234哈哈哈哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值