回答
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.
}
}
}
如果要測試10個隨機數,會發生什麼情況?它會變得很長 – 2010-08-15 06:07:03
Brock Woolf:是的。更新了答案。 :) – 2010-08-15 06:29:16
編輯沒有必要我只需要測試3個隨機數字。但是,謝謝你的努力! – user377419 2010-08-15 13:46:20
這個怎麼樣?
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];
Crikey,這些答案很囉嗦!把你隨機生成的數字放入NSIndexSet
。在插入一個數字之前先測試一下這個數字,你就會知道這個數字已經存在了,而且這個數字已經存在了。
- 1. Cocoa Touch - 假設延遲比較
- 2. basic haskell:比較一組Ints和一組int集合ints
- 3. Cocoa-touch和UIButtonContent
- 4. NSNumberFormatter for Cocoa-touch?
- 5. 比較sencha touch 2.3.0與2.2.1
- 6. Cocoa-Touch UIButton isSelected clarification
- 7. initWithCoder中NSCoder的用途:? (Cocoa和Cocoa Touch)
- 8. 開源Cocoa/Cocoa-Touch POP3/SMTP庫?
- 9. Cocoa-Touch - 連鎖動畫
- 10. Cocoa Touch中的動畫?
- 11. 循環添加UIImageView Cocoa Touch
- 12. Cocoa Touch - 替代viewDidLoad方法
- 13. NSCollectionView for Cocoa-Touch(iPhone/iPad)
- 14. Cocoa-Touch - 委託混淆
- 15. Cocoa Touch - 更改tex屬性?
- 16. Cocoa Touch藍牙TicTacToe(GameKit)
- 17. Cocoa Touch上的AES加密
- 18. Cocoa Touch - 按住按鈕
- 19. Cocoa-Touch如何:MPMoviePlayerController循環?
- 20. Cocoa-Touch字符串拆分
- 21. Cocoa-touch - 實現文件?
- 22. Cocoa-Touch:問題循環MPMoviePlayerController
- 23. C#從字符串中提取Ints並比較
- 24. cocoa-touch:爲什麼不應用字體?
- 25. 在Cocoa/Cocoa Touch中監視一個目錄
- 26. 在Cocoa和Cocoa-Touch應用程序之間共享常量
- 27. 在Cocoa中切換視圖(不是Cocoa Touch)
- 28. Cocoa/Cocoa Touch UI元素的圖像存儲在哪裏?
- 29. objective-c/cocoa-touch - 兩個NSString之間的比較失敗,即使它們是相同的
- 30. 什麼是Cocoa Touch UIKit佈局
你想要int數據類型嗎?或者你想要NSInteger? – 2010-08-15 06:31:43