0

我正在尝试用 6 个不同字符中的 4 个填充数组,其中数组中的每个字符都不同。放入数组的字符基于 switch 语句。然后我想通过数组返回以确保我刚刚放置的字符不在数组中,如果是,我必须放入不同的字符。我该怎么做呢?我不想重新填充整个阵列,只是这一点。

这是我到目前为止的代码,包括评论:

-(void) fillarray{

for (int i=0; i<4; i++)
{
    int randomNum = arc4random() % 6; // generates a random number

          switch (randomNum) // based on the number generated, it choses what color will be placed into the slot
    {
        case 1:
            colorarray[i] ='r';
            break;

        case 2:
            colorarray[i] ='o';
            break;

        case 3:
            colorarray[i] ='y';
            break;

        case 4:
            colorarray[i] ='g';
            break;
        case 5:
            colorarray[i] = 'b';

        case 6:
            colorarray[i] = 'p';

        default:
            break;
    }

    for (int j=0; j<4; j++) // runs through the array to ensure that the recently placed color does not match previous placed colors
    {
        while (j /= i)

            if (colorarray[i]==colorarray[j]) // if the color is already in the array
                                            //try again to place another color in this location

    }
4

5 回答 5

1

我会使用这个,基于Fisher-Yates 洗牌算法

NSArray * array = @[ @"r", @"o", @"y", @"g", @"b", @"p" ] ;
array = [ [ array shuffledArray ] subarrayWithRange:(NSRange){ .length = 4 } ] ;

在哪里-shuffledArray添加到 NSArray 通过:

@implementation NSArray ( Shuffling )

-(NSArray*)shuffledArray
{
    NSMutableArray * result = [ NSMutableArray arrayWithCapacity:self.count ] ;
    [ result addObject:self[0] ] ;
    for( NSUInteger index=1, count = self.count; index < count; ++index )
    {
        int j = arc4random_uniform( (unsigned int)index ) ;
        [ result addObject:result[j] ] ;
        [ result replaceObjectAtIndex:j withObject:self[ index ] ] ;
    }

    return result ;
}

@end
于 2012-09-29T01:09:18.650 回答
1

首先,您获取可以提取的所有可能字符的数组(您也可以使用 NSArray,在这里我假设您想使用 C 样式的数组):

char characters[]={'r','o','y','g','b','p'};
int length=6;

然后每次提取一个字符时,都会减小长度变量并将最后一个提取的字符与最后一个交换,以确保它不会再次被使用:

int randomNum = arc4random() % length;
< put characters[randomNum] somewhere>
char temp=characters[randomNum];
characters[randomNum]=characters[length-1];
characters[length-1]=temp;
length--;    

PS:% 操作数返回商的余数,所以数字 %N 永远不会是 N,它的范围是从 0 到 N-1。

于 2012-09-28T19:37:16.397 回答
0

这是另一种洗牌数组的方法(获取随机的前 X 个元素)。基本上,你按照 Ramy Alzury 和 nielsbot 的反应去做。算法是这样的。

Do this 10 times the length of the array
    swap the last element with a random element chosen from the array

最后,这应该产生一个随机打乱的数组(你正在打乱它 10 次)

这是Objective C中的一个示例函数。

-(NSArray *) shuffleArray:(NSArray *) arr{
    NSMutableArray * mArr = [NSMutableArray arrayWithArray:arr];
    for(int i=0;i<arr.count*10;i++){
        int num = arc4random()%arr.count;
        NSString * temp = (NSString *)[mArr objectAtIndex:arr.count-1];
        [mArr replaceObjectAtIndex: arr.count-1 withObject:[mArr objectAtIndex:num]]; 
        [mArr replaceObjectAtIndex:num withObject:temp];
    }
    return [NSArray arrayWithArray:mArr];
}
于 2012-10-02T03:25:48.950 回答
0

此外,如果您不想使用 switch 或(本质上)通过递减长度变量来删除从数组中接收到的字符,您总是可以这样做:

char colorarray[5] = {0,0,0,0,'\0'};
char possibleCharacters[6] = {'r', 'o', 'y', 'g', 'b', 'p'};
BOOL isNotUnique = NO;

for(int i = 0; i < 4; i++){
    do{
        colorarray[i] = possibleCharacters[arc4random() % 6];
        for(int j = i - 1; j > -1; --j){
            if(colorarray[i] == colorarray[j]){
                isNotUnique = YES;
                break;
            }
            else isNotUnique = NO;
        }
    }while(isNotUnique);
}

NSLog(@"%s", colorarray);

获取字符的实际循环基本上是:

1    for each spot in the colors arrays
2        generate a random number and use it to get a character
3        check to see if the character is identical to any spot before this one
4        if our new character matched any previous one, repeat from 2 on
于 2012-09-28T20:09:52.697 回答
0

该函数应如下所示:

    int found = 1;
    for (int i=0; i<4; i++)
    {
        while(found) {
           int randomNum = arc4random() % 6;
           switch (randomNum) 
           {
              case 1:
                colorarray[i] ='r';
                break;

              case 2:
                colorarray[i] ='o';
                break;

             case 3:
               colorarray[i] ='y';
               break;

             case 4:
               colorarray[i] ='g';
               break;
             case 5:
               colorarray[i] = 'b';
               break;

             case 6:
               colorarray[i] = 'p';

             default:
               break;
           }
           found = 0;
           for (int j = i-1; i >= 0; j--) {
              if (colorarray[i] == colorarray[j])
                 found = 1;
           }
        }
 }
于 2012-09-28T19:36:56.597 回答