c-R G B元素数组交换
作者:互联网
我正在尝试创建此c程序来执行以下描述.我非常确定问题在于递归,但不确定如何解决.我猜想它只会不断迭代到无穷大并崩溃.我什至没有输出.我以为我可以比较以前的指针和当前的指针,并根据词典顺序执行一个三段式的临时交换.我将使用一个指针来遍历数组,并在每次交换后将其递减,然后以该ptr作为参数进行递归调用.没有工作,我在这里,请帮我:).如果有一个更简单的解决方案也可以使用,但更希望了解该代码在哪里出错.
#include <string>
#include <iostream>
using namespace std;
// Given an array of strictly the characters 'R', 'G', and
// 'B', segregate the values of the array so that all the
// Rs come first, the Gs come second, and the Bs come last.
// You can only swap elements of the array.
char* RGBorder(char* c_a)
{
size_t sz = sizeof(c_a)/sizeof(*c_a);
char* ptr_ca = c_a;
char* prv_ptr = ptr_ca;
ptr_ca++;
char temp;
while(*ptr_ca)
{
switch(*ptr_ca)
{
case 'R' :
if( *prv_ptr < *ptr_ca ) {
temp = *prv_ptr; *prv_ptr = *ptr_ca; *ptr_ca = temp;
} else if( *prv_ptr == *ptr_ca ) {
continue;
} else { ptr_ca--; RGBorder(ptr_ca); }
case 'G' :
if( *prv_ptr < *ptr_ca ) {
temp = *prv_ptr; *prv_ptr = *ptr_ca; *ptr_ca = temp;
} else if( *prv_ptr == *ptr_ca ) {
continue;
} else { ptr_ca--; RGBorder(ptr_ca); }
default:
ptr_ca++;
continue;
}
ptr_ca++;
cout << *ptr_ca;
}
return c_a;
}
int main()
{
char ca[] = {'G', 'B', 'R', 'R', 'B', 'R', 'G'};
char *oca =RGBorder(ca);
char *pca = oca;
while(*pca)
{
cout << *pca << endl;
pca++;
}
}
解决方法:
您的代码有很多问题.
1)您使用字符指针调用函数RGBorder,然后尝试使用以下命令获取字符数:
size_t sz = sizeof(c_a)/sizeof(*c_a);
这不会使您获得字符数.相反,这只会让您
sizeof(char *) / sizeof(char)
通常为4或8.使用char数组调用函数的唯一方法是提供以null终止的数组(因此可以使用strlen),或者必须将数组中的字符数作为单独的参数传递:
char *RGBorder(char *c_a, int size)
2)我没有遍历您的代码,但是有更简单的方法可以在数组中进行三向分区.一种流行的算法是基于Dutch National Flag问题的算法.
由于您要按RGB顺序排列数组,因此您知道G系列将始终位于序列的中间(某处),R位于序列的左侧,B始终位于序列的右侧.
因此,目标是简单地将R交换到中间的左侧,将B交换到中间的右侧.因此,基本上,您需要一个循环,在需要时逐步更改“中间”,同时在检测到R和B时将其交换到适当的位置.
以下代码说明了这一点:
#include <algorithm>
char *RGBorder(char *c_a, int num)
{
int middle = 0; // assume we only want the middle element
int low = 0; // before the G's
int high = num - 1; // after the G's
while (middle <= high)
{
if ( c_a[middle] == 'R' ) // if we see an 'R' in the middle, it needs to go before the middle
{
std::swap(c_a[middle], c_a[low]); // swap it to a place before middle
++middle; // middle has creeped up one spot
++low; // so has the point where we will swap when we do this again
}
else
if (c_a[middle] == 'B') // if we see a 'B' as the middle element, it needs to go after the middle
{
std::swap(c_a[middle], c_a[high]); // place it as far back as you can
--high; // decrease the back position for next swap that comes here
}
else
++middle; // it is a 'G', do nothing
}
return c_a;
}
这是另一个使用std::partition的解决方案.
#include <algorithm>
#include <iostream>
char *RGBorder(char *c_a, int num)
{
auto iter = std::partition(c_a, c_a + num, [](char ch) {return ch == 'R';});
std::partition(iter, c_a + num, [](char ch) {return ch == 'G';});
return c_a;
}
基本上,第一次调用std :: partition会将R放置在数组的前面.由于std :: partition返回一个迭代器(在本例中为char *)到发生分区的末尾,因此我们将其用作对std :: partition的第二次调用的开始位置,在此我们对G值进行分区.
注意,std :: partition也通过交换来实现其目标.
给定此解决方案,我们可以使用循环将其推广到n路分区.假设我们要按RGBA顺序放置事物(4个值而不是3个值).
#include <algorithm>
#include <iostream>
#include <cstring>
char *RGBorder(char *c_a, int num, char *order, int num2)
{
auto iter = c_a;
for (int i = 0; i < num2 - 1; ++i)
iter = std::partition(iter, c_a + num, [&](char ch) {return ch == order[i];});
return c_a;
}
int main()
{
char ca[] = "AGBRRBARGGARRBGAGRARAA";
std::cout << RGBorder(ca, strlen(ca), "RGBA", 4);
}
输出:
RRRRRRRGGGGGBBBAAAAAAA
标签:compare-and-swap,c,arrays 来源: https://codeday.me/bug/20191010/1883994.html