重新排列对于一个阵列到另一个阵列 [英] Rearranging an array with respect to another array
问题描述
我有2个阵列,并行:
defenders = {1,5,7,9,12,18};
attackers = {3,10,14,15,17,18};
两者的排序,我所要做的是,让他们赢得更多的比赛重新卫冕阵列的价值观(后卫[I]>攻击[I]),但我有关于如何在捍卫者交换价值的问题数组。因此,在现实中我们只守军阵列的工作相对于攻击者。
Both are sorted, what I am trying to do is rearrange the defending array's values so that they win more games (defender[i] > attacker[i]) but I am having issues on how to swap the values in the defenders array. So in reality we are only working with the defenders array with respect to the attackers.
我有这个,但如果有什么不移位多少和Im pretty肯定我不这样做是正确的。它的假设是一个强制的方法。
I have this but if anything it isn't shifting much and Im pretty sure I'm not doing it right. Its suppose to be a brute force method.
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++;
}
}
编辑:我之前没有问这个问题,但我觉得,如果我措辞了苦头,而且不知道如何凸点上了年纪后。
I did ask this question before, but I feel as if I worded it terribly, and didn't know how to "bump" the older post.
推荐答案
<子>说实话,我没看你的code,因为我已经在不到2.30小时,去唤醒工作中,希望你不要对我有硬的感觉..:)
我实现了尤金嘘提出的算法。您可能需要先读一些链接,挖掘到code面前:
I implemented the algorithm proposed by Eugene Sh. Some links you may want to read first, before digging into the code:
- qsort in C
- qsort and structs
- shortcircuiting
我的方法:
- 通过扫描创建合并阵列既
ATT
和DEF
。 -
排序合并数组。
- Create merged array by scanning both
att
anddef
. Sort merged array.
补充装 DEF
与满足值的广告 em>的模式。
Refill def
with values that satisfy the ad pattern.
<子> *步骤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
这篇关于重新排列对于一个阵列到另一个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!