Cocoa Touch - 比较Ints

时间:2010-08-15 05:10:13

标签: cocoa-touch

我有一个简单的问题。我将生成3个随机数,范围从0到2,我想确定是否有任何重复。

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

if (num1 == num2) {
}
else if (num1 == num3) {
}
else if (num2 == num3) {
}
else {
     //There are no dups.
}

检查是否有重复。

if (num1 == num2) {
     counter++;
}
if (num1 == num3) {
     counter++;
}
if (num2 == num3) {
     counter++;
}

这可以找到有多少重复项(额外奖励)。

编辑:

对于x数量的数字,您可能想要这样做(使用10作为我的示例数量的整数):

int counter = 0;
int i[10] = {
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};

for (int g = 0; g < 10; g++)
{
    for (int j = g+1; j < 10; j++)
    {
        if(i[g] == i[j])
        {
            counter++;
            printf(@"%d\n", counter);
            //If this if statement is true then there is a dup... In this case none are found.
        }
    }
}

答案 1 :(得分:0)

这个怎么样?

NSArray *randomNumbers  = [[NSArray alloc] initWithObjects:@"0",@"1",@"1",@"2",nil];    
NSMutableDictionary *occurenceDict = [[NSMutableDictionary alloc] init];

for (NSString *number in randomNumbers) 
{
    if ([occurenceDict objectForKey:number] == nil) {
        [occurenceDict setValue:[NSNumber numberWithInt:[number intValue]] forKey:number];
        int occOfNum = 0;
        for (int i = 0; i < [randomNumbers count]; i++) {
            NSString *currentNumber = [randomNumbers objectAtIndex:i];
            if ([currentNumber compare:number] == NSOrderedSame) {
                occOfNum++;
            }
        }
        [occurenceDict setValue:[NSNumber numberWithInt:occOfNum] forKey:number];
    }
}

for (NSString *key in occurenceDict) {
    NSString *occurrences = [occurenceDict objectForKey:key];
    NSLog(@"Number %d is contained %d times", [key intValue], [occurrences intValue]);
}

[randomNumbers release];
[occurenceDict release];

<强>输出:

Number 0 is contained 1 times
Number 1 is contained 2 times
Number 2 is contained 1 times

修改:如果你想知道它是如何工作的,这里是相同的版本,但有评论可以帮助你理解它:

// Create array with the numbers that we have randomly generated
NSArray *randomNumbers  = [[NSArray alloc] initWithObjects:@"0",@"1",@"1",@"2",nil];    
NSMutableDictionary *occurenceDict = [[NSMutableDictionary alloc] init];

for (NSString *number in randomNumbers) 
{
    // If this number has not been added to the dictionary
    if ([occurenceDict objectForKey:number] == nil) {
        // Add it
        [occurenceDict setValue:[NSNumber numberWithInt:[number intValue]] forKey:number];
        // Find how many times the number occurs with the "randomNumbers" array
        int occOfNum = 0;
        for (int i = 0; i < [randomNumbers count]; i++) {
            NSString *currentNumber = [randomNumbers objectAtIndex:i];
            if ([currentNumber compare:number] == NSOrderedSame) {
                // We found this number at this index, so increment the found count
                occOfNum++;
            }
        }
        // Save the number of times which "number" occurs in the dictionary for later
        [occurenceDict setValue:[NSNumber numberWithInt:occOfNum] forKey:number];
    }
}

// Iterate through all items in the dictionary and print out the result
for (NSString *key in occurenceDict) {
    NSString *occurrences = [occurenceDict objectForKey:key];
    NSLog(@"Number %d is contained %d", [key intValue], [occurrences intValue]);
}
// Release alloc'ed memory
[randomNumbers release];
[occurenceDict release];

答案 2 :(得分:0)

Crikey,这些答案都是啰嗦!将随机生成的数字放入NSIndexSet。在插入一个数字之前测试该组,你就会知道这个数字已经存在,所以是一个骗局。