算法编程题#iOS

1. 编程题:假设某一个生物每B年繁殖一次,一次繁殖N只,寿命是M年,编写程序起初有X只生物,求T年之后生物的总数是多少?

@interface Animal ()
/**
  假设某一个生物每B年繁殖一次,一次繁殖N只,寿命是M年,编写程序起初有X只生物,求T年之后生物的总数是多少?
  @param originalCount 起初X只生物
  @param yearsToBreed 每B年繁殖一次
  @param countPerBreed 一次繁殖N只
  @param limitAge 寿命M年
  @param totalYears T年之后
*/
- (void)solvePlanWithOriginalCount:(NSInteger)originalCount yearsToBreed:(NSInteger)yearsToBreed countPerBreed:(NSInteger)countPerBreed limitAge:(NSInteger)limitAge totalYears:(NSInteger)totalYears;

/**
  添加一定数量的Animal对象到数组
  @param number 数量
*/
- (void)addAnimalToArrayWithNumber:(NSInteger)number;

@property (nonatomic, strong) NSMutableArray *arr;//存放对象的数组
@property (nonatomic, assign) NSInteger age;//年龄
@end

@implementation Animal
- (void)solvePlanWithOriginalCount:(NSInteger)originalCount yearsToBreed:(NSInteger)yearsToBreed countPerBreed:(NSInteger)countPerBreed limitAge:(NSInteger)limitAge totalYears:(NSInteger)totalYears {
    self.arr = [[NSMutableArray alloc] init];
    [self addAnimalToArrayWithNumber:originalCount];//初始数量的对象添加到数组

    for (int year = 0; year < totalYears; year++) { 
        NSLog(@"第%zd年-->%.2zd只", year, _arr.count);
        NSInteger lastYearOfCount = _arr.count;//上一年的数量
        for (int index = 0; index < lastYearOfCount; index++) {
            Animal *animal = [_arr objectAtIndex:index];
            animal.age++;
            //死亡年
            if (animal.age >= limitAge) {
                [_arr removeObject:animal];
                index--;
                lastYearOfCount--;
                continue;
            }
            //出生年
            if (animal.age >= yearsToBreed){
                if (!(animal.age % yearsToBreed)) {
                    [self addAnimalToArrayWithNumber:countPerBreed];
                    continue;
                }
            }
        }
        if (year == (totalYears-1))
            NSLog(@"第%zd年-->%.2zd只", year+1, _arr.count);
    }

    NSLog(@"假设某一个生物每%zd年繁殖一次,一次繁殖%zd个只,寿命是%zd年,编写程序起初有%zd只生物,求%zd年之后生物的总数是%zd!", yearsToBreed, countPerBreed, limitAge, originalCount, totalYears, _arr.count);
}
- (void)addAnimalToArrayWithNumber:(NSInteger)number {
    if (!number) return;
    for (int i = 0; i < number; i++) {
        [_arr addObject:[[Animal alloc] init]];
    }
}
@end

2. 有M个数,从中取出N个不相同的数

/**
 有M个数,从中取出N个不相同的数
 @param randomCount M个数
 @param numCount N个数
 @param minCount 随机数最小值
 @param maxCount 随机数最大值
*/
- (void)solvePlanWithRandomCount:(NSInteger)randomCount toGetNumCount:(NSInteger)numCount minCount:(NSInteger)minCount maxCount:(NSInteger)maxCount {
    if (!randomCount && !numCount && (maxCount <= minCount)) return;

    NSMutableArray *randomArray = [NSMutableArray arrayWithCapacity:randomCount];//存放M个随机数的数组
    NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:numCount];//取出N个数的数组

    //初始化随机数数据
    for (int i = 0; i < randomCount; i++) {
        [randomArray addObject:[self getRandWithMin:minCount andMax:maxCount]];
    }

    //获取
    for (int j = 0; j < numCount; j++) {
        NSString *str = [randomArray objectAtIndex:arc4random_uniform((int)numCount)];
        if (![resultArray containsObject:str]) {
            [resultArray addObject:str];
        }
        else {
            j--;
        }
    }
}
/**
  获取一定范围的随机数
  @param minNum 最小值
  @param maxNum 最大值
*/
- (NSString *)getRandWithMin:(NSInteger)minNum andMax:(NSInteger)maxNum {
    return [NSString stringWithFormat:@"%zd", minNum + arc4random()%(maxNum - minNum)];
    //return [NSString stringWithFormat:@"%zd", minNum + arc4random_uniform(maxNum)];
}

3. 假如电子记时器所显示的十个数字是“0126093028”这样一串数,它表示的是1月26日9时30分28秒.在这串数里,“0”出现了3次,“2”出现了2次,“1”、“3”、“6”、“8”、“9”各出现1次,而“4”、“5”、“7”没有出现.如果在电子记时器所显示的这串数里,“0,1,2,3,4,5,6,7,8,9”这一个数字都只出现一次,称它所表示的时刻为“十全时”那么2003年一共有( )个这样的“十全时?

PS:算法完善打印每个十全时

- (void)get2003 {
    int result = 0;
    //每月多少天数组
    NSMutableArray *dayOfPerMonth = [NSMutableArray array];
    for (int i = 1; i < 13; i++) {
        [dayOfPerMonth addObject:[NSString stringWithFormat:@"%zd", [self howManyDaysInThisYear:2003 withMonth:i]]];
    }

    //二维数组创建
    NSMutableArray *dayArr = [NSMutableArray array];
    for (int i = 0; i < 13; i++) {
        [dayArr addObject:[NSMutableArray arrayWithCapacity:32]];
    }

    //初始化二维数组
    for (int i = 0; i < dayArr.count; i++) {
        for (int j = 0; j < 32; j++) {
            if (i > 0 && j > 0 && j <= [dayOfPerMonth[i==0 ? 0 : i-1] intValue]) {
                [dayArr[i] setObject:@1 atIndexedSubscript:j];
            }
            else {
                [dayArr[i] setObject:@0 atIndexedSubscript:j];
            }
        }
    }

    //得到前四位数的组合
    for (int i = 0; i < dayArr.count; i++) {
        for (int j = 0; j < 32; j++) {
            //第一次筛选
            if ([[dayArr[i] objectAtIndex:j] isEqualToValue:@1]) {
                //月日四位数0101
                NSString *strOfPreFour = [self getPre4StringWithString:[self intToString:i] dayString:[self intToString:j]];
                //判断四位数是否中是否重复或者同时包含0,1,2
                if (![self validateString:strOfPreFour]) {
                    [dayArr[i] setObject:@0 atIndex:j];
                }
            }
            //第二次筛选出结果
            if ([[dayArr[i] objectAtIndex:j] isEqualToValue:@1]) {

                //得到其余六位数
                NSString *strOfPreFour = [self getPre4StringWithString:[self intToString:i] dayString:[self intToString:j]];
                NSMutableArray *arr = [NSMutableArray array];
                for (int h = 0; h < strOfPreFour.length; h++) {
                    [arr addObject:[NSString stringWithFormat:@"%c", [strOfPreFour characterAtIndex:h]]];
                }

                //六位数组
                NSMutableArray *sixArray = [NSMutableArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9",nil];
                [sixArray removeObjectsInArray:arr];

                //六位字符串
                NSString *sixStr = [NSString string];
                for (NSString *str in sixArray) {
                    sixStr = [sixStr stringByAppendingString:str];
                }
                //判断六位数小于6的个数
                int lessSixCount = 0;
                for (int p = 0; p < sixArray.count; p++) {
                    if ([sixArray[p] intValue] < 6) lessSixCount++;
                }
                //判断有没有1,或2
                if (lessSixCount >= 3) {
                    if ([sixStr containsString:@"1"]) {
                        if (lessSixCount == 3) {
                            result += 12;
                            NSLog(@"%@--%@", strOfPreFour, sixStr);
                            //得到六位数字的数组
                            NSMutableArray *sixArray = [NSMutableArray arrayWithCapacity:6];
                            for (int i = 0; i < [sixStr length]; i++) {
                                [sixArray addObject:[NSString stringWithFormat:@"%c",[sixStr characterAtIndex:i]]];
                            }

                        }
                        else  {
                            result +=36;
                            NSLog(@"%@--%@", strOfPreFour, sixStr);
                        }
                    }
                    else if ([sixStr containsString:@"2"]) {
                        if (lessSixCount == 4) {
                            result += 4;
                            NSLog(@"%@--%@", strOfPreFour, sixStr);
                        }
                    }
                }
            }

        }
    }
    NSLog(@"2003年有%i个十全时", result);

}
/**
 *  判断四位数是否中是否重复或者同时包含0,1,2
 *  不满足条件返回NO
 */
- (BOOL)validateString:(NSString *)str {

    if ([str containsString:@"0"] && [str containsString:@"1"] && [str containsString:@"2"]) {
        return NO;
    }
    for (int k = 0; k < str.length; k++) {
        if ([[[str substringToIndex:k] stringByAppendingString:[str substringFromIndex:k+1]] containsString:[NSString stringWithFormat:@"%c", [str characterAtIndex:k]]]) {

            return NO;
        }
    }
    return YES;
}
/**
 *  int类型转换固定字符串,例子:1->01
 */
- (NSString *)intToString:(int)intVal {
    return intVal < 10 ? [NSString stringWithFormat:@"0%i", intVal] : [NSString stringWithFormat:@"%i", intVal];
}
/**
 *  得到月日四位数
 */
- (NSString *)getPre4StringWithString:(NSString *)monthStr dayString:(NSString *)dayStr {
    return [NSString stringWithFormat:@"%@%@", monthStr, dayStr];
}
/**
 *  获得某年某月的天数
 */
- (NSInteger)howManyDaysInThisYear:(NSInteger)year withMonth:(NSInteger)month{
    if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
        return 31 ;

    if((month == 4) || (month == 6) || (month == 9) || (month == 11))
        return 30;

    if((year % 4 == 1) || (year % 4 == 2) || (year % 4 == 3))
        return 28;

    if(year % 400 == 0)
        return 29;

    if(year % 100 == 0)
        return 28;

    return 29;
}

你可能感兴趣的:(编程题)