Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Spring 2015 -- CSC 2700 Section 01

[Isaac's Home Page ]  [Mailing List ]  [Class Page ]  [Normal ]  

week08/i/Time3.java

/*
 *   Author: Son Pham
 *     Date: March 16, 2013
 *  Problem: 518 Time
 * Comments: It took two revisions to get this one right. Hence the Time3.java...
 */
import java.util.Scanner;

public class Time3 {
    
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        while(reader.hasNext()){
            String read = reader.nextLine();
            String[] date = read.split("\\s+");
            Date date1 = new Date(Integer.parseInt(date[0]),
                    Integer.parseInt(date[1]),Integer.parseInt(date[2]),
                    Integer.parseInt(date[3]),Integer.parseInt(date[4]),
                    Integer.parseInt(date[5]));
            read = reader.nextLine();
            date = read.split("\\s+");
            Date date2 = new Date(Integer.parseInt(date[0]),
                    Integer.parseInt(date[1]),Integer.parseInt(date[2]),
                    Integer.parseInt(date[3]),Integer.parseInt(date[4]),
                    Integer.parseInt(date[5]));
            //System.out.println(date1);
            //System.out.println(date2);
            read = reader.nextLine();
            date = read.split("\\s+");
            long numP = Long.parseLong(date[0]);
            String period = date[1];
            //System.out.println(period);
            if(!(period.equals("second")));
                pushDate(date1,period);
            if((period.equals("year")) || (period.equals("month"))){
                
                if(period.equals("year")){
                    numP = (date2.year - date1.year)/numP;
                }
                else{
                    int years = date2.year - date1.year;
                    int months = date2.month - date1.month;
                    if(months < 0){
                        months += 12;
                        years--;
                    }
                    numP = (years*12+months)/numP;
                }
            }
            else
                numP = (getPeriods(date2, period)-getPeriods(date1,period))/numP;
            System.out.println(numP);
            if(reader.hasNext())
                read = reader.nextLine();
        }
    }
    
    public static void pushDate(Date d, String p){
        if(p.equals("year")){
            if((d.second != 0) || (d.minute != 0) || (d.hour != 0) || (d.month != 1) || (d.day != 1)){
                d.year++;
                d.month = 1;
                d.day = 1;
                d.hour = 0;
                d.minute = 0;
                d.second = 0;
            }
        }
        else if(p.equals("month")){
            if((d.second != 0) || (d.minute != 0) || (d.hour != 0) || (d.day != 1)){
                d.month++;
                d.day = 1;
                d.hour = 0;
                d.minute = 0;
                d.second = 0;
                if(d.month > 12){
                    d.year++;
                    d.month = 1;
                }
            }
        }
        else if(p.equals("day")){
            if((d.second != 0) || (d.minute != 0) || (d.hour != 0)){
                d.day++;
                d.hour = 0;
                d.minute = 0;
                d.second = 0;
                if(d.day > getDaysInMonth(d.month,d.year)){
                    d.day = 1;
                    d.month++;
                }
                if(d.month > 12){
                    d.year++;
                    d.month = 1;
                }
            }
        }
        else if(p.equals("hour")){
            if((d.second != 0) || (d.minute != 0)){
                d.hour++;
                d.minute = 0;
                d.second = 0;
                if(d.hour > 23){
                    d.day++;
                    d.hour = 0;
                }
                if(d.day > getDaysInMonth(d.month,d.year)){
                    d.day = 1;
                    d.month++;
                }
                if(d.month > 12){
                    d.year++;
                    d.month = 1;
                }
            }
        }
        else if(p.equals("minute")){
            if((d.second != 0)){
                d.minute++;
                d.second = 0;
                if(d.minute > 59){
                    d.minute = 0;
                    d.hour++;
                }
                if(d.hour > 23){
                    d.day++;
                    d.hour = 0;
                }
                if(d.day > getDaysInMonth(d.month,d.year)){
                    d.day = 1;
                    d.month++;
                }
                if(d.month > 12){
                    d.year++;
                    d.month = 1;
                }
            }
        }
    }
    
    public static boolean isLeap(int year)
    {
       return ((year%400 == 0) ||(year%100 != 0 && year%4 == 0));
    }
    
    public static int getDaysInMonth(int m,int y){
        switch(m){
            case 1:
                return 31;
            case 2:
                if(isLeap(y))
                    return 29;
                else
                    return 28;
            case 3:
                return 31;
            case 4:
                return 30;
            case 5:
                return 31;
            case 6:
                return 30;
            case 7:
                return 31;
            case 8:
                return 31;
            case 9:
                return  30;
            case 10:
                return 31;
            case 11:
                return 30;
            default:
        }
        return 31;
    }
    
    private static long getPeriods(Date d, String p){
        long days = 0;
        int years = d.year;
        while(years >= 1970){
            --years;
            if(isLeap(years)){
                days += 366;
            }
            else{
                days += 365;
            }
        }
        int months = d.month;
        years = d.year;
        while(months > 1){
            --months;
            days += getDaysInMonth(months,years);
        }
        days += (d.day-1);
        if(p.equals("day"))
            return days;
        long hours = days*24+d.hour;
        if(p.equals("hour"))
            return hours;
        long minutes = hours*60+d.minute;
        if(p.equals("minute"))
            return minutes;
        long seconds = minutes*60+d.second;
        return seconds;    
    }
    
    static class Date{
        
        int year,month,day,hour,minute,second;
        
        public Date(int y, int m, int d, int h, int min, int sec){
            year = y;
            month = m;
            day = d;
            hour = h;
            minute = min;
            second = sec;
        }
        public String toString(){
            return (year+" "+month+" "+day+" "+hour+" "+minute+" "+second);
        }
        
    }
    
}

The statements and opinions included in these pages are those of only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015