【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 ...
随机推荐
- JS方法代理
作者:Jiang, Jilin JS作为一门脚本语言.十分easy上手.外加其灵活性,能够轻而易举地扩展功能.今天,我们就聊聊JS的方法代理. 方法代理是脚本语言中常见的方法扩展形式.这样的灵活的形式 ...
- 给linux操作系统安装中文环境
如果设置的默认环境是英文,需要安装中文环境.最简答的方法如下: sudo apt-get install language-pack-zh-hant sudo apt-get install lang ...
- c++通过类名动态创建对象
转载:http://www.seacha.com/article.php/knowledge/cbase/2013/0615/2154.html 主要思想:在每次创建类的过程中,通过各自类的辅助类(所 ...
- iOS 集成阿里百川最新版(3.1.1.96) 实现淘宝授权登录以及调用淘宝客户端商品详情页
公司最近要做第三方登录,由于是做导购项目,必不可少的有淘宝的授权登录.本来就是一个授权登录,没什么大不了的.但淘宝的无线开放业务——阿里百川更新的最新版本3.1.1.96,开发文档不是不详细,是很 ...
- js 触摸事件 touch
//ban 为某div let startX = 0; ban.addEventListener("touchstart",function(){ //获取初始点击位置 start ...
- android Service 保持不被杀死
Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStart ...
- Nginx nginx.conf配置文件详细说明
在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户user www-data; #启动进程,通常设置成和cpu的数量相等worker_process ...
- Python之可迭代对象、迭代器、生成器
在使用Python的过程中,很容易混淆如下几个关联的概念: 1.容器(container) 2.可迭代对象(Iterable) 3.迭代器(Iterator) 4.生成器(generator) 5.生 ...
- Linux中的awk命令
awk '条件1{动作1} 条件2{动作2} ...' 文件名 条件: BEGIN 在处理文件里的第一行数据之前执行 END 在处理完文件里的最后一行数据 ...
- go语言之并发编程同步一
前面介绍了采用go语法的并行操作以及channel.既然是并行操作,那么就涉及到数据原子性以及同步的问题.所以在Go里面也需要采用同步的机制. 互斥锁: 由标准库代码包sync中的Mutex结构体类型 ...