需要逻辑方面的帮助 (C)
我需要交换两个非重复序列(数组)中的前 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用单个 for 循环交换前 n 个元素非常简单。
现在您需要找出数组中发生了什么变化。您可以通过比较交换的部件来做到这一点。
Missing_array2 现在包含 array2 中缺少的数字。这些是将在 array1 中重复的所有数字。 Missing_array1 也是如此。接下来,您需要扫描两个数组并用缺失的数字替换重复的数字。
总之,您可以比较交换的部分以查找每个数组中缺少的值。这些值也是将在相反数组中重复的值。然后扫描每个数组并用其中一个缺失值替换重复值(我假设您不关心哪个缺失值,只要所有值都是唯一的即可。
Swapping the first n elements is straightforward using a single for loop.
Now you need to find what has changed in the arrays. You can do this by comparing the parts you swapped.
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.
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.
如果序列的交换部分包含相同的值,则不会有重复 - 执行交换只会打乱前 n 个元素。因此,您需要修复的值是交换序列之一中出现的值。
首先,我将创建 n 个交换元素的直方图,其中序列 1 中的元素计为位 0,序列 2 中的元素计为位 1如果直方图的任何成员非零,则它们仅以一个或另一个顺序出现。
如果有需要修复的值,那么您可以构建一个需要重写的值的查找表。这应该将 i 映射到 i ,除非 i 是直方图中的不对称值之一,在这种情况下它需要映射到另一个不对称值。
如果序列 1 的 n = 4
直方图
映射(当直方图 [S1[i]] & 1 时,将 [S1[i]] 替换为 S2[i] )
将映射应用于序列 1(i > 1)。 序列 2 的n
映射(当直方图 [S2[i]] 和 2 时,将 [S2[i]] 替换为 S1[i] )
将映射应用于序列 1(当 i > 2 时) n
或者,用直方图中设置的其他位替换下一个值(迭代替换还需要检查是否用其自身替换值);我假设只要结果中的值是唯一的,使用什么值作为替换并不重要。
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.
If n = 4
histogram
mapping for sequence 1 ( while histogram [S1[i]] & 1, replace[S1[i]] with S2[i] )
apply mapping to sequence 1 for i > n
mapping for sequence 2 ( while histogram [S2[i]] & 2, replace[S2[i]] with S1[i] )
apply mapping to sequence 1 for i > n
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.