• 算法提高 日期计算


    问题描述
      已知2011年11月11日是星期五,问YYYY年MM月DD日是星期几?注意考虑闰年的情况。尤其是逢百年不闰,逢400年闰的情况。
    输入格式
      输入只有一行
      YYYY MM DD
    输出格式
      输出只有一行
      W
    数据规模和约定
      1599 <= YYYY <= 2999
      1 <= MM <= 12
      1 <= DD <= 31,且确保测试样例中YYYY年MM月DD日是一个合理日期
      1 <= W <= 7,分别代表周一到周日
    样例输入
    2011 11 11
    样例输出
    5
    求法:算出给出的日期距2011年11月11日有多少天,在计算星期几
     1 import java.util.Arrays;
     2 import java.util.Scanner;
     3 
     4 public class Main{
     5     static int y;
     6     static int m;
     7     static int d;
     8     public static void main(String[] args) {
     9         Scanner input = new Scanner(System.in);
    10         y = input.nextInt();
    11         m = input.nextInt();
    12         d = input.nextInt();
    13         int a = 2011;
    14         int b = 11;
    15         int c = 11;
    16         int sum = 0;
    17         int temp1 = y*10000+m*100+d;
    18         int temp2 = a*10000+b*100+c;
    19         if(temp1<temp2){
    20             int temp;
    21             temp = y;
    22             y = a;
    23             a = temp;
    24             temp = m;
    25             m = b;
    26             b = temp;
    27             temp = d;
    28             d = c;
    29             c = temp;
    30         }
    31         while(a!=y||b!=m||c!=d){
    32             sum++;
    33             if(yue(b)==1){
    34                 if(c<31) c++;
    35                 else{
    36                     c = 1;
    37                     if(b==12){
    38                         b = 1;
    39                         a++;
    40                     }else{
    41                         b++;
    42                     }
    43                 }
    44             }else if(yue(b)==3){
    45                 if(c<30) c++;
    46                 else{
    47                     b++;
    48                     c = 1;
    49                 }
    50             }else{
    51                 if(runian(a)){
    52                     if(c<29) c++;
    53                     else{
    54                         b++;
    55                         c = 1;
    56                     }
    57                 }else{
    58                     if(c<28) c++;
    59                     else{
    60                         b++;
    61                         c++;
    62                     }
    63                 }
    64             }
    65         }
    66         sum = sum%7;
    67         if(temp1>temp2){
    68             sum = sum+5;
    69             sum = sum%7;
    70         }else{
    71             sum = 5-sum;
    72             if(sum<0) sum = sum+7;
    73         }
    74         if(sum==0)
    75             System.out.println(7);
    76         else
    77             System.out.println(sum);
    78     }
    79     public static boolean runian(int a){
    80         if(a%4==0&&a%100!=0) return true;
    81         if(a%400==0) return true;
    82         return false;
    83     }
    84     public static int yue(int b){
    85         if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
    86             return 1;
    87         else if(b==2) return 2;
    88         else return 3;
    89     }
    90     
    91 
    92 }
  • 相关阅读:
    spring boot-17.RabbitMQ
    spring boot-16.使用redis做缓存
    spring boot-15.缓存
    spring boot-14.集成MyBatis
    spring boot-13.数据访问
    docker 安装完mysql 后客户端无法访问
    【python】string functions
    【转】澄清P问题、NP问题、NPC问题
    ubuntu中使用gensim+word2vec[备忘]
    ubuntu熟悉过程中遇到一些小问题记录一下
  • 原文地址:https://www.cnblogs.com/lolybj/p/6649354.html
Copyright © 2020-2023  润新知