Change language :

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的更多相关文章

  1. 【HackerRank】Gem Stones

    Gem Stones John has discovered various rocks. Each rock is composed of various elements, and each el ...

  2. 【HackerRank】How Many Substrings?

    https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...

  3. 【Leetcode】Jewels and Stones

    Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...

  4. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

  5. 【LeetCode】Jewels and Stones(宝石与石头)

    这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...

  6. 【Leetcode_easy】1033. Moving Stones Until Consecutive

    problem 1033. Moving Stones Until Consecutive 参考 1. Leetcode_easy_1033. Moving Stones Until Consecut ...

  7. 【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 ...

  8. 【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 ...

  9. 【LeetCode】1033. Moving Stones Until Consecutive 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 脑筋急转弯 日期 题目地址:https://leet ...

随机推荐

  1. LeetCode543. Diameter of Binary Tree

    Description Given a binary tree, you need to compute the length of the diameter of the tree. The dia ...

  2. Play on Words UVA - 10129 欧拉路径

    关于欧拉回路和欧拉路径 定义:欧拉回路:每条边恰好只走一次,并能回到出发点的路径欧拉路径:经过每一条边一次,但是不要求回到起始点 ①首先看欧拉回路存在性的判定: 一.无向图每个顶点的度数都是偶数,则存 ...

  3. DJI SDK iOS 开发之二:搭建主要的开发环境

    本文想介绍搭建主要的DJI SDK iOS下的开发环境,只是DJI官方已经给出了非常具体的执行其demo的教程.网址例如以下: https://dev.dji.com/cn/guide 我这里总结一下 ...

  4. 巧用批处理cmd快速切换IP地址

    如果你的笔记本经常在不同的地方使用,有些地方需要自动获取IP,而有些地方需要配置固定IP,每换一个地方都需要重新配置一遍,是不是感觉很麻烦呢? 下面介绍一种通过建立批处理文件来快速切换IP的方法: s ...

  5. 如何创建Windows定时任务

    我们经常使用电脑,有没有那么一个瞬间想着要是电脑可以每隔一段时间,自动处理一件事情就好了呢? 其实Windows还真有这样的功能,很多软件检测更新就是通过这个方法实现的. 这次我们来做一个简易的喝水提 ...

  6. thinkphp 视频教程

    http://edu.51cto.com/lesson/id-28238.html thinkphp

  7. python django -7 Git与项目

    git的使用,主要包括: 本地仓库的命令 远程仓库的命令 项目需求.页面.模型类的设计,及页面的使用 Git简介 Git是目前世界上最先进的分布式版本控制系统 安装 sudo apt-get inst ...

  8. 整理的一些java中常使用jar包以及说明

    slf4j:Simple Logging Facade for Java SLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于 ...

  9. resize和reserve

    resize改变的实际的大小,reserve是容量即capacity 如果先指定capacity的大小,可以防止内存的重新分配,我感觉在分配实际的内存的时候会餐口capacity的大小,如果事先指定容 ...

  10. 路径 php中'.'和'..'还有'./'和'../'

    ./当前目录(就是当前执行文件所在目录) ../上级目录 / 这个才是根目文件名/ 同级目录 例子如图 1.cart下的index.php 1)要引用Public->css->index. ...