반응형
1. 2739
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
for(int i = 1; i < 10; i++) {
System.out.println(number + " * " + i + " = " + number * i);
}
}
}
2. 10950
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int caseNum = scan.nextInt();
int first = 0;
int second = 0;
ArrayList<Integer> firstNums = new ArrayList<Integer>();
ArrayList<Integer> secondNums = new ArrayList<Integer>();
for(int i = 0; i < caseNum; i++) {
first = scan.nextInt();
second = scan.nextInt();
firstNums.add(first);
secondNums.add(second);
}
for(int i = 0; i < caseNum; i++) {
System.out.println(firstNums.get(i) + secondNums.get(i));
}
}
}
3. 8393
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
int result = 0;
for(int i = 1; i <= number; i++) {
result += i;
}
System.out.println(result);
}
}
4. 15552'
알고리즘을 하면서 I/O 개념을 확실히 이해할 수 있었던 문제였다.
간단하게 Scanner = BufferedReader, sysout = BufferedWriter
자바 데이터 타입에 더 신경을 써야되지만 효율은 데이터가 클수록 buffered가 좋다는게 속도에서 나타난네요
이제 부터 웬만하면 buffered을 사용해서 알고리즘의 효율을 신경쓰겠습니다
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int caseNum = Integer.parseInt(br.readLine());
for(int i = 0; i < caseNum; i++) {
String[] temp = br.readLine().split(" ");
bw.write(Integer.parseInt(temp[0]) + Integer.parseInt(temp[1]) + "\n");
}
bw.flush();
}
//understanding input and output efficiency is the main point
//and scanner = bufferedreader and sysout = bufferedwriter
//BufferedReader is a bit faster as compared
//to scanner because scanner does parsing of input
//data and BufferedReader simply reads sequence of characters.
}
5. 2741
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int number = Integer.parseInt(br.readLine());
for(int i = 1; i <= number; i++) {
bw.write(String.valueOf(i) + "\n");
}
bw.flush();
}
}
6. 2742
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
for(int i = n; i > 0; i--) {
bw.write(i + "\n");
}
bw.flush();
}
}
7. 11021
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception{
//takes 72ms, about almost 1.5faster than scanner and sysout
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int caseNum = Integer.parseInt(br.readLine());
for(int i = 1; i <= caseNum; i++) {
String[] temp = br.readLine().split(" ");
bw.write("Case #" + i + ": "+ (Integer.parseInt(temp[0]) + Integer.parseInt(temp[1])) + "\n");
}
bw.flush();
// Scanner scan = new Scanner(System.in);
// int caseNum = scan.nextInt();
//
// for(int i = 1; i <= caseNum; i++) {
// int first = scan.nextInt();
// int second = scan.nextInt();
//
// System.out.println("Case #" + i + ": " + (first + second));
//
// }
//takes 128ms
}
}
8. 11022
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out));
int caseNum = Integer.parseInt(br.readLine());
for(int i = 1; i <= caseNum; i++) {
String[] temp = br.readLine().split(" ");
wr.write("Case #" + i + ": " + temp[0] + " + " + temp[1] + " = " + (Integer.parseInt(temp[0]) + Integer.parseInt(temp[1])) + "\n");
}
wr.flush();
}
}
9. 2438
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out));
int caseNum = Integer.parseInt(br.readLine());
for(int i = 0; i < caseNum; i++) {
for(int j = 0; j <= i; j++) {
wr.write("*");
}
wr.write("\n");
}
wr.flush();
}
}
10. 2439
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int caseNum = Integer.parseInt(br.readLine());
for(int i = 0; i < caseNum; i++) {
for(int j = i; j < caseNum - 1; j++ ) {
bw.write(" ");
}
for(int j = 0; j <= i; j++) {
bw.write("*");
}
bw.write("\n");
}
bw.flush();
}
}
11. 10871
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] amountNcompare = br.readLine().split(" ");
String[] numberList = br.readLine().split(" ");
for(int i = 0; i < Integer.parseInt(amountNcompare[0]); i++) {
if(Integer.parseInt(amountNcompare[1]) > Integer.parseInt(numberList[i])) {
bw.write(numberList[i] + " ");
}
}
bw.flush();
}
}
반응형
'IT > 알고리즘' 카테고리의 다른 글
백준 5단계(10818) (0) | 2019.11.23 |
---|---|
백준 4단계(11/23/19)(10952, 10951, 1110) (0) | 2019.11.23 |
백준 2단계(11/23/19)(1330, 9498, 2753, 2884, 10817) (0) | 2019.11.23 |
백준 1단계(11/23/19)(2557, 10718, 10171, 10172, 7287, 1000, 1001, 10998, 1008, 10869, 10430, 2588) (0) | 2019.11.23 |
코딩테스트 (11/21/19) (0) | 2019.11.21 |