VMware coding Challenge:Date of Weekday

这道题再次证明了这种细节的题目,画个图容易搞清楚
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的更多相关文章
- VMware Coding Challenge: Possible Scores && Summary: static
Combination Sum I 那道题的变体 /* * Complete the function below. */ static int is_score_possible(int score ...
- VMware coding Challenge
思路:这道题要观察,举个例子,1 2 * * 3 * 4 5 * * 6 7 * 8 * *, 用Stack,先序遍历,遇到数字就入栈,如果遇到 * *,说明栈顶节点是叶子节点,一条根到叶子的路径这 ...
- VMware Coding Challenge: The Heist
类似BackpackII问题 static int maximize_loot(int[] gold, int[] silver) { int[][] res = new int[gold.lengt ...
- VMware Coding Challenge: Removing Duplicates Entries
static LinkedListNode removeDuplicates(LinkedListNode list) { LinkedListNode cur = list; HashSet< ...
- VMware coding Challenge: Coin Toss Betting
static int CoinTossEndAmount(int betAmount, String coinTossResults) { if (betAmount <=0 || coinTo ...
- Linux命令学习总结:date命令
命令简介: date 根据给定格式显示日期或设置系统日期时间.print or set the system date and time 指令所在路径:/bin/date 命令语法: date [OP ...
- Linux命令学习总结:date命令【转】
本文转自:http://www.cnblogs.com/kerrycode/p/3427617.html 命令简介: date 根据给定格式显示日期或设置系统日期时间.print or set the ...
- 每天一个linux命令(37):date命令
在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...
- 原生JS:Date对象详细参考
Date对象:基于1970年1月1日(世界标准时间)起的毫秒数 本文参考MDN做的详细整理,方便大家参考MDN 构造函数: new Date(); 依据系统设置的当前时间来创建一个Date对象. ne ...
随机推荐
- dos 下如何查看环境变量
使用命令:echo %path%
- makefile高级应用
https://www.zybuluo.com/lishuhuakai/note/206938 make是Linux下的一款程序自动维护工具,配合makefile的使用,就能够根据程序中模块的修改情况 ...
- Elasticsearch学习之深入聚合分析三---案例实战
1. 统计指定品牌下每个颜色的销量 任何的聚合,都必须在搜索出来的结果数据中进行,搜索结果,就是聚合分析操作的scope GET /tvs/sales/_search { , "query& ...
- sencha touch 常见问题解答(1-25)
欢迎留言补充,持续更新中... 1.sencha touch 是什么? 答:Sencha touch框架是世界上第一个基于HTML 5的移动应用框架.它可以让你的Web应用看起来像网络应用.美丽的用户 ...
- docker参数--restart=always的作用
创建容器时没有添加参数 --restart=always ,导致的后果是:当 Docker 重启时,容器未能自动启动. 现在要添加该参数怎么办呢,方法有二: 1.Docker 命令修改 docker ...
- redmine创建新闻,自动发邮件给项目组所有成员
redmine创建新闻,自动发邮件给项目组所有成员: 1.添加用户至公共项目内 2.配置系统邮件推送配置 3.检查用户接受推送配置 3.使用管理员账户发布新闻(不能自己发送自己) 4.查看邮件接受邮件
- 【CF827E】Rusty String 调和级数+FFT
[CF827E]Rusty String 题意:给你一个01串,其中部分字符是'?',?可以是0或1,求所有可能的d,满足存在一种可能得到的01串,在向右移动d格后与自己相同. $n\le 5\tim ...
- windows下gcc的安装
首先打开 www.mingw.org . www.mingw.org 直接点击右上方的 Download Installer 即可下载. 点击 Download Installer 进入下载页 ...
- HDU 4734 - F(x) - [数位DP][memset优化]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 Time Limit: 1000/500 MS (Java/Others) Memory Lim ...
- Python:字符串处理函数
split() / join() 拆分和组合 #split() 通过指定分隔符对字符串进行切片(拆分),默认空格符 lan = "python ruby c c++ swift" ...