题目1096:日期差值
来源:互联网 发布:淘宝商城怎么开 编辑:程序博客网 时间:2024/05/17 11:08
题目描述:
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天
输入:
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
输出:
每组数据输出一行,即日期差值
样例输入:
20110412
20110422
样例输出:
11
Hash基本思想解题,学习一下~
import java.util.Scanner;class Date{ private String input; private int days; private int month; private int year; public Date(){ } public Date( int year, int month, int day) { this.days = days; this.month = month; this.year = year; } public int[][] daysOfMonth = {{0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31}}; public String getInput() { return input; } public void setInput(String input) { this.input = input; } public int getDays() { return days; } public void setDays(int days) { this.days = days; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int isYeap(){ return (year%100 == 0 && year%400 ==0)||(year%100 != 0 && year%4 == 0)? 1:0; } public void calcutionDate(){ String str_year = input.substring(0, 4); String str_month = input.substring(4,6); String str_day = input.substring(6, 8); year = Integer.parseInt(str_year); month = Integer.parseInt(str_month); days = Integer.parseInt(str_day); } public void nextDay(){ days++; if(days>daysOfMonth[isYeap()][month]){ month++; days=1; if(month>12){ month = 1; year++; } } }}public class Main { static int days[][][] = new int[5001][13][32]; public static void main(String args[]){ Scanner sc = new Scanner(System.in); Date d1 = new Date(); Date d2 = new Date(); int temp_year = 0; int temp_month = 1; int temp_day = 1; int sum = 1; Date temp_date = new Date(temp_year,temp_month,temp_day); while(temp_date.getYear()<5001){ try{ //System.out.println(temp_date.getYear()+" "+temp_date.getMonth()+" "+temp_date.getDays()); days[temp_date.getYear()][temp_date.getMonth()][temp_date.getDays()] = sum++; }catch(ArrayIndexOutOfBoundsException e){ //System.out.println("ArrayIndexOutOfBoundsException"); } temp_date.nextDay(); } while(sc.hasNext()){ String year1 = sc.next(); //System.out.println("==??"); String year2 = sc.next(); d1.setInput(year1); d2.setInput(year2); d1.calcutionDate(); d2.calcutionDate(); int diff = days[d1.getYear()][d1.getMonth()][d1.getDays()] - days[d2.getYear()][d2.getMonth()][d2.getDays()]; System.out.println(diff>0?diff+1:-diff+1); } sc.close(); }}/************************************************************** Problem: 1096 User: zzuljs Language: Java Result: Accepted Time:170 ms Memory:28428 kb****************************************************************/
0 0
- 题目1096:日期差值
- 题目1096:日期差值
- 题目1096:日期差值
- 题目1096:日期差值
- 题目1096:日期差值
- 题目1096:日期差值
- 6-题目1096:日期差值
- 九度题目1096:日期差值
- [九度]题目1096:日期差值
- 九度题目1096:日期差值
- 九度OJ 题目1096:日期差值
- 题目1096:日期差值 Java/C++
- 九度-题目1096:日期差值
- 【九度】题目1096:日期差值
- 【九度】题目1096:日期差值
- 九度 oj 题目1096:日期差值
- 九度 OJ 题目1096:日期差值
- 题目6:日期差值
- Android之调用高德地图或百度地图原生app
- Maven的使用
- 初识curses (简单常用的函数简介)
- Java学习日记8:文件上传工具类的实现和服务器给客户端发送图片
- iOS源码解析—AFNetworking(RequestSerializer)
- 题目1096:日期差值
- 1445: Pku3245 Sequence Partitioning
- 《Python核心编程》(第二版) 第3章 Python基础
- 数据库管理员用户角色组权限设计
- 一个循环实现新数组n位值是老数组前n位的和
- C++ 11 匿名函数Lambda表达式
- fail-fast(快速失败/报错机制)-ConcurrentModificationException
- Ubuntu14.04 下载&&编译 Android 5.1.1源码(采用国内清华大学镜像)
- 设计模式学习--桥接模式