1. 程式人生 > >oc和swift實現演算法:一個人a年b月c日出生,a,b,c三數的乘積為428575,這個人是什麼時候出生的?

oc和swift實現演算法:一個人a年b月c日出生,a,b,c三數的乘積為428575,這個人是什麼時候出生的?

題目:

一個人a年b月c日出生,a,b,c三數的乘積為428575,這個人是什麼時候出生的?

用Object-C實現:

//一個人a年b月c日出生,a,b,c三數的乘積為428575,這個人是什麼時候出生的?
- (void)p_caclueYearMonthDay {
    //獲取當前年月日
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY"];
    NSInteger currentYear = [[formatter stringFromDate:date] integerValue];
    [formatter setDateFormat:@"MM"];
    NSInteger currentMonth= [[formatter stringFromDate:date] integerValue];
    [formatter setDateFormat:@"DD"];
    NSInteger currentDay  = [[formatter stringFromDate:date] integerValue];
    
    ///每月基本天數
    NSMutableArray *dayArray= [NSMutableArray arrayWithArray:@[@31, @28, @31, @30, @31, @30, @31, @31, @30, @31, @30, @31]];
    ///遍歷年
    for (NSInteger year = 0; year <= currentYear; ++year) {
        ///是否閏年,修改2月天數
        if (year % 4 == 0) {
            [dayArray setObject:@29 atIndexedSubscript:1];
        } else {
            [dayArray setObject:@28 atIndexedSubscript:1];
        }
        //根據是否是當前年判斷有沒有12個月
        NSInteger maxMonth = ((year == currentYear)?currentMonth:12);
        ///遍歷月
        for (NSInteger month = 1; month <= maxMonth; ++month) {
            //根據是否是當前年月判斷這個月有多少天
            NSInteger maxDay = ((year == currentYear && month == currentMonth)?currentDay:[dayArray[month-1] integerValue]);
            ///遍歷日
            for (NSInteger day = 1; day <= maxDay; ++day) {
                if (428575 == (year * month * day)) {
                    NSLog(@"這個人出生在%ld年%ld月%ld日", (long)year, (long)month, (long)day);
                    //break; //不能用break;的原因是因為可能會存在多個結果
                }
            }
        }
    }
}

用swift實現:

//一個人a年b月c日出生,a,b,c三數的乘積為428575,這個人是什麼時候出生的?
    func calueYearMonthDay() {
        //獲取當前年月日
        let currentdate = NSDate()
        let formatter   = DateFormatter()
        formatter.dateFormat    = "YYYY"
        let currentYear  : Int = Int(formatter.string(from: currentdate as Date))!
        formatter.dateFormat    = "MM"
        let currentMonth : Int = Int(formatter.string(from: currentdate as Date))!
        formatter.dateFormat    = "DD"
        let currentDay   : Int = Int(formatter.string(from: currentdate as Date))!
        
        //每月基本天數
        let dayArray = NSMutableArray.init(array: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31])
        //遍歷年
        for year in 0...currentYear {
            //判斷是否閏年
            dayArray[1] = ( year % 4 == 0 ) ? 29 : 28
            ///根據是否是當前年判斷有沒有12個月
            let maxMonth : Int = (year == currentYear ? currentMonth : 12)
            //遍歷月
            for month in 1...maxMonth {
                //根據是否是當前年月判斷這個月有多少天
                let maxDay = ((year == currentYear && month == currentMonth) ? currentDay : ((dayArray[month-1]) as! Int))
                //遍歷日
                for day in 1...maxDay {
                    if (428575 == year * month * day) {
                        print("這個人出生在\(year)年\(month)月\(day)日")
                    }
                }
            }
        }
    }