本文介绍了C中的数组旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试旋转如下所示的数组:

I am trying to rotate an array that looks like this:

    a a a a  
    a b a a  
    b b b a
    a a a a

我应该将其旋转5次,共90度。

I am supposed to rotate it 5 times for 90 degrees. It is supposed to be done in C.

我非常感谢每一个帮助,因为我只是一个初学者,而且对此一无所知。

I appreciate every help because I am just a beginner and am stuck on this.

提前感谢。

#include <stdio.h>

int main()
{
    char array_1[4][4] = { {'-','-','-','-'},
                           {'-','o','-','-'},
                           {'o','o','o','-'},
                           {'-','-','-','-'}};

    char array_2[4][4] = { {'-','-','-','-'},
                           {'-','o','o','-'},
                           {'o','o','-','-'},
                           {'-','-','-','-'}};

    char array_3[4][4] = { {'-','-','-','-'},
                           {'-','o','-','-'},
                           {'-','o','-','-'},
                           {'-','o','o','-'}};

    char array_4[4][4] = { {'-','-','o','-'},
                           {'-','-','o','-'},
                           {'-','-','o','-'},
                           {'-','-','o','-'}};

    int counter = 0;
    int counter_1 = 0;


    for(counter = 0; counter < 4; counter++)
    {
        for(counter_1 = 0; counter_1 < 4; counter_1++)
        {
            printf("%c ",array_1[counter][counter_1]);
        }
        printf("  ");

        for(counter_1 = 0; counter_1 < 4; counter_1++)
        {
            printf("%c ",array_2[counter][counter_1]);
        }
        printf("  ");

        for(counter_1 = 0; counter_1 < 4; counter_1++)
        {
            printf("%c ",array_3[counter][counter_1]);
        }
            printf("  ");

        for(counter_1 = 0; counter_1 < 4; counter_1++)
        {
            printf("%c ",array_4[counter][counter_1]);
        }
        printf("  ");

        printf("\n");
    }

    printf("\n");

    for(counter= 0; counter < 4; counter++)
    {
        for(counter_1 =  3; counter_1 >= 0; counter_1--)
        {
            printf("%c ",array_1[counter_1][counter]);
        }
        printf("  ");
        for(counter_1 =  3; counter_1 >= 0; counter_1--)
        {
            printf("%c ",array_2[counter_1][counter]);
        }
        printf("  ");
        for(counter_1 =  3; counter_1 >= 0; counter_1--)
        {
            printf("%c ",array_3[counter_1][counter]);
        }
        printf("  ");
        for(counter_1 =  3; counter_1 >= 0; counter_1--)
        {
            printf("%c ",array_4[counter_1][counter]);
        }
        printf("  ");

        printf("\n");
    }
    printf("\n");


推荐答案

像这样:

#include <stdio.h>

typedef struct point { int x, y; } Point;

void rotate(int n, char array[n][n]){
    //rotate right 90 degrees
    if(n == 1) return ;
    int times = n / 2;
    for(int i = 0; i < times; ++i){
        Point base = { i, i };
        for(int j = 0; j < n - 1; ++j){
            Point transition[4] = { {j, n-1}, {n-1,n-1-j},{n-1-j,0},{0,j} };
            char curr = array[base.x][base.y+j];//base + {0,j}
            for(int k = 0; k < 4; ++k){
                char temp = array[base.x + transition[k].x][base.y + transition[k].y];
                array[base.x + transition[k].x][base.y + transition[k].y] = curr;
                curr = temp;
            }
        }
        n -= 2;
    }
}

void display(int n, char array[n][n]){
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            if(j)
                putchar(' ');
            putchar(array[i][j]);
        }
        putchar('\n');
    }
    putchar('\n');
}

int main(void){
    //demo
    char array4[4][4] = {
        {'1','2','3','4'},
        {'5','6','7','8'},
        {'9','A','B','C'},
        {'D','E','F','0'}
    };
    display(4, array4);
    int n = 4;
    while(n--){
        rotate(4, array4);
        display(4, array4);
    }
    char array5[5][5] = {
        {'A','B','C','D','E'},
        {'F','G','H','I','J'},
        {'K','L','M','N','O'},
        {'P','Q','R','S','T'},
        {'U','V','W','X','Y'}
    };
    display(5, array5);
    n = 4;
    while(n--){
        rotate(5, array5);
        display(5, array5);
    }
}

这篇关于C中的数组旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:43