# 1078 : 짝수 합 구하기
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int sum = 0;
for(int i=1; i<=num; i++) {
if(i%2==0) {
sum += i;
}
}
System.out.println(sum);
sc.close();
}
}
# 1079 : 원하는 문자가 입력될 때까지 반복 출력
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
char ch = sc.next().charAt(0);
if(ch=='q') {
System.out.println(ch); // 'q'까지 출력
break;
}
System.out.println(ch);
}
sc.close();
}
}
# 1080 : 언제가지 더해야 할까?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int hap = sc.nextInt();
int sum = 0;
int i = 1;
while(true) {
sum += i;
if (sum>=hap) {
System.out.println(i);
break;
}
i++;
}
sc.close();
}
}
# 1081 : 주사위를 2개 던지면?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
System.out.println(i+ " "+j);
}
}
sc.close();
}
}
# 1082 : 16진수 구구단?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(16);
for(int i=1; i<16; i++) {
System.out.printf("%X*%X=%X\n", n, i, n*i);
}
sc.close();
}
}
// int n = sc.nextInt(16) : 16진수로 입력 받기
// %x : 16진수 소문자로 출력, %X : 16진수 대문자로 출력
# 1083 : 3 6 9 게임의 왕이 되자!
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n; i++) {
if(i%3==0) {
System.out.print("X ");
}
else {
System.out.print(i+" ");
}
}
sc.close();
}
}
# 1084 : 빛 섞어 색 만들기 (시간초과)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int g = sc.nextInt();
int b = sc.nextInt();
int count = 0;
for(int i=0; i<r; i++) {
for(int j=0; j<g; j++) {
for(int k=0; k<b; k++) {
System.out.println(i+" "+j+" "+k);
count++;
}
}
}
System.out.println(count);
}
}
// Scanner는 공란과 줄바꿈을 모두 입력값의 경계로 인식하기 때문에 좀 더 쉽게 데이터를 입력 받을 수 있지만, 데이터를 입력 받을 경우 즉시 사용자에게 전송하기에 이 부분에서 많은 시간이 소요된다.
또한, 내부에서 입력 데이터를 처리할 때 정규식을 많이 사용하기 때문에 상당한 시간이 소요된다.
★ 시간 초과 해결 방법 → BufferedReader
// BufferedReader는 큰 버퍼 사이즈를 가지고 있고, 일정한 크기의 데이터를 한번에 읽어 버퍼에 보관한 후, 사용자의 요청이 있을 때 버퍼에서 데이터를 읽어오는 방식이다. 속도가 빠르고, 시간 부하가 적다.
그럼에도 Scanner가 있는 이유는, BufferedReader는 모든 입력 데이터를 String으로 인식해서 형변환을 해줘야 하는 불편함이 있기 때문이다.
# 1084 : 빛 섞어 색 만들기
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));
String[] s = br.readLine().split(" ");
int count = 0;
for(int i=0; i<Integer.valueOf(s[0]); i++) {
for(int j=0; j<Integer.valueOf(s[1]); j++) {
for(int k=0; k<Integer.valueOf(s[2]); k++) {
bw.write(i+" "+j+" "+k+"\n");
count++;
}
}
}
bw.write(String.valueOf(count));
bw.flush();
}
}
# 1085 : 소리 파일 저장 용량 계산
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double h = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
double s = sc.nextDouble();
double result = (h*b*c*s) / 8 / 1024 / 1024;
System.out.printf("%.1f MB", result);
sc.close();
}
}
# 1086 : 그림 파일 저장 용량 계산
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double w = sc.nextDouble();
double h = sc.nextDouble();
double b = sc.nextDouble();
double result = (w*h*b) / 8 / 1024 / 1024;
System.out.printf("%.2f MB", result);
sc.close();
}
}
# 1087 : 여기까지! 이제 그만~
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for(int i=1; ; i++) {
sum += i;
if (sum>=n) {
System.out.println(sum);
break;
}
}
sc.close();
}
}
# 1088 : 3의 배수는 통과?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n; i++) {
if(i%3==0) {
continue;
}
System.out.print(i+" ");
}
sc.close();
}
}
CodeUp
☆ 파이썬 다운로드 : 파이썬3 ☆ 무료 C언어 IDE : Code::blocks DEV C++ ☆ 추천 온라인 IDE : C C++11 Python3 Java ☆ 채점 가능 언어 : C, C++, JAVA, Python 3.5 ★ C++로 제출시 void main()을 사용하면
codeup.kr
'CodeUp > Java' 카테고리의 다른 글
[CodeUp] 1089-1097 (Java) (0) | 2022.11.06 |
---|---|
[CodeUp] 1071-1077 (Java) (0) | 2022.11.04 |
[CodeUp] 1065-1070 (Java) (0) | 2022.11.03 |
[CodeUp] 1063-1064 (Java) (0) | 2022.11.02 |
[CodeUp] 1059-1062 (Java) (0) | 2022.11.01 |