2010-08-15 59 views
1

我有一個可能很簡單的問題。我將生成3個隨機數,範圍從0到2,我想確定是否有任何重複。Cocoa Touch - 比較Ints

任何想法?

+0

你想要int數據類型嗎?或者你想要NSInteger? – 2010-08-15 06:31:43

回答

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. 
     } 
    } 
} 
+0

如果要測試10個隨機數,會發生什麼情況?它會變得很長 – 2010-08-15 06:07:03

+0

Brock Woolf:是的。更新了答案。 :) – 2010-08-15 06:29:16

+0

編輯沒有必要我只需要測試3個隨機數字。但是,謝謝你的努力! – user377419 2010-08-15 13:46:20

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]; 
0

Crikey,這些答案很囉嗦!把你隨機生成的數字放入NSIndexSet。在插入一個數字之前先測試一下這個數字,你就會知道這個數字已經存在了,而且這個數字已經存在了。