Recent Post
Print Pretty.cpp-HackerRank
- Get link
- X
- Other Apps
Problem-Your manager gave you a text file with many lines of numbers to format and print. For each row of 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, , the number of test cases.
Each of the subsequent lines describes a test case as space-separated floating-point numbers: , , and , respectively.
Output Format
For each test case, print lines containing the formatted , , and , respectively. Each , , and must be formatted as follows:
- : Strip its decimal (i.e., truncate it) and print its hexadecimal representation (including the prefix) in lower case letters.
- : Print it to a scale of 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 characters wide.
- : Print it to a scale of exactly nine decimal places, expressed in scientific notation using upper case.
Solution-
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int T; cin >> T;
cout << setiosflags(ios::uppercase);
cout << setw(0xf) << internal;
while(T--) {
double A; cin >> A;
double B; cin >> B;
double C; cin >> C;
cout << hex << showbase << nouppercase<<left;
cout << (long long)A << endl;
cout << setw(15) << showpos << setfill('_') << setprecision(2)<<fixed<<right;
cout << B << endl;
cout << scientific << setw(15) << setprecision(9) << uppercase<<noshowpos;
cout << C << endl;
}
return 0;
}
Link to the problem-
Result!
- Get link
- X
- Other Apps
Popular posts from this blog
Caesar Cipher.c-HackerRank
Problem- Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's Cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c. Original alphabet: abcdefghijklmnopqrstuvwxyz Alphabet rotated +3: defghijklmnopqrstuvwxyzabc For example, the given cleartext and the alphabet is rotated by . The encrypted string is . Note: The cipher only encrypts letters; symbols, such as - , remain unencrypted. Solution- #include < assert.h > #include < limits.h > #include < math.h > #include < stdbool.h > #include < stddef.h > #include < stdint.h > #include < stdio.h > #include < stdlib.h > #include < string.h > int mai...
Bon Appétit.c-HackerRank
Problem- Anna and Brian are sharing a meal at a restaurant and they agree to split the bill equally. Brian wants to order something that Anna is allergic to though, and they agree that Anna won't pay for that item. Brian gets the check and calculates Anna's portion. You must determine if his calculation is correct. Solution- #include < assert.h > #include < ctype.h > #include < limits.h > #include < math.h > #include < stdbool.h > #include < stddef.h > #include < stdint.h > #include < stdio.h > #include < stdlib.h > #include < string.h > int main() { int n,k,sum= 0 ,i,sum2= 0 ; int price= 0 ,extra= 0 ; scanf( "%d %d" ,&n,&k); int arr[n]; for (i= 0 ;i<n;i++) { scanf( "\n%d " ,&arr[i]); } ...
Electronics Shop.c-HackerRank
Problem- Monica wants to buy a keyboard and a USB drive from her favorite electronics store. The store has several models of each. Monica wants to spend as much as possible for the 2 items, given her budget. Given the price lists for the store's keyboards and USB drives, and Monica's budget, find and print the amount of money Monica will spend. If she doesn't have enough money to both a keyboard and a USB drive, print -1 instead. She will buy only the two required items . Solution- Code for the function provided:- int getMoneySpent( int n, int * keyboards, int m, int * drives, int b) { int sum,max=- 1 ; for ( int i= 0 ;i<n;i++) { for ( int j= 0 ;j<m;j++) { ...
Comments
Post a Comment