Java Datatypes
Problem Statement :
Java has 8 primitive data types; char, boolean, byte, short, int, long, float, and double. For this exercise, we'll work with the primitives used to hold integer values (byte, short, int, and long): A byte is an 8-bit signed integer. A short is a 16-bit signed integer. An int is a 32-bit signed integer. A long is a 64-bit signed integer. Given an input integer, you must determine which primitive data types are capable of properly storing that input. To get you started, a portion of the solution is provided for you in the editor. Reference: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html Input Format The first line contains an integer, T, denoting the number of test cases. Each test case, T, is comprised of a single line with an integer, , which can be arbitrarily large or small. Output Format For each input variable n and appropriate primitive datatype, you must determine if the given primitives are capable of storing it. If yes, then print: n can be fitted in: * dataType If there is more than one appropriate data type, print each one on its own line and order them by size (i.e.: byte < short < int < long ). If the number cannot be stored in one of the four aforementioned primitives, print the line: n can't be fitted anywhere. Sample Input 5 -150 150000 1500000000 213333333333333333333333333333333333 -100000000000000 Sample Output -150 can be fitted in: * short * int * long 150000 can be fitted in: * int * long 1500000000 can be fitted in: * int * long 213333333333333333333333333333333333 can't be fitted anywhere. -100000000000000 can be fitted in: * long
Solution :
Solution in C :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String testCases = br.readLine();
int N = Integer.parseInt(testCases);
for (int i = 0; i < N; i++) {
//BufferedReader brr = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
try {
long n = Long.parseLong(line);
if (n == 0 || n == 1) {
System.out.println(n + " can be fitted in:");
System.out.println("* boolean");
System.out.println("* byte");
System.out.println("* short");
System.out.println("* int");
System.out.println("* long");
continue;
} else if (n == (byte)n) {
System.out.println(n + " can be fitted in:");
System.out.println("* byte");
System.out.println("* short");
System.out.println("* int");
System.out.println("* long");
continue;
}
else if (n == (short)n) {
System.out.println(n + " can be fitted in:");
System.out.println("* short");
System.out.println("* int");
System.out.println("* long");
continue;
} else if (n == (int)n) {
System.out.println(n + " can be fitted in:");
System.out.println("* int");
System.out.println("* long");
continue;
} else {
System.out.println(n + " can be fitted in:");
System.out.println("* long");
continue;
}
} catch (NumberFormatException e) {
System.out.println(line + " can't be fitted anywhere.");
continue;
}
}
}
}
View More Similar Problems
Poisonous Plants
There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan
View Solution →AND xor OR
Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value
View Solution →Waiter
You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the
View Solution →Queue using Two Stacks
A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que
View Solution →Castle on the Grid
You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):
View Solution →Down to Zero II
You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.
View Solution →