鱼C论坛

 找回密码
 立即注册
查看: 2246|回复: 7

[已解决]学校的上机题,交换矩阵,求助各位大神啦

[复制链接]
发表于 2017-12-9 10:59:55 | 显示全部楼层 |阅读模式
3鱼币
这次学校的上机题,想了好久没有思路,求助各位大神!
下面直接上题目:
矩阵交换
【问题描述】
给定m行n列的图像各像素点灰度值,对其依次进行一系列操作后,求最终图像。其中,可能的操作及对应字符有如下四种:
A:顺时针旋转90度    B:逆时针旋转90度     C:左右翻转      D:上下翻转
输入:第一行包含两个整数m和n,表示图像的行数和列数,中间用单个空格隔开。1 <= m <= 100, 1 <= n <= 100。接下来m行,每行n个整数,表示图像中每个像素点的灰度值,相邻两个数之间用单个空格隔开,灰度值范围在0到255之间。接下来一行,包含由A、B、C、D组成的字符串s,表示需要按顺序执行的操作序列。s的长度在1到100之间。
输出:m行,每行包含n个整数,为最终图像各像素点的灰度值。其中m为最终图像的行数,n为最终图像的列数。相邻两个整数之间用单个空格隔开。
【样例输入】
2 3
10   0  10
100 100 10
AC
【样例输入】
10 100
0 100
10 10
请问大家怎么做呀
最佳答案
2017-12-9 10:59:56
楼主大人,在下写了一个
#include <iostream>

using namespace std;
void tranlateA();
void tranlateB();
void tranlateC();
void tranlateD();

int m, n ;
int a[100][100] = {0};
int b[100][100] = {0};

int main(void){

        char c[100], ch;
        
        cin >> m >> n;
        for(int i=0; i<m; i++){
                for(int j=0; j<n; j++){
                        cin >> a[i][j];
                }
        }
        //getchar();
        i = 0;
        while(1){
                ch = getchar();
                if(ch >= 'A' && ch <='D')
                {                
                        c[i] = ch;
                        i++;
                        while((ch = getchar()) != '\n'){
                                c[i] = ch;
                                i++;
                        }
                        break;
                }
        }

        for(int k=0; k<i; k++){
                switch (c[k]){
                case 'A':
                        tranlateA();
                        break;
                case 'B':
                        tranlateB();
                        break;
                case 'C':
                        tranlateC();
                        break;
                case 'D':
                        tranlateD();
                        break;
                }
        }

        for(i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        cout << b[i][j] << " ";
                }
                cout << endl;
        }
        return 0;
}

void tranlateA(){
        //cout << "test A" << endl;
        //顺时针旋转90度
        int temp = 0;
        temp = m; 
        m = n;
        n =temp;

        for(int i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        b[i][j] = a[n-1-j][i];
                }
        }

        for(i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        a[i][j] = b[i][j];
                        //cout << b[i][j] << " ";
                }
        //        cout << endl;
        }
}
void tranlateB(){
        //cout << "test B" << endl;
        //逆时针旋转90度
        int temp = 0;
        temp = m; 
        m = n;
        n =temp;
        for(int i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        b[i][j] = a[j][n-i];
                }
        }

        for(i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        a[i][j] = b[i][j];
                //        cout << b[i][j] << " ";
                }
                //cout << endl;
        }
}

void tranlateC(){
        //cout << "test C" << endl;
        //左右翻转

        for(int i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        b[i][j] = a[i][n-1-j];
                }
        }

        for(i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        a[i][j] = b[i][j];
                        //cout << b[i][j] << " ";
                }
                //cout << endl;
        }
}

void tranlateD(){
//        cout << "test D" << endl;
        //左右翻转

        for(int i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        b[i][j] = a[m-1-i][j];
                }
        }

        for(i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        a[i][j] = b[i][j];
                }
        }
}

最佳答案

查看完整内容

楼主大人,在下写了一个
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-9 10:59:56 | 显示全部楼层    本楼为最佳答案   
楼主大人,在下写了一个
#include <iostream>

using namespace std;
void tranlateA();
void tranlateB();
void tranlateC();
void tranlateD();

int m, n ;
int a[100][100] = {0};
int b[100][100] = {0};

int main(void){

        char c[100], ch;
        
        cin >> m >> n;
        for(int i=0; i<m; i++){
                for(int j=0; j<n; j++){
                        cin >> a[i][j];
                }
        }
        //getchar();
        i = 0;
        while(1){
                ch = getchar();
                if(ch >= 'A' && ch <='D')
                {                
                        c[i] = ch;
                        i++;
                        while((ch = getchar()) != '\n'){
                                c[i] = ch;
                                i++;
                        }
                        break;
                }
        }

        for(int k=0; k<i; k++){
                switch (c[k]){
                case 'A':
                        tranlateA();
                        break;
                case 'B':
                        tranlateB();
                        break;
                case 'C':
                        tranlateC();
                        break;
                case 'D':
                        tranlateD();
                        break;
                }
        }

        for(i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        cout << b[i][j] << " ";
                }
                cout << endl;
        }
        return 0;
}

void tranlateA(){
        //cout << "test A" << endl;
        //顺时针旋转90度
        int temp = 0;
        temp = m; 
        m = n;
        n =temp;

        for(int i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        b[i][j] = a[n-1-j][i];
                }
        }

        for(i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        a[i][j] = b[i][j];
                        //cout << b[i][j] << " ";
                }
        //        cout << endl;
        }
}
void tranlateB(){
        //cout << "test B" << endl;
        //逆时针旋转90度
        int temp = 0;
        temp = m; 
        m = n;
        n =temp;
        for(int i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        b[i][j] = a[j][n-i];
                }
        }

        for(i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        a[i][j] = b[i][j];
                //        cout << b[i][j] << " ";
                }
                //cout << endl;
        }
}

void tranlateC(){
        //cout << "test C" << endl;
        //左右翻转

        for(int i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        b[i][j] = a[i][n-1-j];
                }
        }

        for(i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        a[i][j] = b[i][j];
                        //cout << b[i][j] << " ";
                }
                //cout << endl;
        }
}

void tranlateD(){
//        cout << "test D" << endl;
        //左右翻转

        for(int i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        b[i][j] = a[m-1-i][j];
                }
        }

        for(i=0; i < m; i++){
                for(int j=0; j<n; j++){
                        a[i][j] = b[i][j];
                }
        }
}
捕获.PNG

评分

参与人数 1荣誉 +3 鱼币 +3 贡献 +1 收起 理由
幸运之星 + 3 + 3 + 1 感谢你的无私奉献!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-12-9 11:16:22 | 显示全部楼层
哈哈啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-9 11:25:54 | 显示全部楼层
我觉得凡事得靠自己,自己去思考得到的结果比别人给你的结果是完全不一样的,别人的就是别人的,自己的东西就自己的东西,我会在一题上思考一个上午一个下午我都愿意!只要自己努力了,不管结果是否与老师给的题意符合,但是自己思考了有自己的东西足矣!自己动手是关键!希望你可以明白这个道理!

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
幸运之星 + 5 + 5 + 3 谢谢你的提醒!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-12-9 11:32:31 | 显示全部楼层
像番茄加两个蛋 发表于 2017-12-9 11:25
我觉得凡事得靠自己,自己去思考得到的结果比别人给你的结果是完全不一样的,别人的就是别人的,自己的东西 ...


我试过横着竖着遍历,类似于矩阵转置的方法,但有明显的算法错误。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-12-9 18:18:16 | 显示全部楼层
tailor_long 发表于 2017-12-9 10:59
楼主大人,在下写了一个

谢谢啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-9 21:01:24 | 显示全部楼层

不客气啦
其实我的代码改进的地方还有很多,例如全局变量的问题,例如字符(ABCD)的输入问题,还有第一个变换那里,楼主可以思考一下啦!加油(&#3591; &#8226;_&#8226;)&#3591;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-11 16:11:53 From FishC Mobile | 显示全部楼层
题目都看不懂......
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-1 03:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表