2013-02-02 73 views
0

因此,我一直在Objective-C中處理Project Euler問題25,並針對大小限制NSDecimalNumber運行。因此,在嘗試使用其他代碼庫之後,我決定重新表示我的斐波那契數,首先是int的數組,然後是NSNumberNSArray的數組。陣列中的每個單元都是一個數字。然後,我可以將數字逐個數字一起添加到第1000位。不幸的是出現了問題,我的進位是正確的,但最後進位前的數字似乎總是爲零。將對象插入到NSMutableArray中時出現意外行爲

我試過計算第8個斐波那契數和第12個斐波那契數並得到相同的行爲。而不是得到13和144我得到10和104.我總結數字正確,但

[nextFib insertObject: [NSNumber numberWithInt:digitSum] atIndex: arrayIndex]; 

似乎並沒有做我所期望的。 digitSum在這兩種情況下都是3和4,但是一旦我的方法返回下一個斐波那契數字作爲NSArray,我有0的地方,我期望3或4.我試過了它,我很困惑。它似乎在某些時候有效,但其他時候我在飛行中創建的NSNumber沒有我期望的值。這裏是我的整個方法:

+ (NSArray*) nextBigFibonancci: (NSArray*) fibZero After: (NSArray*) fibOne 
{ 
    // It's come to manually adding digits in one thousand count arrays. 
    // I can't return or pass arrays... will have to use NSArrays for everything, version at least 4.0 
    // Since XCode 4.5 I can use array[i] and other array literals... Lets just get it working... 

    // Works for Fib 1 and Fib 2 and Fib 3, but not Fib 12... 

    int fibZeroDigits = [fibZero count]; 
    int fibOneDigits = [fibOne count]; 
    NSMutableArray* nextFib = [NSMutableArray arrayWithCapacity:1000]; 
    NSArray* number; 
    int arrayIndex = 999; 
    int cellCount = fibZeroDigits - 1; 
    int digitZero, digitOne, digitSum; 

    // There is an Objective-c loop structure for looping through all objects in array, but stick to this... 
    for (int j = 0; j < 1000; j++) 
    { 
    // All the integers must be zero. 
    [nextFib insertObject: [NSNumber numberWithInt:0] atIndex: j]; 
    } 

    for (int i = fibOneDigits; i > 0; i--) 
    { 
    digitZero = [[fibZero objectAtIndex: cellCount] intValue]; 
    digitOne = [[fibOne objectAtIndex: i - 1] intValue]; 
    NSLog(@"arrayIndex is: %i", arrayIndex); // arrayIndex seems correct why am I getting 104? 
    if (digitZero + digitOne < 10) 
    { 
     digitSum = digitZero + digitOne + [[nextFib objectAtIndex: arrayIndex] intValue]; 
     [nextFib insertObject: [NSNumber numberWithInt:digitSum] atIndex: arrayIndex]; 
    } 
    else 
    { 
     digitSum = digitZero + digitOne - 10 + [[nextFib objectAtIndex:arrayIndex] intValue]; 
     // This isn't working the second time, though digitSum is added correctly... 
     // Getting 1,0,4 for fibTwelve instead of 144 
     // Doesn't work for fibEight get 1,0 instead of 13... 
     [nextFib insertObject: [NSNumber numberWithInt:digitSum] atIndex: arrayIndex]; 
     [nextFib insertObject: [NSNumber numberWithInt: 1] atIndex: arrayIndex -1]; 
    } 
    arrayIndex = arrayIndex - 1; 
    cellCount = cellCount - 1; 
    } 
    // Must carry the last digit in fibOne if arrays are of different sizes... 

    if (fibZeroDigits < fibOneDigits) 
    { 
    // fibOne has one extra digit 
    digitSum = [[fibOne objectAtIndex:0] intValue] + [[nextFib objectAtIndex:arrayIndex - 1] intValue]; 
    [nextFib insertObject:[NSNumber numberWithInt:digitSum] atIndex:arrayIndex -1]; 
    } 

    // Shouldn't return nextFib, but only the signifigant, ie non zero integers 

    // Find first non zero digit and then the range from there until the end of the array nextFib 
    for(int n = 0; n < 1000; n++) 
    { 
    if ([[nextFib objectAtIndex: n] intValue] > 0) 
    { 
     // First non zero digit. 
     NSRange theRange; 

     theRange.location = n; 
     theRange.length = 1000 - n; 

     number = [nextFib subarrayWithRange:theRange]; 
     break; // Could set n = 1000 which would also break... 
    } 
    } 


    return number; 
} 

爲什麼digitSum,我用它創建NSNumber攜帶必要時無法按預期獲得存儲任何想法?

+1

我的最大的錯誤是由於使用insertObjectAtIndex:而不是replaceObjectAtIndex:withObject:但上面的代碼中也存在其他錯誤。 – Muskie

+0

恭喜,您對問題25的回答是正確的。 您是第60154位來解決此問題的人。 – Muskie

+0

我修復了我的代碼,然後從我的main.m中運行,直到我得到1000位長的字。 – Muskie

回答

1

正如上面提到的,我發現了自己的bug和更多。 insertObjectAtIndex向NSMutableArray添加一個全新的對象,我反而想要replaceObjectAtIndexWith,它用新計算的數字代替前一個數字。

XCode剛剛更新,添加了從調試器中查看NSArrays的功能,如上面我提到的那樣,它本來不錯。這裏是添加兩個1000位數字的方法,它可以修改爲添加更大的數字,它們甚至不必是斐波那契數字。

+ (NSArray*) nextBigFibonancci: (NSArray*) fibZero After: (NSArray*) fibOne 
{  
    int fibZeroDigits = [fibZero count]; 
    int fibOneDigits = [fibOne count]; 
    int loops = fibZeroDigits - 1; 
    int fibOneIndex = loops; 
    NSMutableArray* nextFib = [NSMutableArray arrayWithCapacity:1000]; 
    NSArray* number; 
    int arrayIndex = 999; 
    int digitZero, digitOne, digitSum; 

    // There is an Objective-c loop structure for looping through all objects in array, but I'll just stick to this... 
    for (int j = 0; j <= arrayIndex; j++) 
    { 
     // All the integers start at zero. 
     [nextFib insertObject: [NSNumber numberWithInt:0] atIndex: j]; 
    } 

    if (fibOneDigits > fibZeroDigits) 
    { 
     fibOneIndex++; 
    } 

    for (int i = loops; i >= 0; i--) 
    { 
     digitZero = [[fibZero objectAtIndex: i ] intValue]; 
     digitOne = [[fibOne objectAtIndex: fibOneIndex ] intValue]; 
     // Have to use replaceObjectAtIndex not insertObjectAtIndex! 
     digitSum = digitZero + digitOne + [[nextFib objectAtIndex: arrayIndex] intValue]; 
     if (digitSum < 10) 
     { 
      [nextFib replaceObjectAtIndex: arrayIndex withObject: [NSNumber numberWithInt:digitSum]]; 
     } 
     else 
     { 
      digitSum = digitSum - 10; 
      [nextFib replaceObjectAtIndex: arrayIndex withObject: [NSNumber numberWithInt:digitSum]]; 
      [nextFib replaceObjectAtIndex: arrayIndex -1 withObject: [NSNumber numberWithInt:1]]; 
     } 
     arrayIndex = arrayIndex - 1; 
     fibOneIndex = fibOneIndex -1; 
    } 
    // Must carry the last digit in fibOne if arrays are of different sizes... 

    if (fibZeroDigits < fibOneDigits) 
    { 
     // fibOne has one extra digit 
     digitSum = [[fibOne objectAtIndex:0] intValue] + [[nextFib objectAtIndex:arrayIndex] intValue]; 
     [nextFib replaceObjectAtIndex: arrayIndex withObject: [NSNumber numberWithInt:digitSum]]; 
    } 

    // Shouldn't return nextFib, but only the signifigant, ie non zero integers 
    // Find first non zero digit and then the range from there until the end of the array nextFib 
    for(int n = 0; n < 1000; n++) 
    { 
     if ([[nextFib objectAtIndex: n] intValue] > 0) 
     { 
      // First non zero digit. 
      NSRange theRange; 

      theRange.location = n; 
      theRange.length = 1000 - n; 

      number = [nextFib subarrayWithRange:theRange]; 
      break; // Could set n = 1000 which would also break... 
     } 
    } 


    return number; 
} 
0

我還使用NSNumbers NSArray來解決這個問題。如果您按相反順序存儲斐波納契數字的數字,則可以使用更少的代碼。

我創建了一個帶有2個參數的方法,兩個NSArrays都代表Fibonacci數字,它返回一個代表下一個數字的NSArray。因此,要通過加入89和144來計算出第13個斐波那契數,方法的兩個參數的值是[9,8] & [4,4,1],並返回[3,3,2]。

我想你會發現用這種方式「攜帶一個」更簡單。顯然there's also a way to solve this on paper如果你是一個天才數學家。

+0

我知道我的答案不是最優的,它甚至不能工作,但我仍然想知道爲什麼當調試器中digiSum = 3時,我創建一個帶有digiSum的NSNumber並將其存儲在一個NSMutableArray中,然後獲取一個子數組從這個方法返回它,當我讀取NSNumber的intValues出來時,我的3變爲0? – Muskie

+0

你最近對這個問題的評論說你解決了這個問題。這是否意味着你已經回答了你自己的問題並且你的代碼正在工作? –

+0

我的代碼正在工作,我是否應該更多地關閉該線程?我不認爲我有權力。問題是insertObjectAtIndex添加了一個新對象,並將所有內容向左或向右移動,其中replaceObjectAtIndexWith將當前的零位替換爲我計算的零位,但還有其他錯誤。 Xcode更新能夠檢查NSArrays和NSMutableArrays,這將有助於調試,但我只是使用NSLog ... – Muskie

相關問題