gpt4 book ai didi

c - C 中的数组旋转

转载 作者:行者123 更新时间:2023-11-30 20:22:04 24 4
gpt4 key购买 nike

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

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

我应该将其旋转 5 次 90 度。它应该是用 C 语言完成的。

我感谢每一个帮助,因为我只是一个初学者并且一直坚持这一点。

提前致谢。

#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 - C 中的数组旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40832947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com