CSP201503-1:图像旋转
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试,针对计算机软件开发、软件测试、信息管理等领域的专业人士进行能力认证。认证对象是从事或将要从事IT领域专业技术与技术管理人员,以及高校招考研究生的复试对象。
- 问题描述
旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度。
计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可。
- 输入格式
输入的第一行包含两个整数n, m,分别表示图像矩阵的行数和列数。
接下来n行每行包含m个整数,表示输入的图像。
- 输出格式
输出m行,每行包含n个整数,表示原始矩阵逆时针旋转90度后的矩阵。
- 样例输入
2 3
1 5 3
3 2 4
- 样例输出
3 4
5 2
1 3
- 评测用例规模与约定
1 ≤ n, m ≤ 1,000,矩阵中的数都是不超过1000的非负整数。
- 源代码(一):使用动态分配的一维数组
|
/* 一维数组的方式求解,空间动态分配 */ # include <stdio.h> # include <stdlib.h> # include <memory.h> int main(void) { int n; //行数 int m; //列数 scanf("%d", &n); scanf("%d", &m); int count = m*n+1; int *input = (int *)malloc(sizeof(int) * count); memset(input, 0, sizeof(int) * count); int *output = (int *)malloc(sizeof(int) * count); memset(output, 0, sizeof(int) * count); for (int i = 1; i < count; i++) { scanf("%d", input+i); int row_in = (i / m) + 1; int column_in = i % m; if (column_in == 0) { row_in -= 1; column_in = m; } int row_out = m + 1 - column_in; int column_out = row_in; output[(row_out - 1) * n + column_out] = input[i]; } int num = 0; for (int i = 1; i < count; i++) { num += 1; if (num == n) { printf("%d\n", output[i]); num = 0; } else { printf("%d ", output[i]); } } free(input); free(output); return 0; } |
- 源代码(二):使用动态分配的二维数组
|
/* 二维数组的方式求解,空间动态分配 */ # include <stdio.h> # include <stdlib.h> # include <memory.h> int main(void) { int n; //行数 int m; //列数 scanf("%d", &n); scanf("%d", &m); //给输入的二维数据分配空间 int **input = (int **)malloc(sizeof(int *) * n); //给行分配空间 for (int i = 0; i < n; i++) //给列分配空间 { input[i] = (int *)malloc(sizeof(int) * m); memset(input[i], 0, sizeof(int) * m); } //给输出的二维数组分配空间 int **output = (int **)malloc(sizeof(int *) * m); //给行分配空间 for (int i = 0; i < m; i++) //给列分配空间 { output[i] = (int *)malloc(sizeof(int) * n); memset(output[i], 0, sizeof(int) * n); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &input[i][j]); output[m-1-j][i] = input[i][j]; } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (j == n-1) { printf("%d\n", output[i][j]); } else { printf("%d ", output[i][j]); } } } //回收分配给输入数组的内存 for (int i = 0; i < n; i++) { free(input[i]); } free(input); //回收分配给输出数组的内存 for (int i = 0; i < m; i++) { free(output[i]); } free(output); return 0; } |
CSP201503-1:图像旋转的更多相关文章
- NOI题库 09:图像旋转翻转变换
NOI题库开始的题,也是略水,当然也是大水,所以彼此彼此 09:图像旋转翻转变换 总时间限制: 1000ms 内存限制: 65536kB 描述 给定m行n列的图像各像素点灰度值,对其依次进行一系列操作 ...
- 【OpenCV学习笔记】之六 手写图像旋转函数---万丈高楼平地起
话说,平凡之处显真格,这一点也没错! 比如,对旋转图像进行双线性插值,很简单吧? 可,对我,折腾了大半天,也没有达到预期效果! 尤其是三个误区让我抓瞎好久: 1,坐标旋转公式. 这东西,要用 ...
- opencv 图像仿射变换 计算仿射变换后对应特征点的新坐标 图像旋转、缩放、平移
常常需要最图像进行仿射变换,仿射变换后,我们可能需要将原来图像中的特征点坐标进行重新计算,获得原来图像中例如眼睛瞳孔坐标的新的位置,用于在新得到图像中继续利用瞳孔位置坐标. 仿射变换在:http:// ...
- Opencv2.4.4作图像旋转和缩放
关于下面两个主要函数的讲解: cv::getRotationMatrix2D(center, angle, scale); cv::warpAffine(image, rotateImg, rotat ...
- 每日算法37:Rotate Image (图像旋转)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [google面试CTCI] 1-6.图像旋转问题
[字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, wr ...
- pyhton:图像旋转
最近一个作业中要用到图像旋转,分享一下学习过程.如有讲错的地方,恳请指正! 图像旋转,想想真简单啊,不就是将图像矩阵乘上一个旋转平移矩阵就完了吗?实际上还真没这么简单.首先这个旋转平移矩阵怎么获得?通 ...
- CCF CSP 201503-1 图像旋转 (降维)
题目链接:http://118.190.20.162/view.page?gpid=T27 问题描述 试题编号: 201503-1 试题名称: 图像旋转 时间限制: 5.0s 内存限制: 256.0M ...
- 图像旋转、伸缩的自写matlab实现
一.图像的旋转 今天的代码不是自己写的,缺少一些时间.但是认认真真推导了一下旋转的公式,代码的思想与原博博主一致,致敬! 愚以为,自己来实现图像旋转算法的关键点有二:其一,确定旋转后的图像边界.其二, ...
- 图像旋转与图像缩放及Matlab代码实现
本周的作业是自己通过公式编写图像旋转与缩放的代码.今天先通过调用函数的方法来实现. 图像的旋转: A=imread('2.jpg'); J=imrotate(A, 30); subplot(1,2,1 ...
随机推荐
- js 跳转整理
js方式的页面跳转1.window.location.href方式 <script language="javascript" type="text/java ...
- shell命令查看某文件夹下的文件个数
shell命令查看某文件夹下的文件个数 2010-06-25 17:05:15| 分类: shell |字号 订阅 1.查看某文件夹下文件的个数: ls -l |grep "^-&qu ...
- SqlSugar批量添加修改问题
直接InsertRange空集合会报错,如果我们是同时执行多个添加或修改,不要共用一个上下文,最好是在方法里面声明上下文进行区分,不然容易报错 //如果同时执行多个添加,更新 操作不要共用一个上下文, ...
- echarts图标相关
图标类型参考地址: http://echarts.baidu.com/echarts2/doc/doc.html 知识点一: 堆叠柱状图与普通柱状图的区别在于: 堆叠柱状图 在series中需要设置 ...
- JavaScript编写棋盘覆盖
一.前言 之前做了一个算法作业,叫做棋盘覆盖,本来需要用c语言来编写的,但是因为我的c语言是半桶水(哈哈),所以索性就把网上的c语言写法改成JavaScript写法,并且把它的覆盖效果显示出来 二.关 ...
- c#常用数据库封装再次升级
c#封装的几类数据库操作: 1.sqilte 2.berkeleydb 3.一般数据库 4.redis 包含其他项目: 1.序列化 2.通信 3.自定义数据库连接池 再次升级内容: 1.新增redis ...
- ionic 安装步骤
安装ionic和cordova 1,需要首先安装好nodejs,然后通过npm来安装 npm install -g cordova ionic 注意:可能遇到的错误:Error: Cannot fi ...
- #leetcode刷题之路20-有效的括号
#include <iostream> #include <string> #include <stack> using namespace std; bool i ...
- ABAP术语-IDOC
IDOC 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/21/1075988.html Intermediate Document Inte ...
- SpringBoot配置redis和分布式session-redis
springboot项目 和传统项目 配置redis的区别,更加简单方便,在分布式系统中,解决sesssion共享问题,可以用spring session redis. 1.pom.xml <d ...