在 C 中构造数组补集的惯用方法

发布于 2024-09-15 21:38:36 字数 608 浏览 2 评论 0原文

我正在编写一个函数,它传递一个指向长度为 4 的数组的指针。该数组将包含整数 0 <= x <= 52 并且我想构造一个长度为 48 的数组与 da kine 中不在传入数组中的每个整数一起。在Python中,这在C中是

# just included for specificity
cards = [card for card in deck if card not in hand]

最好的,我能做的就是

int i, j, k, found_flag;
int cards[48];    /* int hand[4] is passed in */

k = 0;
for (i = 0; i < 52; i++) {
  found_flag = 0;
  for (j = 0; j < 4; j++) {
    if (i == hand[j]) {
      found_flag = 1;
      break;
    }
  }
  if (!found_flag) {
      cards[k++] = i;
  }
}

这对于这种情况来说是最佳的吗?一般来说,是“首选”模式吗?

I'm writing a function that gets passed a pointer to an array of length 4. This array will contain integers 0 <= x <= 52 and I would like to construct an array of length 48 with every integer from da kine that's not in the passed in array. In python this would be

# just included for specificity
cards = [card for card in deck if card not in hand]

in C the best I can do is

int i, j, k, found_flag;
int cards[48];    /* int hand[4] is passed in */

k = 0;
for (i = 0; i < 52; i++) {
  found_flag = 0;
  for (j = 0; j < 4; j++) {
    if (i == hand[j]) {
      found_flag = 1;
      break;
    }
  }
  if (!found_flag) {
      cards[k++] = i;
  }
}

Is this optimal for this circumstance? Generally, is the 'go-to' pattern?

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

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

发布评论

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

评论(4

爱本泡沫多脆弱 2024-09-22 21:38:36

当然,你的例子对于只有 4 手的大小来说是很好的——它足够清楚了。在数组更大的情况下,可以使用基于各种排序的更有效的算法。

例如,基数排序消除了嵌套循环:

int i, j;
int card_in_hand[52] = { 0 };
int cards[48];    /* int hand[4] is passed in */

for (i = 0; i < 4; i++)
    card_in_hand[hand[i]] = 1;

j = 0;
for (i = 0; i < 52; i++)
  if (!card_in_hand[i])
      cards[j++] = i;

Sure, your example is fine for a hand size of only 4 - it's clear enough. In situations where the arrays were much larger, then more efficient algorithms based on various kinds of sorting could be used.

For example, a radix sort eliminates the nested loops:

int i, j;
int card_in_hand[52] = { 0 };
int cards[48];    /* int hand[4] is passed in */

for (i = 0; i < 4; i++)
    card_in_hand[hand[i]] = 1;

j = 0;
for (i = 0; i < 52; i++)
  if (!card_in_hand[i])
      cards[j++] = i;
帅气尐潴 2024-09-22 21:38:36

可以这样吗?

cards_in_deck[48]={1};
for (int i=0;i<4;i++)
    cards_in_deck[hand[i]]=0;

cards_in_deck 是一个数组,对于不在牌组中的卡片,其值为 1。这是您要找的吗?

Could this be done this way?

cards_in_deck[48]={1};
for (int i=0;i<4;i++)
    cards_in_deck[hand[i]]=0;

The cards_in_deck is an array with value of 1 for those that are not in the deck. Is this what you are looking for ?

冷清清 2024-09-22 21:38:36

这是我为解决这个问题而编写的小测试程序。它创建一个集合来显示选择了哪些卡片,并扫描该集合以构建剩余卡片的数组。

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    int cardsSelected[4] = {3,7,10,49} ;
    int cardsLeft[48] ;
    int allCards[52] ;

    memset(allCards,0,sizeof(int)*52) ;
    for(int i= 0; i < 4; ++i) {
        allCards[cardsSelected[i]] = 1 ;
    }

    int k = 0 ;
    for(int i =0; i < 52; ++i) {
        if (!allCards[i])
            cardsLeft[k++] = i ;
    }

    for(int i = 0; i < 48; ++i) {
        printf("%d ", cardsLeft[i]) ;
    }
    printf("\n") ;

    return 0;
}

Here's the little test program I put together to solve this one. It creates a set to show which cards are selected and scans through the set to build the array of the cards that are left.

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    int cardsSelected[4] = {3,7,10,49} ;
    int cardsLeft[48] ;
    int allCards[52] ;

    memset(allCards,0,sizeof(int)*52) ;
    for(int i= 0; i < 4; ++i) {
        allCards[cardsSelected[i]] = 1 ;
    }

    int k = 0 ;
    for(int i =0; i < 52; ++i) {
        if (!allCards[i])
            cardsLeft[k++] = i ;
    }

    for(int i = 0; i < 48; ++i) {
        printf("%d ", cardsLeft[i]) ;
    }
    printf("\n") ;

    return 0;
}
笔落惊风雨 2024-09-22 21:38:36

在 C 中,迭代手中已排序的卡片:

int cards_in_deck[48];
const int ranges[6][2] = {
    {0,           hand[0]}, 
    {hand[0] + 1, hand[1]},
    {hand[1] + 1, hand[2]},
    {hand[2] + 1, hand[3]},
    {hand[3] + 1, hand[4]},
    {hand[4] + 1, 52}
};
int j = 0;
for (int i = 0; i < sizeof(ranges)/sizeof(ranges[0]); i++) {
    const int *range = ranges[i];
    for (int k = range[0]; k < range[1]; k++)
        cards_in_deck[j++] = k;
}

在 python 中,代码如下所示:

hand = [0, 10, 11, 40, 51]
ranges = [
    [0,           hand[0]], 
    [hand[0] + 1, hand[1]],
    [hand[1] + 1, hand[2]],
    [hand[2] + 1, hand[3]],
    [hand[3] + 1, hand[4]],
    [hand[4] + 1, 52]
]
cards_in_deck = [k for r in ranges for k in range(r[0], r[1])]

In C, iterate over the sorted cards in hand:

int cards_in_deck[48];
const int ranges[6][2] = {
    {0,           hand[0]}, 
    {hand[0] + 1, hand[1]},
    {hand[1] + 1, hand[2]},
    {hand[2] + 1, hand[3]},
    {hand[3] + 1, hand[4]},
    {hand[4] + 1, 52}
};
int j = 0;
for (int i = 0; i < sizeof(ranges)/sizeof(ranges[0]); i++) {
    const int *range = ranges[i];
    for (int k = range[0]; k < range[1]; k++)
        cards_in_deck[j++] = k;
}

In python the code looks like this:

hand = [0, 10, 11, 40, 51]
ranges = [
    [0,           hand[0]], 
    [hand[0] + 1, hand[1]],
    [hand[1] + 1, hand[2]],
    [hand[2] + 1, hand[3]],
    [hand[3] + 1, hand[4]],
    [hand[4] + 1, 52]
]
cards_in_deck = [k for r in ranges for k in range(r[0], r[1])]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文