首页 > 代码库 > HDU_ACM_2005

HDU_ACM_2005

第几天?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 147067    Accepted Submission(s): 52729


Problem Description
给定一个日期,输出这个日期是该年的第几天。
 

 

Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
 

 

Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
 

 

Sample Input
1985/1/20 2006/3/12
 

 

Sample Output
20 71
 

 

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

int isLeapYear(int year) {
    if(year % 4 == 0 && year % 100 != 0 || year % 400 ==0) {
        return 29;
    } else {
        return 28;
    }
}

int monthToDay(int month,int year) {
    int day;
    switch(month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12: day = 31;break;
        case 4:
        case 6:
        case 9:
        case 11: day = 30;break;
        case 2: day = isLeapYear(year);
    }
    return day;
}

int main() {
    string date;
    while(cin>>date) {
        int year;
        int month;
        int day;
        int days = 0;
        int first = date.find(/, 0);
        int second = date.find(/, 5);
        int length = date.length();
        year = atoi(date.substr(0, first).c_str());
        month = atoi(date.substr(first + 1, second - first).c_str());
        day = atoi(date.substr(second + 1, length - second).c_str());
        for(int i = 1; i < month; i++) {
            days += monthToDay(i, year);
        }
        days += day;
        cout<<days<<endl;
    }
    return 0;
}

JAVA撸多了,思路都不一样。。

HDU_ACM_2005