因此,我一直在Objective-C中處理Project Euler問題25,並針對大小限制NSDecimalNumber運行。因此,在嘗試使用其他代碼庫之後,我決定重新表示我的斐波那契數,首先是int
的數組,然後是NSNumber
的NSArray
的數組。陣列中的每個單元都是一個數字。然後,我可以將數字逐個數字一起添加到第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
攜帶必要時無法按預期獲得存儲任何想法?
我的最大的錯誤是由於使用insertObjectAtIndex:而不是replaceObjectAtIndex:withObject:但上面的代碼中也存在其他錯誤。 – Muskie
恭喜,您對問題25的回答是正確的。 您是第60154位來解決此問題的人。 – Muskie
我修復了我的代碼,然後從我的main.m中運行,直到我得到1000位長的字。 – Muskie