Menu Close

Mana and trail of Stones ….Java FTC

Mana is out on a hike with friends. She finds a trail of stones with numbers on them. She starts following the trail and notices that two consecutive stones have a difference of either a or b. Legend has it that there is a treasure trove at the end of the trail and if Mana can guess the value of the last stone, the treasure would be hers.

Given that the number on the first stone was 0, find all the possible values for the number on the last stone.

Note: The numbers on the stones are in increasing order.

Input Format
The first line contains an integer T , i.e. the number of test cases. T test cases follow; each has 3 lines.
The first line contains n (the number of stones).
The second line contains a, and
The third line contains b.

Constraints
1<=T<=10
1<=n,a,b<=103

Output Format
Space-separated list of numbers which are the possible values of the last stone in increasing order.

Input:
TestCases: 2
No: of Stones: 3
A value: 1
B value: 2

No: of Stones: 4
A value: 10
B values: 100

Output:
2 3 4 
30 120 210 300 
import java.io.*;
import java.util.*;
public class temp{
  public static void main(String[] args) {
    int t,a,b,n;
    Scanner in = new Scanner(System.in);
    System.out.println("No: of TestCases: ");
    t = in.nextInt();
    while(t>0){
      System.out.println("No: of Stones: ");
      n = in.nextInt();
      System.out.println("A Value: ");
      a = in.nextInt();
      System.out.println("B Value: ");
      b = in.nextInt();
     	if(a>b){
        int tem = a;
        a = b;
        b = tem;
      }
      int val = ((int)--n)*a;
      short diff = (short)(b - a);
      System.out.println("\nPossible Values:  ");
     	if (diff > 0){
        while(n-- > 0){
          System.out.print(val+" ");
          val += diff;
        }
        System.out.print(val+" ");
      }
     --t;
     System.out.println("\n");
   }
 }
}

INPUT_1:
No: of TestCases:
2
No: of Stones:
6
A Value:
4
B Value:
8

No: of Stones:
11
A Value:
3
B Value:
10

OUTPUT:
Possible Values:
20  24  28  32  36  40                                              //First test case values
30  37  44  51  58  65  72  79  86  93  100          //Second test case values


INPUT_2:
No: of TestCases:
2
No: of Stones:
3
A Value:
1
B Value:
2

No: of Stones:
4
A Value:
10
B Value:
100

OUTPUT:
Possible Values:
2  3  4
30  120  210  300


ILLUSTRATION

Executed using javac in Ubuntu Terminal

Leave a Reply

Your email address will not be published. Required fields are marked *