Career Cup 1-6

/*******************************************************

    1.6

    Given an image represented by an NxN matrix, where each pixel in the image is 4
    bytes, write a method to rotate the image by 90 degrees.  Can you do this in place?

  Note:  At first line the parameter N is read.
         After the first line
         N * N lines are read to store into the image structure,
         each line containing 4 bytes pixel data.

        This code will perform a anticlockwise rotation of 90 degrees.

  Complexity: O(n^2 * k)

  Mod Time: 4.22, 2012

  Copyright: Ben

  *******************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define BYTE_PER_PIX        4

typedef struct
{
    short byte[4];
}    PIXEL;

int main(void)
{
    //ImageN is the one size length of image. i.e. the whole image is n * n.
    //Pixel is number of pixel, i.e. n * n
    int i, j, k;
    int ImageN, Pixel;
    PIXEL *ImageMap;

    freopen("Image.txt", "r", stdin);

    scanf("%d", &ImageN);

    Pixel = ImageN * ImageN;

    ImageMap = (PIXEL *)malloc(sizeof(PIXEL) * Pixel);

    for(i = 0; i < ImageN; i++)
    {
        for(j = 0; j < ImageN; j++)
        {
            for(k = 0; k < BYTE_PER_PIX; k++)
            {    //scanf should take the address as parameter.
                scanf("%hd", &((ImageMap + ImageN * (ImageN - 1 - j) + i)->byte[k]));  //ImageMap[N - 1 - j][i]
            }
        }
    }

    for(i = 0; i < ImageN; i++)
    {
        for(j = 0; j < ImageN; j++)
        {
            for(k = 0; k < BYTE_PER_PIX; k++)
            {
                printf("%hd\t", (ImageMap + (ImageN * i + j))->byte[k]);  //ImageMap[i][j]
            }
            printf("\n");
        }
        
    }
    
    fclose(stdin);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值