[HackerRank] Solve Java
2021. 1. 4. 19:43ㆍHi/Java
2. Java If-Else
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if (N%2 != 0)
{System.out.println("Weird");}
else if (N>=2 && N<=5)
{System.out.println("Not Weird");}
else if (N>5 && N<=20)
{System.out.println("Weird");}
else if (N>20)
{System.out.println("Not Weird");}
scanner.close();
}
}
3. Java Stdin and Stdout I
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
4. Java Stdin and Stdout II
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();//버퍼주기
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
5. Java Output Formatting
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("================================");
for(int i=0;i<3;i++){
String s1=sc.next();
int x=sc.nextInt();
System.out.printf("%-15s%03d \n", s1, x);
}
System.out.println("================================");
}
}
6. Java Loops I
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int i = 1;
while(i<11){
int result = N * i;
System.out.printf("%d x %d = %d\n",N,i,result );
i++;
}
scanner.close();
}
}
7. Java Loops II
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for(int j=0; j<n; j++){
a= a+b;
if(j>0){
System.out.print(" ");
}
System.out.print(a);
b= 2*b;
}
System.out.printf("\n");
}
in.close();
}
}
8. Java Datatypes
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
try
{
long x=sc.nextLong();
System.out.println(x+" can be fitted in:");
if(x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE)
System.out.println("* byte");
if(x >= Short.MIN_VALUE && x <= Short.MAX_VALUE)
System.out.println("* short");
if(x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE)
System.out.println("* int");
if(x >= Long.MIN_VALUE && x <= Long.MAX_VALUE)
System.out.println("* long");
}
catch(Exception e)
{
System.out.println(sc.next()+" can't be fitted anywhere.");
}
}
}
}
9. Java End-of-file
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) {
Scanner scan = new Scanner(System.in);
int i = 1;
while(scan.hasNext()){
System.out.println(i+ " "+ scan.nextLine());
i++;
}
//scan.hasNext()는 다음신호가 있을때까지 무한반복임. 아니면 값을 넣어주면 True를 반환하기도함.
scan.close();
//scan.close()는 스캔을 그만두는것.
}
}
10. Java Static Initializer Block
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
private static int B;
private static int H;
private static boolean flag;
static{
Scanner scan = new Scanner(System.in);
B = scan.nextInt();
H = scan.nextInt();
scan.close();
if(B<=0 || H<=0)
{System.out.println("java.lang.Exception: Breadth and height must be positive");
flag = false;}
else {flag = true;
}
}
public static void main(String[] args){
if(flag){
int area=B*H;
System.out.print(area);
}
}//end of main
}//end of class
--
Java Date and Time
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
class Result {
public static String findDay(int month, int day, int year) {
Calendar cal = Calendar.getInstance();
//캘린더클래스사용방법 로컬시스템시간정보를 표현하도록함.
cal.set(Calendar.MONTH, month-1);
//:1월= 0 이라 -1해줌.
cal.set(Calendar.DATE, day);
cal.set(Calendar.YEAR, year);
//현제 로컬날짜를 설정해서 바꿈.
String[] day_of_week = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY","SATURDAY"};
//문제에서 string으로 표현하라했으니 설정
return day_of_week[cal.get(Calendar.DAY_OF_WEEK)-1];
//일요일은 = 1, 토요일=7
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int month = Integer.parseInt(firstMultipleInput[0]);
int day = Integer.parseInt(firstMultipleInput[1]);
int year = Integer.parseInt(firstMultipleInput[2]);
String res = Result.findDay(month, day, year);
bufferedWriter.write(res);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
728x90
'Hi > Java' 카테고리의 다른 글
[백준] 6단계 문제 모음 (0) | 2021.01.11 |
---|---|
[백준]5단계 문제 모음 (0) | 2021.01.08 |
[백준]4단계 문제 모음 (0) | 2021.01.03 |
[백준]3단계 문제 모음 (0) | 2021.01.01 |
[백준] 2단계 문제 모음 (0) | 2021.01.01 |