Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Java BigDecimal :- HackerRank Java Solution

 Problem:-

Java’s BigDecimal class can handle arbitrary-precision signed decimal numbers. Let’s test your knowledge of them!Given an array,S , N of  real number strings, sort them in descending order — but wait, there’s more! Each number must be printed in the exact same format as it was read from stdin, meaning that .1  is printed as .1  , and 0.1  is printed as 0.1  . If two numbers represent numerically equivalent values (e.g. .1 = 0.1 , ), then they must be listed in the same order as they were received as input).

Complete the code in the unlocked section of the editor below. You must rearrange array s‘s elements according to the instructions above.

Input Format

The first line consists of a single integer, n, denoting the number of integer strings. 
Each line i of the n  subsequent lines contains a real number denoting the value of  si.

Constraints

  • < n < 200
  • Each  has at most  digits.

Output FormatLocked stub code in the editor will print the contents of array  to stdout. You are only responsible for reordering the array’s elements.Sample Input

9
-100
50
0
56.6
90
0.12
.12
02.34
000.000

Sample Output

90
56.6
50
02.34
0.12
.12
0
000.000
-100

Solution:-

import java.math.BigDecimal;


import java.util.*;


class Solution{



    public static void main(String []args){


        //Input


        Scanner sc= new Scanner(System.in);


        int n=sc.nextInt();


        String []s=new String[n+2];


        for(int i=0;i<n;i++){


            s[i]=sc.next();


        }


        sc.close();



    Arrays.sort(s, new Comparator<String>() {


        @Override


        public int compare(String o1, String o2) {


        if (o1 == null || o2 == null) {


            return 0;


        }


        BigDecimal o1bd = new BigDecimal(o1);


        BigDecimal o2bd = new BigDecimal(o2);


        return o2bd.compareTo(o1bd);


        }


    });



        //Output


        for(int i=0;i<n;i++)


        {


            System.out.println(s[i]);


        }


    }



}

Comments