简体   繁体   English

值超出目标c的算术运算

[英]Arithmetic operation of value that exceeds limit in objective c

I have been trying the question on hacker rank in which addition of all the substring of a string is taken into account. 我一直在尝试关于黑客排名的问题,其中考虑了字符串的所有子字符串的加法。 In doing so our answer exceeds the limit of NSInteger, NSUInteger, long, very very long, double. 这样做,我们的答案超出了NSInteger,NSUInteger,长,非常长,两倍的限制。

So in this case i tried 所以在这种情况下,我尝试了

- (NSDecimalNumber *) substrings:(NSString *)n {

    NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    if ([n rangeOfCharacterFromSet:notDigits].location == NSNotFound) {

        NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
        f.numberStyle = NSNumberFormatterNoStyle;
        NSDecimalNumber *sum = [NSDecimalNumber decimalNumberWithString:@"0"];
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i<n.length; i++) {

            for (NSInteger j = 1; j<=n.length-i; j++) {

                NSString *stringInRange = [n substringWithRange:NSMakeRange(i, j)];
                [arr addObject:stringInRange];
                NSDecimalNumber *myNumber1 = [NSDecimalNumber decimalNumberWithString:stringInRange];
                sum = [sum decimalNumberByAdding:myNumber1];
            }
        }
        return sum;
    } else {

        return nil;
    }
}

But still, I am getting the wrong answer of addition. 但仍然,我得到加法的错误答案。 I want to know in such cases where test cases include much heavier value than the limits, what API should we use to perform arithmetic operation? 我想知道在这种情况下,测试用例包含的值比限制值大得多,我们应该使用哪种API来执行算术运算?

What I have tried is a correct approach or not? 我尝试过的方法是否正确?

If you need to operate with big integer values in Objective-C you can use JKBigInteger . 如果需要在Objective-C中使用大整数值进行操作,则可以使用JKBigInteger For example, you can multiply 2 big integers and convert result to NSString with JKBigInteger. 例如,您可以将2个大整数相乘,然后使用JKBigInteger将结果转换为NSString Here you can see how to use this library: 在这里,您可以看到如何使用此库:

JKBigInteger *bigInteger1 = [[JKBigInteger alloc] initWithString:@"2"];
JKBigInteger *bigInteger2 = [[JKBigInteger alloc] initWithString:@"3"];
JKBigInteger* bigInteger3 = [bigInteger1 add:bigInteger2];
NSLog(@"%@", [bigInteger3 stringValue]); // output is 5

And I guess you can get values that you need with this function: 而且我想您可以使用此功能获得所需的值:

-(JKBigInteger*)sumOfSubValues:(JKBigInteger*)value {
    JKBigInteger* currentValue = [[JKBigInteger alloc] initWithString:value.stringValue];
    JKBigInteger* result = [[JKBigInteger alloc] initWithString:currentValue.stringValue];
    NSUInteger digitsCount = currentValue.stringValue.length - 1;
    JKBigInteger* const tenValue = [[JKBigInteger alloc] initWithString:@"10"];
    while (digitsCount != 0) {
        JKBigInteger* const firstDigit = [[JKBigInteger alloc] initWithString:[currentValue.stringValue substringToIndex:1]];
        JKBigInteger* const firstDigitPowerBase = [tenValue pow:(unsigned int)digitsCount];
        JKBigInteger* const firstDigitPower = [firstDigit multiply:firstDigitPowerBase];
        JKBigInteger* const diff = [currentValue subtract:firstDigitPower];
        currentValue = diff;
        digitsCount--;
        result = [result add:currentValue];
    }
    return result;
}

You can test it with this code snippet: 您可以使用以下代码段进行测试:

JKBigInteger* result = [self sumOfSubValues:[[JKBigInteger alloc] initWithString:@"1234"]];
NSLog(@"%@", result.stringValue); // output is "1506" (1234 + 234 + 34 + 4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM