【HackerRank】Manasa and Stones
Manasa 和 她的朋友出去徒步旅行。她发现一条小河里边顺序排列着带有数值的石头。她开始沿河而走,发现相邻两个石头上的数值增加 a 或者 b. 这条小河的尽头有一个宝藏,如果Manasa能够猜出来最后一颗石头上的数值,那么宝藏就是她的。假设第一个石头的上数值为0,找出最后一个石头的可能的所有数值。
输入格式
第一行包含整数 T, 代表测试数据的组数。
每组数组包含三行:
第一行包含 n,代表石头的个数
第二行包含 a
第三行包含 b
输出格式
升序输出最后一颗石头上所有可能的数值, 用空格隔开。
取值范围
1 ≤ T ≤ 10
1 ≤ n, a, b ≤ 103
题解:
对于第二个石头,可能的取值是0*a+b或者a+0*b;
对于第三个石头,可能的取值是0*a+2*b,1*a+1*b,2*a+0*b;
.....
对于第n个石头,可能的取值是0*a+(n-1)*b,1*a+(n-2)*b,......,(n-1)*a+0*b;
所以只要枚举a和b的系数就可以算出所有的可能了。
另外,java中的hashset是无需的,treeset是有序的。
代码如下:
import java.io.*;
import java.util.*; public class Solution { public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i = 0; i < t; i++){
Set<Long> answer= ManasaandStones(in.nextLong(), in.nextLong(), in.nextLong());
Iterator<Long> iterator = answer.iterator();
while(iterator.hasNext()){
System.out.printf("%d ",iterator.next());
}
System.out.println();
}
} private static Set<Long> ManasaandStones(long n, long a, long b){ //Write code to solve each of the test over here
if(a > b){
long temp = b;
b = a;
a = temp;
}
Set<Long> hs = new TreeSet<Long>();
for(int i = 0;i <= n-1;i++){
hs.add(i*b+(n-1-i)*a);
}
return hs;
} }
【HackerRank】Manasa and Stones的更多相关文章
- 【HackerRank】Gem Stones
Gem Stones John has discovered various rocks. Each rock is composed of various elements, and each el ...
- 【HackerRank】How Many Substrings?
https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...
- 【Leetcode】Jewels and Stones
Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【LeetCode】Jewels and Stones(宝石与石头)
这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...
- 【Leetcode_easy】1033. Moving Stones Until Consecutive
problem 1033. Moving Stones Until Consecutive 参考 1. Leetcode_easy_1033. Moving Stones Until Consecut ...
- 【leetcode】1033. Moving Stones Until Consecutive
题目如下: Three stones are on a number line at positions a, b, and c. Each turn, you pick up a stone at ...
- 【leetcode】947. Most Stones Removed with Same Row or Column
题目如下: On a 2D plane, we place stones at some integer coordinate points. Each coordinate point may h ...
- 【LeetCode】1033. Moving Stones Until Consecutive 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 脑筋急转弯 日期 题目地址:https://leet ...
随机推荐
- ptmalloc、tcmalloc和jemalloc
内存优化总结:ptmalloc.tcmalloc和jemalloc 转载 2017年09月05日 18:57:12 3674 转载于:http://www.cnhalo.net/2016/06/13/ ...
- Oracle----Oracle 11g XE release2安装与指导
今天上午我安装了Oracle 11g企业版,发现太占内存了,考虑到MS SQL有express版本,所以寻思着尝试尝试Oracle 11g的express版本,就是EX版本.下面是具体的安装步骤. 1 ...
- 离散数学及其应用(Discrete Mathematica With Application 7th)学习笔记 第一章
目前本人只进行到了第五章的章末补充练习,应该是从4月6号开始学习的,又是英文版,而且基本就下班回家抽2个小时左右去学,所以进度较慢. 由于本质是数学,除了一些程序处理和大计算量的问题,基本上一本草稿本 ...
- python 爬虫3 异常处理
1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本机无法上网 连接不到特定的服务器 服务器不存在 在代码中,我们需要用try-except语句来包围并捕获相应的异常.下 ...
- git Staging Deleted files
Use git rm foo to stage the file for deletion. (This will also delete the file from the file system, ...
- iOS --转载2018苹果$299美元企业级开发者账号申请攻略
前言篇 现在苹果企业级开发者账号申请十分严格,大部分企业都无法申请下来,本人尝试过多次申请,现将一些审核技巧分享出来,希望能帮到你们通过申请,需要帮助请看本文最后 2018年6月7号申请成功案例 ...
- WINDOWS 7.1 SDK 安装失败
错误提示: Please refer to Samples\Setup\HTML\ConfigDetails.htm document for further information. 原因:本机上安 ...
- 【BZOJ3124】[Sdoi2013]直径 树形DP(不用结论)
[BZOJ3124][Sdoi2013]直径 Description 小Q最近学习了一些图论知识.根据课本,有如下定义.树:无回路且连通的无向图,每条边都有正整数的权值来表示其长度.如果一棵树有N个节 ...
- VLC 媒体播放器
VLC 媒体播放器 VLC 媒体播放器是一个便携式. 免费.开源. 跨平台的媒体播放器. VideoLAN 项目的流式媒体服务器.分为Windows Phone版本和Android版本. 下载地址: ...
- java 对list 排序
Comparable用Collections.sort方法对list排序有两种方法第一种是list中的对象实现Comparable接口,如下: /*** 根据order对User排序*/public ...