Algorithm


Problem Name: Print Pretty

Problem Link: https://www.hackerrank.com/challenges/prettyprint/problem?isFullScreen=true

In this HackerRank Functions in C++ programming problem solution,

Given a text file with many lines of numbers to format and print, for each row of 3 space-separated doubles, format and print the numbers using the specifications in the Output Format section below.

Input Format

The first line contains an integer, T, the number of test cases.
Each of the T subsequent lines describes a test case as 3 space-separated floating-point numbers: A, B and C, respectively.

Constraints

  • 1 <= T <= 1000
  • Each number will fit into a double.

Output Format

For each test case, print 3 lines containing the formatted A , B , and C.respectively. Each A , B , and Cmust be formatted as follows:

 

  1. A: Strip its decimal (i.e., truncate it) and print its hexadecimal representation (including the 0x prefix) in lower case letters.
  2. B : Print it to a scale of 2 decimal places, preceded by a or sign (indicating if it's positive or negative), right justified, and left-padded with underscores so that the printed result is exactly 15 characters wide.
  3. C: Print it to a scale of exactly nine decimal places, expressed in scientific notation using upper case.

Sample Input

1  
100.345 2006.008 2331.41592653498

Sample Output

0x64             
_______+2006.01  
2.331415927E+03

 

 

 

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
#include <iomanip>
using namespace std;

int main(void)
{
    int T;
    cin >> T;
    cout << setiosflags(ios::uppercase);
    cout << setw(0xf) << internal;

    while(T--){
        double A, B, C;
        cin >> A;
        cin >> B;
        cin >> C;

        cout << setw(0) << showbase << hex << left << nouppercase << (long long)A << endl;
        cout << setw(15) << fixed << setprecision(2) << setfill('_') << right << showpos << B << endl;
        cout << setw(15) << scientific << setprecision(9) << left << noshowpos << uppercase << C << endl;
    }

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1 100.345 2006.008 2331.41592653498

Output

x
+
cmd
0x64 _______+2006.01 2.331415927E+03
Advertisements

Demonstration


Previous
[Solved] Maps-STL in C++ solution in Hackerrank - Hacerrank solution C++
Next
[Solved] Inheritance Introduction in C++ solution in Hackerrank - Hacerrank solution C++