1

Does anyone know any efficient ways to check if any set of integers contains an integer that has changed by a certain amount.

For example I have:

int1 = 10;
int2 = 20;
int3 = 30;

And I want to know if any of these 3 integers change by 30

Now if int1 becomes 40 than the call should be triggered. At first I thought of just doing something like this.

if (abs((int1+int2+int3)-(newInt1+newInt2+newInt3)) >= 30) {

But many problems can arise from this...

  • False triggers (e.g. each new int value increases by 10, making the NET change greater than 30 but not necessarily any individual int greater than 30)
  • Untriggered reactions (e.g. one of the new integer values has increased by 50 so it should be called but then another new integer value is decreased by 50 (again, it should be called) but the net change is now zero because -50+50=0)

Does anyone have any efficient way of doing this? (Yes, I know obviously I could just check each value individually with OR statements...)

So far this is my best stab at it

if ((((abs(int1-newInt1))>=30)+((abs(int2-newInt2))>=30)+((abs(int3-newInt3))>=30))>0) {

But that's basically the same as using an OR statement (probably even takes a little longer than an OR statment.

4

2 回答 2

1

我不认为你能得到比这更快的速度,除非你处理数亿个整数,否则这不应该带来显着的性能损失。


但是,您可能想变得“聪明”。如果你以某种方式“校验和”这两个总和怎么办?例如,将所有新旧数字与第 n 个素数相乘,然后检查新旧和除以index第 th 个素数的差是否是您想要的数量。

int sum(int arr[], size_t n)
{
    int n = 0;
    for (int i = 0; i < n; i++)
        n += primes[i] * arr[i];

    return n;
}

int primes[3] = { 2, 3, 5 }; // or more
int olds[3] = { 10, 20, 30 };
int news[3] = { 40, 20, 30 };

int nth = 0; // check first
int change_expected = 30;
int oldsum = sum(olds, 3);
int newsum = sum(news, 3);
if ((newsum - oldsum) / primes[nth] == change_expected) {
    // 1st value changed as expected
}

请注意,与您的幼稚方法相比,这将花费更多的时间和 CPU 周期。

于 2013-05-14T13:15:14.493 回答
0

由于您使用的是objective -c,因此您始终可以创建一个完全符合您要求的对象(参见示例下方的类)。这样做的主要好处是您不必在每次设置一个整数时检查每个整数。您只需检查每个数字的变化,看看它是否变化太大。

使用示例:

// myNumbers.confined will be true until you create a number, and THEN change it by 30 or more.
ARConfinedNumbers *myNumbers = [ARConfinedNumbers new];
[myNumbers addNumber:10];
[myNumbers addNumber:20];
[myNumbers addNumber:30];

[myNumbers replaceNumberAtIndex:0 withObject:40];

// No longer confined because we have changed 10 to 40
if (!myNumbers.confined)
    NSLog(@"Not confined.");

// Reset
[myNumbers setConfined:YES];

这是我为此编写的课程。请注意,我使用了NSArray,假设您正在为 iOS/MacOS 编程,但如果您不使用这些类,则可以将 NSArray 替换为其他内容。不过,这应该给你一个很好的起点。

ARConfinedNumbers.h:

#import <Foundation/Foundation.h>

@interface ARConfinedNumbers : NSObject

// confined is true if the numbers have not been changed by more than 30.
// This can be reset by setting it to YES.
@property (nonatomic, assign) BOOL confined;

// Methods to manipulate the set of numbers
// Add more array-type-methods as needed
- (void)addNumber:(int)number;
- (void)replaceNumberAtIndex:(NSUInteger)index withObject:(int)number;
- (int)numberAtIndex:(NSUInteger)index;
- (NSUInteger)count;

@end

ARConfinedNumbers.m

#import "ARConfinedNumbers.h"

/* Private Methods */
@interface ARConfinedNumbers()

@property (nonatomic, strong) NSMutableArray *numbers;

@end

@implementation ARConfinedNumbers

- (id)init
{
    self = [super init];
    if (self)
    {
        _confined = YES;
        _numbers = [NSMutableArray new];

    }
    return self;
}

- (void)addNumber:(int)number
{
    [self.numbers addObject:@(number)];
}

- (void)replaceNumberAtIndex:(NSUInteger)index withObject:(int)number
{
    if (index < self.numbers.count)
    {
        if (number)
        {
            int existingNumber = [self.numbers[index] intValue];
            if (abs(existingNumber - number) >= 30)
                self.confined = NO;

            [self.numbers replaceObjectAtIndex:index withObject:@(number)];
        }
    }
}

- (int)numberAtIndex:(NSUInteger)index
{
    if (index < self.numbers.count)
        return [self.numbers[index] intValue];

    return 0;
}

- (NSUInteger)count
{
    return self.numbers.count;
}

@end
于 2013-07-18T03:40:47.040 回答