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. HTML5之本地存储localstorage

    Web Storage是HTML5引入的一个非常重要的功能,可以在客户端本地存储数据,类似HTML4的cookie,但可实现功能要比cookie强大的多,cookie大小被限制在4KB,Web Sto ...

  2. 关于fork()父子进程返回值的问题

    我们都知道,父进程fork()之后返回值为子进程的pid号,而子进程fork()之后的返回值为0.那么,现在就有一个问题了,子进程fork()的返回值是怎么来的?如果子进程又执行了一遍fork()函数 ...

  3. Java进阶02 异常处理(转载)

    异常处理 Java的异常处理机制很大一部分来自C++.它允许程序员跳过暂时无法处理的问题,以继续后续的开发,或者让程序根据异常做出更加聪明的处理. Java使用一些特殊的对象来代表异常状况,这样对象称 ...

  4. python 集合set remove update add

    1. 集合(set):把不同的元素组成一起形成集合,是python基本的数据类型. 集合对象是一组无序排列hashable value:集合成员可以做字典的键. 集合就像是 list 和 dict 的 ...

  5. windows 下XAMPP 使用Nginx替代apache作为服务器

    说实话, 在windows下使用Nginx 着实有点不太方便, 但因项目需求, 又不想换系统(虽然可以搞个虚拟机玩), 只能用Nginx了 好了, 不多说了. 开始... 首先我用的是xampp包(A ...

  6. Laravel Lumen 数组操作

    php原生:http://www.w3school.com.cn/php/php_ref_array.asp Lumen方法:https://laravel.com/docs/5.6/helpers ...

  7. C#中的Form,textBox,Bitmap,PictureBox,Button,WebBrowser

    因为今天是用的家里的电脑,用的不是retena屏的mac,因此稍后截图的时候大家看到的图片可能会有一些模糊,还请大家海涵... 兄弟们,我胡汉三又回来啦! 以下開始我们的第一个C#程序.我们之前已经通 ...

  8. A guide to analyzing Python performance

    来源:http://www.huyng.com/posts/python-performance-analysis/ While it's not always the case that every ...

  9. 学习boost::asio一些小例子

    # #include <boost/asio.hpp> #include <boost/thread.hpp> #include <iostream> void h ...

  10. filter和find区别,元素遍历

    转 filter和find区别 find()会在当前指定元素中查找符合条件的子元素,是对它的子集操作,而filter()则是在当前指定的元素集合中查找符合条件的元素,是对自身集合元素进行筛选. HTM ...