CSP201503-1: image rotation, csp201503-1 Rotation

Source: Internet
Author: User

CSP201503-1: image rotation, csp201503-1 Rotation

Introduction:CSP (http://www.cspro.org/lead/application/ccf/login.jsp) is a "Computer vocational qualification certification" examination initiated by CCF, perform capability certification for professionals in computer software development, software testing, information management, and other fields. The subjects of certification are those who are engaged in or will be engaged in IT professional technical and technical management personnel, as well as review subjects for university recruitment and postgraduate students.

 

  • Problem description

Rotation is the basic operation of image processing. In this case, you need to rotate an image 90 degrees counter-clockwise.

The image representation in the computer can be expressed by a matrix. to rotate an image, you only need to rotate the corresponding matrix.

  • Input Format

The first line contains two integers, n and m, indicating the number of rows and columns of the image matrix.

Each line in the next n rows contains m integers, indicating the input image.

  • Output Format

Output m rows. Each row contains n integers, indicating the matrix after 90 degrees of counter-clockwise rotation of the original matrix.

  • Sample Input

    2 3

    1 5 3

    3 2 4

  • Sample output

    3 4

    5 2

    1 3

  • Scale and conventions of evaluation cases

    1 ≤ n, m ≤ 1,000, and the number in the matrix is a non-negative integer not greater than 1000.

 

  • Source code (1): Use a dynamically allocated one-dimensional array

/* One-dimensional array solution, dynamic space allocation */

# Include <stdio. h>

# Include <stdlib. h>

# Include <memory. h>

 

Int main (void)

{

Int n; // number of rows

Int m; // Number of Columns

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;

}

 

  • Source code (2): Use a dynamically allocated two-dimensional array

/* Two-dimensional array solution, dynamic space allocation */

# Include <stdio. h>

# Include <stdlib. h>

# Include <memory. h>

 

Int main (void)

{

Int n; // number of rows

Int m; // Number of Columns

Scanf ("% d", & n );

Scanf ("% d", & m );

// Allocate space for the input two-dimensional data

Int ** input = (int **) malloc (sizeof (int *) * n); // allocate space for rows

For (int I = 0; I <n; I ++) // allocate space to the column

{

Input [I] = (int *) malloc (sizeof (int) * m );

Memset (input [I], 0, sizeof (int) * m );

}

// Allocate space to the output two-dimensional array

Int ** output = (int **) malloc (sizeof (int *) * m); // allocate space for rows

For (int I = 0; I <m; I ++) // allocate space to the column

{

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]);

}

}

}

// Reclaim the memory allocated to the input Array

For (int I = 0; I <n; I ++)

{

Free (input [I]);

}

Free (input );

// Reclaim the memory allocated to the output Array

For (int I = 0; I <m; I ++)

{

Free (output [I]);

}

Free (output );

Return 0;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.