我有 2 个并行的数组:
defenders = {1,5,7,9,12,18};
attackers = {3,10,14,15,17,18};
两者都已排序,我想做的是重新排列防御数组的值,以便他们赢得更多游戏(防御者[i] > 攻击者[i]),但我在如何交换防御者中的值方面遇到问题大批。所以实际上,我们只针对攻击者使用防御者数组。
我有这个,但如果有什么变化的话,我很确定我做的不对。它应该是一种蛮力方法。
void rearrange(int* attackers, int* defenders, int size){
int i, c, j;
int temp;
for(i = 0; i<size; i++){
c = 0;
j = 0;
if(defenders[c]<attackers[j]){
temp = defenders[c+1];
defenders[c+1] = defenders[c];
defenders[c] = temp;
c++;
j++;
}
else
c++;
j++;
}
}
编辑:我以前也问过这个问题,但感觉自己措辞很烂,不知道怎么“撞”旧帖。
最佳答案
老实说,我没有看你的代码,因为我必须在不到 2.30 小时内起床去上类,希望你不会对我有好感.. :)子>
我实现了 algorithm由 Eugene Sh 提出。在深入研究代码之前,您可能需要先阅读一些链接:
我的方法:
- 通过扫描
att
创建合并数组和def
. 对合并后的数组进行排序。
笔芯
def
具有满足 ad 模式的值。- 完成重新填充
def
与剩余的值(即 失败)*.
*步骤 3 和 4 在我的方法中需要两次传递,也许它可以变得更好。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char c; // a for att and d for def
int v;
} pair;
void print(pair* array, int N);
void print_int_array(int* array, int N);
// function to be used by qsort()
int compar(const void* a, const void* b) {
pair *pair_a = (pair *)a;
pair *pair_b = (pair *)b;
if(pair_a->v == pair_b->v)
return pair_b->c - pair_a->c; // d has highest priority
return pair_a->v - pair_b->v;
}
int main(void) {
const int N = 6;
int def[] = {1, 5, 7, 9, 12, 18};
int att[] = {3, 10, 14, 15, 17, 18};
int i, j = 0;
// let's construct the merged array
pair merged_ar[2*N];
// scan the def array
for(i = 0; i < N; ++i) {
merged_ar[i].c = 'd';
merged_ar[i].v = def[i];
}
// scan the att array
for(i = N; i < 2 * N; ++i) {
merged_ar[i].c = 'a';
merged_ar[i].v = att[j++]; // watch out for the pointers
// 'merged_ar' is bigger than 'att'
}
// sort the merged array
qsort(merged_ar, 2 * N, sizeof(pair), compar);
print(merged_ar, 2 * N);
// scan the merged array
// to collect the patterns
j = 0;
// first pass to collect the patterns ad
for(i = 0; i < 2 * N; ++i) {
// if pattern found
if(merged_ar[i].c == 'a' && // first letter of pattern
i < 2 * N - 1 && // check that I am not the last element
merged_ar[i + 1].c == 'd') { // second letter of the pattern
def[j++] = merged_ar[i + 1].v; // fill-in `def` array
merged_ar[i + 1].c = 'u'; // mark that value as used
}
}
// second pass to collect the cases were 'def' loses
for(i = 0; i < 2 * N; ++i) {
// 'a' is for the 'att' and 'u' is already in 'def'
if(merged_ar[i].c == 'd') {
def[j++] = merged_ar[i].v;
}
}
print_int_array(def, N);
return 0;
}
void print_int_array(int* array, int N) {
int i;
for(i = 0; i < N; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
void print(pair* array, int N) {
int i;
for(i = 0; i < N; ++i) {
printf("%c %d\n", array[i].c, array[i].v);
}
}
输出:
gsamaras@gsamaras:~$ gcc -Wall px.c
gsamaras@gsamaras:~$ ./a.out
d 1
a 3
d 5
d 7
d 9
a 10
d 12
a 14
a 15
a 17
d 18
a 18
5 12 18 1 7 9
关于c - 相对于另一个数组重新排列一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35834752/