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

 import java.util.Scanner;

 public class Solution2 {
static int DateOfWeekday(int date, int weekday) {
int cur = date%7;
int res = 0;
int dif = 0;
if (weekday > 0) {
dif = 7*((weekday-1)/7) + (weekday%7-cur>0? weekday%7-cur : 7-(cur-weekday%7));
res = date + dif;
}
else if (weekday < 0){
weekday = Math.abs(weekday);
dif = 7*((weekday-1)/7) + (cur-weekday%7>0? cur-weekday%7 : 7-(weekday%7-cur));
res = date - dif;
}
else res = date;
return res;
} public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int res;
int _date;
_date = Integer.parseInt(in.nextLine()); int _weekday;
_weekday = Integer.parseInt(in.nextLine()); res = DateOfWeekday(_date, _weekday);
System.out.println(res);
} }
     static int DateOfWeekday(int date, int weekday) {
int cur = date % 7;
int res;
if (weekday == 0) return date;
else if (weekday > 0) {
if (weekday % 7 > cur) res = date + weekday % 7 - cur;
else res = date + 7 - (cur - weekday % 7);
res = res + 7*((weekday-1)/7);
}
else {
if (Math.abs(weekday % 7)<cur) res= date - cur - weekday % 7;
else res = date - 7 - cur - weekday%7;
res = res - 7*((Math.abs(weekday)-1)/7);
}
return res;
}

VMware coding Challenge:Date of Weekday的更多相关文章

  1. VMware Coding Challenge: Possible Scores && Summary: static

    Combination Sum I 那道题的变体 /* * Complete the function below. */ static int is_score_possible(int score ...

  2. VMware coding Challenge

    思路:这道题要观察,举个例子,1 2 * * 3 * 4  5 * * 6 7 * 8 * *, 用Stack,先序遍历,遇到数字就入栈,如果遇到 * *,说明栈顶节点是叶子节点,一条根到叶子的路径这 ...

  3. VMware Coding Challenge: The Heist

    类似BackpackII问题 static int maximize_loot(int[] gold, int[] silver) { int[][] res = new int[gold.lengt ...

  4. VMware Coding Challenge: Removing Duplicates Entries

    static LinkedListNode removeDuplicates(LinkedListNode list) { LinkedListNode cur = list; HashSet< ...

  5. VMware coding Challenge: Coin Toss Betting

    static int CoinTossEndAmount(int betAmount, String coinTossResults) { if (betAmount <=0 || coinTo ...

  6. Linux命令学习总结:date命令

    命令简介: date 根据给定格式显示日期或设置系统日期时间.print or set the system date and time 指令所在路径:/bin/date 命令语法: date [OP ...

  7. Linux命令学习总结:date命令【转】

    本文转自:http://www.cnblogs.com/kerrycode/p/3427617.html 命令简介: date 根据给定格式显示日期或设置系统日期时间.print or set the ...

  8. 每天一个linux命令(37):date命令

    在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...

  9. 原生JS:Date对象详细参考

    Date对象:基于1970年1月1日(世界标准时间)起的毫秒数 本文参考MDN做的详细整理,方便大家参考MDN 构造函数: new Date(); 依据系统设置的当前时间来创建一个Date对象. ne ...

随机推荐

  1. 2015.7.10js-07(简单时间)

    今天学习了一个小程序,将本地时间显示在页面上,用了图片的形式. 1.执行原理是,先用6张全0的图片,然后通过循环img各自根据时间来更换相对应的时间图片. 2.使用Date()函数获取本地时间,然后转 ...

  2. Nexus网页直接上传jar包

    登陆已经安装好的nexus私有仓库,如图:   点击左边菜单“Repositories”,选择右边列表“3rd party“   点击“3rd party”,选择artifact Upload,如下图 ...

  3. DragonBones龙骨换装(局部换装+全局换装)

    参考: Egret官方换装动画 Egret换装三种方式 CSDN(全局换装) egret使用DragonBones实现简单的换装 换装,主要是替换任意插槽的图片,来达到局部换装的目的. 游戏中可以只制 ...

  4. 使用curl进行s3服务操作

    最近使用curl对s3进行接口测试,本想写个总结文档,但没想到已有前辈写了,就直接搬过来做个记录吧,原文见: http://blog.csdn.net/ganggexiongqi/article/de ...

  5. JAVA unicode转换成中文

    /** * * unicode 转换成 中文 * @param theString * @return */ public static String decodeUnicode(String the ...

  6. R因子

    factor(x = character(), levels, labels = levels, exclude = NA, ordered = is.ordered(x), nmax = NA) l ...

  7. UI自动化--PageObjects(页面对象)

    核心的核心:减少了重复代码的数量,减少变更涉及面:做到如果UI发生更改,则只需在一个位置应用此修复程序. PageObject:将页面作为一个对象,进行封装,包括元素定位,封装获取各元素.操作的方法: ...

  8. opengl学习笔记(三):经过纹理贴图的棋盘

    opengl纹理贴图的步骤: 1:创建纹理对象,并为它指定一个纹理 2:确定纹理如何应用到每个像素上 3:启用纹理贴图功能 4:绘制场景,提供纹理坐标和几何图形坐标 注意:纹理坐标必须在RGBA模式下 ...

  9. hadoop(角色)各个组件配置信息

    1)namenode:        core-site.xml文件中决定:            <property>                <name>fs.def ...

  10. 排序的本质是什么 冒泡排序 bubble sort

    import random def getRandomNums(min=4, max=20): n = random.randint(min, max) arr = [random.randint(1 ...