在Objective-C中添加对象算法 [英] Adding objects algorithm in Objective-C

查看:66
本文介绍了在Objective-C中添加对象算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数组,其中有五个随机数,它们之间在1到10之间:

I have this array that has five random numbers between one to ten:

NSMutableArray *arrayOfNumbers = [NSMutableArray array];
    for (int x = 0; x < 5; x++)
    {
        [arrayOfNumbers addObject: [NSNumber numberWithInt: arc4random()%10]];
    }
    NSLog(@"%@",arrayOfNumbers);

我想将前两个和后两个对象加在一起。如果它们确实相加,我想返回2。如果它们不相加,我想检查第二个和第三个对象是否将最后两个对象相加。所以基本上,如果我有数组[2,3,9,4,1],那么我想检查是否2 + 3 = 4 + 1。在这种情况下,是的,所以我将返回2。在此数组[2,3,6,4,5] 2 + 3!= 4 + 5中,我们将继续检查3 + 6 = 4 + 5,因为它是返回2。现在,如果没有两个对象加在一起可以等于[0,1,2,3,4],那么我们将返回-1。

I want to take the first two and the last two objects and add them together. If they do add together, I want to return 2. If they don't add together, I want to check to see if the second and third object add up the last two objects. So basically if I have the array [2,3,9,4,1] then i want to check to see if 2+3=4+1. In this case, yes, so I would return 2. In this array [2,3,6,4,5] 2+3!=4+5 so we would move onto check if 3+6=4+5 and since it is we return 2. Now if there are no two objects that when added together can be equal [0,1,2,3,4] then we would return -1.

这是我到目前为止所做的:

This is what I have done so far:

    int lastValue = [arrayOfNumbers count];
    int secondlastValue = [arrayOfNumbers count] - 1;
    int firstValue = 0;
    int secondValue = 1;

    int i;
for (i = 0; i < [arrayOfNumbers count]; i++) {
    int one = [[arrayOfNumbers objectAtIndex:firstValue + i] integerValue];
    int two = [[arrayOfNumbers objectAtIndex:secondValue + i] integerValue];
    int secondtolast = [[arrayOfNumbers objectAtIndex:secondlastValue] integerValue];
    int last = [[arrayOfNumbers objectAtIndex:lastValue] integerValue];

    if (one + two == secondtolast + last) {
        NSLog(@"2: Because %i + %i = %i + %i",firstValue,secondValue,secondlastValue,lastValue);
        break;
    } else {
        NSLog(@"-1");
    }
}

但是由于某些原因,它崩溃了...任何帮助将不胜感激。谢谢!

But for some reason, it crashes... Any help would be appreciated. thanks!

编辑:这是错误 ***由于未捕获的异常'NSRangeException'而终止应用程序,原因:'***-[__ NSArrayM objectAtIndex:]:索引5超出范围[0 .. 4]'

编辑2 :
这些是我的NSLog

EDIT 2: These are my NSLog's

if ((one + two) == totalOfLastValues) {
            NSLog(@"2: Because %@ + %@ (%i) = %@ + %@",arrayOfNumbers[i],arrayOfNumbers[i+1],one+two,[arrayOfNumbers objectAtIndex:[arrayOfNumbers count] - 2],[arrayOfNumbers lastObject]);
            break;
        } else {
            NSLog(@"-1: Because %@ + %@ (%i) != %@ + %@",arrayOfNumbers[i],arrayOfNumbers[i+1],one+two,[arrayOfNumbers objectAtIndex:[arrayOfNumbers count] - 2],[arrayOfNumbers lastObject]);
        }


推荐答案

由于最终值而崩溃 i 指向的东西是数组中的最后一项。因此,您尝试查找的最后一件事是 secondValue + i ,它比数组中的最后一项过去了。

It crashes because the final value thing i points to is the last item in the array. So the final thing you attempt to look up is secondValue+i, which is one past the last item in the array. That raises the exception.

类似地,您的 lastValue secondLastValue 设置得太高。数组中的第一项是项 0 。因此,如果有两个项目,则最后一个项目是项目 1 。如果有 n 个项目,那么最后一个项目是 n-1

Similarly, your lastValue and secondLastValue are set to be one too high. The first item in an array is item 0. So if there are two items then the last item is item 1. If there are n items then the last item is n-1.

所以:

        int lastValue = [arrayOfNumbers count] - 1;
        int secondlastValue = [arrayOfNumbers count] - 2;
        int firstValue = 0;
        int secondValue = 1;

        int i;
    for (i = 0; i < [arrayOfNumbers count]-1; i++) {
        int one = [[arrayOfNumbers objectAtIndex:firstValue + i] integerValue];
        int two = [[arrayOfNumbers objectAtIndex:secondValue + i] integerValue];
        int secondtolast = [[arrayOfNumbers objectAtIndex:secondlastValue] integerValue];
        int last = [[arrayOfNumbers objectAtIndex:lastValue] integerValue];

        if (one + two == secondtolast + last) {
            NSLog(@"2: Because %i + %i = %i + %i",firstValue,secondValue,secondlastValue,lastValue);
            break;
        } else {
            NSLog(@"-1");
        }
    }

编辑:您还想避免考虑是否最后两个数字加在一起等于最后两个数字加在一起。否则,您将始终返回2。循环中执行该部分算法也没有意义。因此,第一个经过整理的版本可能看起来像:

you also want to avoid considering whether the last two numbers added together equal the last two numbers added together. Otherwise you'll just always return 2. There's also no point doing that part of the arithmetic inside the loop. So a first neatened version might look like:

    for (int x = 0; x < 5; x++)
    {
        [arrayOfNumbers addObject: @(arc4random_uniform(10))];
    }
    NSLog(@"%@",arrayOfNumbers);

    int lastValue = [[arrayOfNumbers lastObject] integerValue];
    int secondLastValue = [[arrayOfNumbers objectAtIndex:[arrayOfNumbers count] - 2] integerValue];
    int totalOfLastValues = lastValue + secondLastValue;
    int firstValue = 0;
    int secondValue = 1;

        int i;
    for (i = 0; i < [arrayOfNumbers count]-3; i++) {
        int one = [[arrayOfNumbers objectAtIndex:firstValue + i] integerValue];
        int two = [[arrayOfNumbers objectAtIndex:secondValue + i] integerValue];

        if ((one + two) == totalOfLastValues) {
            NSLog(@"2: Because %i + %i (%i) = %i",firstValue+i,secondValue+i,one+two,totalOfLastValues);
            break;
        } else {
            NSLog(@"-1");
        }
    }

清理并防止重复调用 integerValue 会导致:

Cleaning up and preventing duplicate calls to integerValue leads to:

    int lastValue = [[arrayOfNumbers lastObject] integerValue];
    int secondLastValue = [[arrayOfNumbers objectAtIndex:[arrayOfNumbers count] - 2] integerValue];
    int totalOfLastValues = lastValue + secondLastValue;

    int mostRecentValue  = [arrayOfNumbers[0] integerValue];
    for (int i = 1; i < [arrayOfNumbers count]-3; i++) {
        int one = mostRecentValue;
        int two = [arrayOfNumbers[i] integerValue];
        mostRecentValue = two;

        if ((one + two) == totalOfLastValues) {
            NSLog(@"2: Because %i + %i (%i) = %i",i,i+1,one+two,totalOfLastValues);
            break;
        } else {
            NSLog(@"-1");
        }
    }

这篇关于在Objective-C中添加对象算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆