共轭复合物对的数量

发布于 2025-01-10 17:09:23 字数 1557 浏览 0 评论 0原文

我需要编写一个函数,用 p1 和 p2 界定的数组中的共轭复数对填充数组 rez。该函数返回放置在数组中的共轭复数对的数量。序列中不得放置重复项。共轭复数对是 a + bi 和 a - bi 形式的对。

这个任务应该使用结构和指针算术来解决。不允许使用辅助阵列。

#include <stdio.h>
typedef struct {
  int im, re;
} complex;
void remove_duplicates(complex *rez, int *number){
    int i,j,k;
    for (i = 0; i < *number; i++) {
    for (j = i + 1; j < *number; j++) {
      if (rez[i].im == rez[j].im && rez[i].re == rez[j].re) {
        for (k = j; k < *number - 1; k++) {
          rez[k].im = rez[k + 1].im;
          rez[k].re = rez[k + 1].re;
        }
        (*number)--;
        j--;
      }
    }
  }
}
int conjugate_complex(complex *p1, complex *p2, complex *rez) {
  int number_of_pairs = 0;
  while (p1 < p2) {
    if (p1->im == p1->re||p1->im == -1*p1->re) {
      number_of_pairs++;
      rez->re = p1->re;
      rez->im = -1*p1->im;
    }
     rez++;
    p1++;
  }
  remove_duplicates(rez,&number_of_pairs);
  return number_of_pairs;
}
int main() {
    int i;
  complex arr1[5] = {{5, 5}, {3, 3}, {-5, -5}, {5, 5}, {-3, 3}};
  complex arr2[5];
  int vel = conjugate_complex(arr1, arr1 + 5, arr2);
  printf("%d\n", vel);
  for (i=0; i<vel; i++)
    printf("(%d,%d) ",arr2[i].im,arr2[i].re);
  return 0;
}

输出应该是:

4
(-5,5) (-3,3) (5,-5) (3,3)

我的输出是:

5
(-5,5) (-3,3) (5,-5) (-5,5) (3,3)

我的代码的问题是它打印重复项。 你能帮我修复我的remove_duplicates功能吗? 如果我在主函数中调用它,它就会起作用。但是,我需要在函数 conjugate_complex 中调用它。

I need to write a function which fills array rez with the conjugate-complex pairs from the array bounded by p1 and p2. The function returns the number of conjugate-complex pairs placed in the array. Duplicates must not be placed in the sequence. Conjugate-complex pairs are pairs of forms a + bi and a - bi.

This task should be solved using structures and pointer arithmetic. Auxiliary arrays are not allowed.

#include <stdio.h>
typedef struct {
  int im, re;
} complex;
void remove_duplicates(complex *rez, int *number){
    int i,j,k;
    for (i = 0; i < *number; i++) {
    for (j = i + 1; j < *number; j++) {
      if (rez[i].im == rez[j].im && rez[i].re == rez[j].re) {
        for (k = j; k < *number - 1; k++) {
          rez[k].im = rez[k + 1].im;
          rez[k].re = rez[k + 1].re;
        }
        (*number)--;
        j--;
      }
    }
  }
}
int conjugate_complex(complex *p1, complex *p2, complex *rez) {
  int number_of_pairs = 0;
  while (p1 < p2) {
    if (p1->im == p1->re||p1->im == -1*p1->re) {
      number_of_pairs++;
      rez->re = p1->re;
      rez->im = -1*p1->im;
    }
     rez++;
    p1++;
  }
  remove_duplicates(rez,&number_of_pairs);
  return number_of_pairs;
}
int main() {
    int i;
  complex arr1[5] = {{5, 5}, {3, 3}, {-5, -5}, {5, 5}, {-3, 3}};
  complex arr2[5];
  int vel = conjugate_complex(arr1, arr1 + 5, arr2);
  printf("%d\n", vel);
  for (i=0; i<vel; i++)
    printf("(%d,%d) ",arr2[i].im,arr2[i].re);
  return 0;
}

OUTPUT should be:

4
(-5,5) (-3,3) (5,-5) (3,3)

My output is:

5
(-5,5) (-3,3) (5,-5) (-5,5) (3,3)

The problem with my code is that it prints duplicates.
Could you help me fix my remove_duplicates function?
If I call it in main function it would work. However, I need to call it in the function conjugate_complex.

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

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

发布评论

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

评论(1

叹梦 2025-01-17 17:09:23

要了解为什么拥有一些 O(n) 空间会很好,请考虑一下您在现实生活中会使用方格纸做什么。取出每个复数并在图中放置一个点(re, abs(im))。这样,任何重复项都会合并为一个。该解决方案的复杂度为O(n)。 (预计,哈希值的大小为 O(n) ,因此您必须丢弃一些信息,这将导致冲突。)

最好首先不要重复元素。无论您是否使用 布隆过滤器 来绕过没有数组的限制,O(n) 哈希、O(n log n) 排序或 O(n^2) 方法(可以说是最简单的)最好有这个功能,(在伪代码中, C99,使用 int,装饰除外):

boolean pair_is_equal(pair a, pair b)

请注意,pair 是在语义上不等同于复杂。您可以使用相同的表示形式(您一直在使用这种表示形式,并且考虑到输出格式,这是最简单的),但请注意它们表示不同的事物。如果您让一个complex代表一个pair

boolean pair_is_equal(complex a, complex b)

那么您还必须检查ab之一> 的复共轭,(除了 Im[a] == 0 || Im[b] == 0。)关心 2 的补码 INT_MIN,超出了范围abs 并且不会有补集(如何测试。)

To see why it would be nice to have some O(n) space, consider what you would do in real life with graph paper. Take each complex number and place a spot in the graph (re, abs(im)). In that way, any duplicates get merged into one. This solution is O(n). (Expected, the hash is O(n) size, so you have to throw out some information, which will lead to collisions.)

It would be better to not duplicate elements in the first place. Whether you are using a Bloom filter to get around the restriction of not having an array, O(n) hash, O(n log n) sort, or an O(n^2) approach (arguably the simplest,) it would be good to have this function, (in pseudo-code, <stdbool.h> is C99, use int, adornments aside):

boolean pair_is_equal(pair a, pair b)

Be aware that a pair is not semantically equivalent to a complex. You can use the same representation (which you've been using, and, considering the output format, the simplest,) but be aware that they represent different things. If you let a complex stand in for a pair:

boolean pair_is_equal(complex a, complex b)

then you have to also also check one of a or b's complex conjugate, (except Im[a] == 0 || Im[b] == 0.) It might also be useful care about 2's-compliment INT_MIN, which is out of the domain of abs and will not have a complement (how to test.)

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