Ios 第14天

Ios 第14天,ios,objective-c,Ios,Objective C,我试图解决代码出现的第14天,我得到的是2660而不是2640。我做错了什么?我看过其他的解决方案,他们似乎也在遵循同样的方法 -(void)第14天:(NSArray*)输入 { NSMutableDictionary*ReinderDictionary=[[NSMutableDictionary alloc]init]; n错误*错误=nil; NSRegularExpression*regex=[NSRegularExpression regular expression with pat

我试图解决代码出现的第14天,我得到的是2660而不是2640。我做错了什么?我看过其他的解决方案,他们似乎也在遵循同样的方法

-(void)第14天:(NSArray*)输入
{
NSMutableDictionary*ReinderDictionary=[[NSMutableDictionary alloc]init];
n错误*错误=nil;
NSRegularExpression*regex=[NSRegularExpression regular expression with pattern:@”(\\w*)可以以(\\d*)km/s的速度飞行(\\d*)秒,但随后必须休息(\\d*)秒。选项:0错误:&error];
NSNumberFormatter*f=[[NSNumberFormatter alloc]init];
f、 numberStyle=NSNumberFormatterDecimalStyle;
如果(错误){
NSLog(@“正则表达式格式错误:%@”,错误);
}
for(输入中的NSString*输入)
{
NSArray*matches=[regex matchesInstalling:input options:0 range:NSMakeRange(0,input.length)];
for(NSTextCheckingResult*匹配结果){
NSMutableDictionary*驯鹿=[[NSMutableDictionary alloc]init];
驯鹿[@“速度”]=[f numberFromString:[输入子字符串WithRange:[结果范围索引:2]];
驯鹿[@“flyTime”]=[f numberFromString:[输入substringWithRange:[结果范围索引:3]];
驯鹿[@“restTime”]=[f numberFromString:[输入子字符串WithRange:[结果范围索引:4]];
驯鹿[@“点数”]=@0;
驯鹿词典[[输入子字符串WithRange:[结果范围索引:1]]]=驯鹿;
}
}
int max秒=1000;
NSNumber*maxDistanceFlown=@0;
对于(int i=0;i maxDistanceFlown){
maxDistanceFlown=distanceFlown;
}
}
}
}
NSLog(@“第1部分:获胜距离:%@\n”,maxDistanceFlown);
}
我做错了什么

简短回答:使用迭代(你的
循环在几秒钟内迭代)

稍长一点的回答:

这个问题可以在几行内解决,无需任何迭代。这是一个谜题,所以没有代码,但有一个提示:把一个周期想象成一个单独的飞行+休息周期。任何行程时间都将由零个或多个完整循环和零个或一个部分循环组成

附录

我不相信你的解决方案,尽管过于复杂,却得出了错误的答案。什么使你认为这是错误的?按照问题中的示例运行1000秒,结果匹配

我做错了什么

简短回答:使用迭代(你的
循环在几秒钟内迭代)

稍长一点的回答:

这个问题可以在几行内解决,无需任何迭代。这是一个谜题,所以没有代码,但有一个提示:把一个周期想象成一个单独的飞行+休息周期。任何行程时间都将由零个或多个完整循环和零个或一个部分循环组成

附录


我不相信你的解决方案,尽管过于复杂,却得出了错误的答案。什么使你认为这是错误的?按照问题中的示例运行1000秒,结果匹配。

在1000秒时,我得到了正确的答案,谢谢。你建议我如何使解决方案不那么复杂?我已经更新了我的答案。与其把它看作是一种程序性的/信息性的方式,不如把它看作一道数学题。相反,计算驯鹿在1000秒内会跑多少次/暂停多少次,然后是什么样的长跑,以及它的状态。然后比较。我觉得@CRD建议的想法你的困难在于建模问题。在没有给出解决方案的情况下,我可以在提示中添加更多内容-考虑到四个输入值,我计算距离的代码只有两行,而且只有那么长,因为我很整洁。。。考虑计算最终结果,而不是重复步骤(你的循环)。在1000秒时,我得到了正确的答案,谢谢你。你建议我如何使解决方案不那么复杂?我已经更新了我的答案。与其把它看作是一种程序性的/信息性的方式,不如把它看作一道数学题。相反,计算驯鹿在1000秒内会跑多少次/暂停多少次,然后是什么样的长跑,以及它的状态。然后比较。我觉得@CRD建议的想法你的困难在于建模问题。在没有给出解决方案的情况下,我可以在提示中添加更多内容-考虑到四个输入值,我计算距离的代码只有两行,而且只有那么长,因为我很整洁。。。考虑计算最终结果,而不是重复步骤(循环)。
     - (void)day14:(NSArray *)inputs
{
    NSMutableDictionary *reindeerDictionary = [[NSMutableDictionary alloc] init];
    NSError *error = nil;

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\w*) can fly (\\d*) km/s for (\\d*) seconds, but then must rest for (\\d*) seconds." options:0 error:&error];
    NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
    f.numberStyle = NSNumberFormatterDecimalStyle;

    if(error) {
        NSLog(@"Error in regex formatting:%@", error);
    }

    for (NSString *input in inputs)
    {
        NSArray *matches = [regex matchesInString:input options:0 range:NSMakeRange(0,input.length)];
        for (NSTextCheckingResult *result in matches) {
            NSMutableDictionary *reindeer = [[NSMutableDictionary alloc] init];
            reindeer[@"speed"] = [f numberFromString:[input substringWithRange:[result rangeAtIndex:2]]];
            reindeer[@"flyTime"] = [f numberFromString:[input substringWithRange:[result rangeAtIndex:3]]];
            reindeer[@"restTime"] = [f numberFromString:[input substringWithRange:[result rangeAtIndex:4]]];
            reindeer[@"points"] = @0;
            reindeerDictionary[[input substringWithRange:[result rangeAtIndex:1]]] = reindeer;
        }
    }

    int maxSeconds = 1000;
    NSNumber *maxDistanceFlown = @0;


    for (int i = 0; i <= maxSeconds; i++) {
        for (NSString *reindeerName in reindeerDictionary.allKeys) {
            NSMutableDictionary *reindeer = reindeerDictionary[reindeerName];

            NSNumber *speed = reindeer[@"speed"];
            NSNumber *flyingPeriod = reindeer[@"flyTime"];
            NSNumber *restPeriod = reindeer[@"restTime"];
            int distanceFlown = [reindeer[@"distanceFlown"] intValue];
            int relativeSeconds = i % (restPeriod.intValue + flyingPeriod.intValue);

            //Check if going at full speed vs rest
            if (relativeSeconds < flyingPeriod.intValue) {
                distanceFlown += speed.intValue;
                reindeer[@"distanceFlown"] = @(distanceFlown);

                NSNumber *distanceFlown = reindeer[@"distanceFlown"];
                if (distanceFlown > maxDistanceFlown) {
                    maxDistanceFlown = distanceFlown;
                }
            }
        }
    }

    NSLog(@"Part 1: Winning Distance: %@\n",maxDistanceFlown);

}