CareerCup-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?

唉!还是会有错误,这次控制到只有一处错误。

#include <iostream>
using namespace std;
#define N 5
void rotate(int data[N][N]) 
{
    int temp, m;
    for(int i=0; i<N-2; i++)
    {
        m = N-1;
        for(int j=i; j<N-i-1; j++)
        {
            temp = data[i][j];
            data[i][j] = data[m-j][i];
            data[m-j][i] = data[m-i][m-j];
            data[m-i][m-j] = data[j][m-i];
            data[j][m-i] = temp;
        }
    }
}

void print(int data[N][N])
{
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<N; j++)
            cout<<data[i][j]<<"\t";
        cout<<endl;
    }
    cout<<"End"<<endl;
}
int main()
{
    int data[N][N];
    int count = 0;
    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
            data[i][j] = ++count;
    print(data);
    rotate(data);
    print(data);
    system("pause"); 
};

#include <iostream>
using namespace std;
#define N 5
void rotate(int data[N][N]) 
{
    int temp, m;
    for(int i=0; i<N-2; i++)
    {
        m = N-1;
        for(int j=i; j<N-i-1; j++)
        {
            temp = data[i][j];
            data[i][j] = data[m-j][i];
            data[m-j][i] = data[m-i][m-j];
            data[m-i][m-j] = data[j][m-i];
            data[j][m-i] = temp;
        }
    }
}

void print(int data[N][N])
{
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<N; j++)
            cout<<data[i][j]<<"\t";
        cout<<endl;
    }
    cout<<"End"<<endl;
}
int main()
{
    int data[N][N];
    int count = 0;
    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
            data[i][j] = ++count;
    print(data);
    rotate(data);
    print(data);
    system("pause"); 
};

目录
相关文章
|
机器学习/深度学习