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. linux服务器宕机分析/性能瓶颈分析

    linux服务器宕机分析/性能瓶颈分析   服务器宕机原因很多,资源不足.应用.硬件.系统内核bug等,以下一个小例子 服务器宕机了,首先得知道服务器宕机的时间点,然后分析日志查找原因 1.last ...

  2. 获取jsapi_ticket

    String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&a ...

  3. python cookies 爬虫处理

    Cookie Cookie 是指某些网站服务器为了辨别用户身份和进行Session跟踪,而储存在用户浏览器上的文本文件,Cookie可以保持登录信息到用户下次与服务器的会话. Cookie原理 HTT ...

  4. 使用sqoop1.4.4从oracle导入数据到hive中错误记录及解决方案

    在使用命令导数据过程中,出现如下错误 sqoop import --hive-import --connect jdbc:oracle:thin:@192.168.29.16:1521/testdb ...

  5. 【翻译自mos文章】当点击完 finishbutton后,dbca 或者dbua hang住

    当点击完 finishbutton后,dbca 或者dbua hang住 来源于: DBCA/DBUA APPEARS TO HANG AFTER CLICKING FINISH BUTTON (文档 ...

  6. Git安装部署

    1.1 最新git源码下载地址 https://github.com/git/git/releases https://www.kernel.org/pub/software/scm/git/ 1.2 ...

  7. eclipse 4.3 汉化

    打开浏览器,浏览“参考资料”内给出的“eclipse语言包下载”地址,在博客新页面找到地址链接,如图所示.“Babel Language...”开头的一栏下面就是各个eclise版本的语言包,此处以I ...

  8. Mysql InnoDB表结构

    索引组织表 在InnoDB存储引擎中,表都是根据主键顺序组织存放的,这种存储方式的表称为索引组织表(index organized table).在InnoDB存储引擎表中,每张表都有个主键(Prim ...

  9. Android无线测试之—UiAutomator UiSelector API介绍之五

    对象搜索—文本与描述 一.文本属性定位对象: 返回值 API 描述 UiSelector test(String text) 文本完全匹配 UiSelector testContains(String ...

  10. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.2——设置Flavors和Variants

    问题: 需要构建大体上一样,但是使用不同资源或者类的应用. 解决方案: 产品的flavors可以帮助你对同一个app创建不同的版本. 讨论: build types是开发过程的一部分,一般用来将app ...