zoukankan      html  css  js  c++  java
  • VMware coding Challenge:Date of Weekday

    这道题再次证明了这种细节的题目,画个图容易搞清楚

     1 import java.util.Scanner;
     2 
     3 
     4 public class Solution2 {
     5     static int DateOfWeekday(int date, int weekday) {
     6         int cur = date%7;
     7         int res = 0;
     8         int dif = 0;
     9         if (weekday > 0) {
    10             dif = 7*((weekday-1)/7) + (weekday%7-cur>0? weekday%7-cur : 7-(cur-weekday%7));
    11             res = date + dif;
    12         }
    13         else if (weekday < 0){
    14             weekday = Math.abs(weekday);
    15             dif = 7*((weekday-1)/7) + (cur-weekday%7>0? cur-weekday%7 : 7-(weekday%7-cur));
    16             res = date - dif;
    17         }
    18         else res = date;
    19         return res;
    20     }
    21     
    22     public static void main(String[] args) {
    23         Scanner in = new Scanner(System.in);
    24         int res;
    25         int _date;
    26         _date = Integer.parseInt(in.nextLine());
    27         
    28         int _weekday;
    29         _weekday = Integer.parseInt(in.nextLine());
    30         
    31         res = DateOfWeekday(_date, _weekday);
    32         System.out.println(res);
    33     }
    34 
    35 }
     1     static int DateOfWeekday(int date, int weekday) {
     2         int cur = date % 7;
     3         int res;
     4         if (weekday == 0) return date;
     5         else if (weekday > 0) { 
     6             if (weekday % 7 > cur) res = date + weekday % 7 - cur;
     7             else res = date + 7 - (cur - weekday % 7);
     8             res = res + 7*((weekday-1)/7);
     9         }
    10         else {
    11             if (Math.abs(weekday % 7)<cur) res= date - cur - weekday % 7;
    12             else res = date - 7 - cur - weekday%7;
    13             res = res - 7*((Math.abs(weekday)-1)/7);
    14         }
    15         return res;
    16     }
  • 相关阅读:
    BZOJ_3133_[Baltic2013]ballmachine_堆+倍增
    机器学习—朴素贝叶斯及其优化
    机器学习-输出一颗树
    机器学习-决策树
    KNN-综合应用
    KNN-机器学习算法
    [转载]Jupyter notebook调试
    机器学习-归一化
    神经网络模型及反向传播代码完全解析
    [转载]神经网络偏置项(bias)的设置及作用
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4303194.html
Copyright © 2011-2022 走看看