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

 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. Android 框架

    1. https://github.com/wyouflf/xUtils xUtils简介 xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal框架,进行了大量重构, ...

  2. UINavigationItem 设置UIBarButtonItem

    转:http://hi.baidu.com/ivan_xu/item/237bb1ad77eff9b028ce9d7c 有A.B两个ViewController,假如A push B: UINavig ...

  3. win7 开机自启动控制

    直接用win+r运行 --- 输入 msconfig 去除“OneNote”开机自启动方法:取消勾选,点击 “应用” ,然后点击“确定” 即可

  4. java(9)并发编程

    整理自<java 并发编程的艺术> 1. 上下文切换 即使是单核处理器也支持多线程执行代码,CPU通过给每个线程分配CPU时间片来实现这个机制.时间片是CPU分配给各个线程的时间,因为时间 ...

  5. [Vue warn]: Error in render: "SyntaxError: Unexpected token ' in JSON at position 1"

    一,场景: 字符串转对象: var str = "{'bankRate':5,'YINGUO':0}" 二,操作: JSON.parse(str)时候,报错 [Vue warn]: ...

  6. parted分区脚本

    #!/bin/bash #Used to fomat 6 disks PATH=/bin:/sbin:/usr/bin:/usr/sbin export PATH disk_to_parted=&qu ...

  7. C#中XML的读取

    本文主要介绍在C#中有关XML的读取,写入操作. 1.XML的内容如下: <?xml version="1.0" encoding="utf-8" ?&g ...

  8. codeforces 782B - The Meeting Place Cannot Be Changed

    time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. SQL Fundamentals || Single-Row Functions || 通用函数 General function || (NVL,NVL2,NULLIF,DECODE,CASE,COALESCE)

    SQL Fundamentals || Oracle SQL语言 SQL Fundamentals: Using Single-Row Functions to Customize Output使用单 ...

  10. 详解Oracle多种表连接方式

    1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表都不加限制) 3. 自连接(同一张表内的连接) SQL的标准语 ...