需要逻辑方面的帮助 (C)

发布于 2024-09-07 16:17:59 字数 909 浏览 4 评论 0原文

我需要交换两个非重复序列(数组)中的前 n 个元素,其中 n 是一个随机整数。

序列 1: 1 4 5 6 9 8 2 3 7

序列 2: 3 9 1 2 8 7 4 5 6

如果 n = 4

序列 1: 3 9 1 2 | 9 8 2 3 7

序列2:1 4 5 6 | 8 7 4 5 6

现在我需要通过替换“|”后的重复数字来修复序列。

如何做到这一点?

这是我的努力..

for(left1 = 0; left1<pivot; left1++)
  {
   for(right1 = pivot; right1 < no_jobs; right1++)
    {
     if(S1->sequence[left1] == S1->sequence[right1])
      {
        for(left2 = 0; left2<pivot; left2++)
        {
          for(right2 = pivot; right2<no_jobs; right2++)
          {
            if(S2->sequence[left2] == S2->sequence[right2])
            {
              swap_temp = S1->sequence[right1];
              S1->sequence[right1] = S2->sequence[right2];
              S2->sequence[right2] = swap_temp;
              break;
            }
          } 
        }
      }
    }
  }

I need to swap first n elements from two non repeating sequences(arrays), where n is a random integer.

Seq1: 1 4 5 6 9 8 2 3 7

Seq2: 3 9 1 2 8 7 4 5 6

If n = 4

Seq1: 3 9 1 2 | 9 8 2 3 7

Seq2: 1 4 5 6 | 8 7 4 5 6

Now i need to repair the sequence by replacing the repeated numbers after '|'.

How to do this?

This is my effort..

for(left1 = 0; left1<pivot; left1++)
  {
   for(right1 = pivot; right1 < no_jobs; right1++)
    {
     if(S1->sequence[left1] == S1->sequence[right1])
      {
        for(left2 = 0; left2<pivot; left2++)
        {
          for(right2 = pivot; right2<no_jobs; right2++)
          {
            if(S2->sequence[left2] == S2->sequence[right2])
            {
              swap_temp = S1->sequence[right1];
              S1->sequence[right1] = S2->sequence[right2];
              S2->sequence[right2] = swap_temp;
              break;
            }
          } 
        }
      }
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ花落谁相伴 2024-09-14 16:17:59

使用单个 for 循环交换前 n 个元素非常简单。

for(int i = 0; i < n; i++){
   int tmp = array1[i];
   array1[i] = array2[i];
   array2[i] = tmp;
}

现在您需要找出数组中发生了什么变化。您可以通过比较交换的部件来做到这一点。

int m1 = 0, m2 = 0;
int missing_array1[n];
int missing_array2[n];

for(int i = 0; i < n; i++){
    bool found = false;
    for(int j = 0; j < n; j++){
        if(array1[i] == array2[j]){
            found = true;
            break;
        } 
    }
    if(!found){
        missing_array2[m2++] = array1[i];
    }
}

for(int i = 0; i < n; i++){
    bool found = false;
    for(int j = 0; j < n; j++){
        if(array2[i] == array1[j]){
            found = true;
            break;
        } 
    }
    if(!found){
        missing_array1[m1++] = array2[i];
    }
}

Missing_array2 现在包含 array2 中缺少的数字。这些是将在 array1 中重复的所有数字。 Missing_array1 也是如此。接下来,您需要扫描两个数组并用缺失的数字替换重复的数字。

while(m1 >= 0){
    int z = 0;
    while(missing_array1[m1] != array2[n + z]){
        z++;
    }
    array2[n + z] = missing_array2[m1--];
}

while(m2 >= 0){
    int z = 0;
    while(missing_array2[m2] != array1[n + z]){
        z++;
    }
    array1[n + z] = missing_array1[m2--];
}

总之,您可以比较交换的部分以查找每个数组中缺少的值。这些值也是将在相反数组中重复的值。然后扫描每个数组并用其中一个缺失值替换重复值(我假设您不关心哪个缺失值,只要所有值都是唯一的即可。

Swapping the first n elements is straightforward using a single for loop.

for(int i = 0; i < n; i++){
   int tmp = array1[i];
   array1[i] = array2[i];
   array2[i] = tmp;
}

Now you need to find what has changed in the arrays. You can do this by comparing the parts you swapped.

int m1 = 0, m2 = 0;
int missing_array1[n];
int missing_array2[n];

for(int i = 0; i < n; i++){
    bool found = false;
    for(int j = 0; j < n; j++){
        if(array1[i] == array2[j]){
            found = true;
            break;
        } 
    }
    if(!found){
        missing_array2[m2++] = array1[i];
    }
}

for(int i = 0; i < n; i++){
    bool found = false;
    for(int j = 0; j < n; j++){
        if(array2[i] == array1[j]){
            found = true;
            break;
        } 
    }
    if(!found){
        missing_array1[m1++] = array2[i];
    }
}

missing_array2 now contains the numbers that are missing from array2. These are all the numbers that will be duplicated in array1. The same goes for missing_array1. Next you need to scan both arrays and replace the duplicates with the missing numbers.

while(m1 >= 0){
    int z = 0;
    while(missing_array1[m1] != array2[n + z]){
        z++;
    }
    array2[n + z] = missing_array2[m1--];
}

while(m2 >= 0){
    int z = 0;
    while(missing_array2[m2] != array1[n + z]){
        z++;
    }
    array1[n + z] = missing_array1[m2--];
}

In summary, you compare the parts you swapped to find the values that will be missing from each array. These value are also the values that will be duplicated in the opposite array. Then you scan each of the arrays and replace the duplicate values with one of the missing values (I assume you don't care which of the missing values, as long as all the values are unique.

累赘 2024-09-14 16:17:59

如果序列的交换部分包含相同的值,则不会有重复 - 执行交换只会打乱前 n 个元素。因此,您需要修复的值是交换序列之一中出现的值。

首先,我将创建 n 个交换元素的直方图,其中序列 1 中的元素计为位 0,序列 2 中的元素计为位 1如果直方图的任何成员非零,则它们仅以一个或另一个顺序出现。

如果有需要修复的值,那么您可以构建一个需要重写的值的查找表。这应该将 i 映射到 i ,除非 i 是直方图中的不对称值之一,在这种情况下它需要映射到另一个不对称值。

Seq1: 1 4 5 6 9 8 2 3 7

Seq2: 3 9 1 2 8 7 4 5 6

如果序列 1 的 n = 4

Seq1: 3 9 1 2 | 9 8 2 3 7

Seq2: 1 4 5 6 | 8 7 4 5 6

直方图

value    1 2 3 4 5 6 7 8 9 
count    3 1 1 2 2 2 0 0 1

映射(当直方图 [S1[i]] & 1 时,将 [S1[i]] 替换为 S2[i] )

value    1 2 3 4 5 6 7 8 9 
replace  1 6 5 4 5 6 7 8 4

将映射应用于序列 1(i > 1)。 序列 2 的n

Seq1:    3 9 1 2 | 9 8 2 3 7
replace  - - - - | 4 8 6 5 7
result   3 9 1 2 | 4 8 6 5 7

映射(当直方图 [S2[i]] 和 2 时,将 [S2[i]] 替换为 S1[i] )

value    1  2  3  4  5  6  7  8  9 
replace  1  2  3  9  3  2  7  8  9 

将映射应用于序列 1(当 i > 2 时) n

Seq2:    1 4 5 6 | 8 7 4 5 6
replace  - - - - | 8 7 9 3 2
result   1 4 5 6 | 8 7 9 3 2

或者,用直方图中设置的其他位替换下一个值(迭代替换还需要检查是否用其自身替换值);我假设只要结果中的值是唯一的,使用什么值作为替换并不重要。

If the swapped portions of the sequences contain the same values, then there would be no repeats - performing the swap would just shuffle the first n elements. So the values you need to repair are the values which occur in one of the swapped sequences

Firstly, I'd create a histogram of the n swapped elements, with those from sequence 1 counting as bit 0, and those from sequence 2 as bit 1. If any members of the histogram are non-zero, then they occur in one or the other sequence only.

If there are values requiring repair, then you can construct a look-up table of the values which require rewriting. This should map i to i unless i is one of the asymmetric values in the histogram, in which case it needs to map to the another asymmetric value.

Seq1: 1 4 5 6 9 8 2 3 7

Seq2: 3 9 1 2 8 7 4 5 6

If n = 4

Seq1: 3 9 1 2 | 9 8 2 3 7

Seq2: 1 4 5 6 | 8 7 4 5 6

histogram

value    1 2 3 4 5 6 7 8 9 
count    3 1 1 2 2 2 0 0 1

mapping for sequence 1 ( while histogram [S1[i]] & 1, replace[S1[i]] with S2[i] )

value    1 2 3 4 5 6 7 8 9 
replace  1 6 5 4 5 6 7 8 4

apply mapping to sequence 1 for i > n

Seq1:    3 9 1 2 | 9 8 2 3 7
replace  - - - - | 4 8 6 5 7
result   3 9 1 2 | 4 8 6 5 7

mapping for sequence 2 ( while histogram [S2[i]] & 2, replace[S2[i]] with S1[i] )

value    1  2  3  4  5  6  7  8  9 
replace  1  2  3  9  3  2  7  8  9 

apply mapping to sequence 1 for i > n

Seq2:    1 4 5 6 | 8 7 4 5 6
replace  - - - - | 8 7 9 3 2
result   1 4 5 6 | 8 7 9 3 2

Alternatively, replace with the next value with the other bit set in the histogram (the iterated replace will also need to check for replacing a value with itself); I'm assuming it doesn't really matter what value is used as the replacement as long as the values in the result are unique.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文