백준 TEST04_if문 사용해보기

baekjoon_java

백준 TEST04_if문 사용해보기



기본 필요 패키지 및 외부클래스

1
2
3
4
5
6
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;

시험성적

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class TEST04_01{
public TEST04_01() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = 0;

while(true){
n = Integer.parseInt(br.readLine());
if(n>= 0 && n<101){
break;
}
}

switch (n/10) {
case 10 :
case 9 :
bw.write("A\n");
bw.flush();
break;
case 8 :
bw.write("B\n");
bw.flush();
break;
case 7 :
bw.write("C\n");
bw.flush();
break;
case 6 :
bw.write("D\n");
bw.flush();
break;
default :
bw.write("F\n");
bw.flush();
break;
}
}
}

public class TEST04{
public static void main(String[] args) throws IOException{
TEST04_01 test04_01 = new TEST04_01();
}
}

세 수 - 버전1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class TEST04_02_01{
public TEST04_02_01() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

int A,B,C;

String a[] = br.readLine().split(" ");//입력과 동시에 " "공백을 구분하여 배열을 나눈다.
A = Integer.parseInt(a[0]);
B = Integer.parseInt(a[1]);
C = Integer.parseInt(a[2]);

if ((A>=B)&&(A<=C)||(A>=C)&&(A<=B)){
bw.write(A+"\n");
bw.flush();
bw.close();
}
else if((B>=C)&&(B<=A)||(B>=A)&&(B<=C)){
bw.write(B+"\n");
bw.flush();
bw.close();
}
else{
bw.write(C+"\n");
bw.flush();
bw.close();
}
}
}
public class TEST04{
public static void main(String[] args) throws IOException{
TEST04_02_01 test04_02_01 = new TEST04_02_01();
}
}

세 수 - 버전2 (정렬이용)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class TEST04_02_02{
public TEST04_02_02() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

int A,B,C;
int tmp;
ArrayList<Integer> num = new ArrayList<>();

String a[] = br.readLine().split(" ");//입력과 동시에 " "공백을 구분하여 배열을 나눈다.
A = Integer.parseInt(a[0]);
B = Integer.parseInt(a[1]);
C = Integer.parseInt(a[2]);
num.add(A);
num.add(B);
num.add(C);

//버블정렬
for(int i=1;i<num.size();i++){
for(int j=0;j<num.size()-i;j++){
if(num.get(j)>=num.get(j+1)){
//num.get(1);//확인. num.set(1,temp);//수정.
tmp = num.get(j);
num.set(j, num.get(j+1));
num.set(j+1, tmp);
}
}
}
/*
//선택정렬
for(int i=0;i<num.size()-1;i++){
for(int j=i+1;j<num.size();j++){
if(num.get(i) >= num.get(j)) {
tmp = num.get(j);
num.set(j,num.get(i));
num.set(i,tmp);
}
}
}
*/
bw.write(num.get(1)+"\n");
bw.flush();
bw.close();

}
}

public class TEST04{
public static void main(String[] args) throws IOException{
TEST04_02_02 test04_02_02 = new TEST04_02_02();
}
}

X보다 작은 수

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class TEST04_03{

public TEST04_03() throws IOException{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

int n = 0;
int i = 0;
int x = 0;

while(true){
String[] a = br.readLine().split(" ");
n = Integer.parseInt(a[0]);
x = Integer.parseInt(a[1]);

if(x>=1 && x<=10000 && n>=1 && n<=10000){
break;
}
}

String[] A = br.readLine().split(" ");

while(true){
for(i=0;i<n;i++){
int y = Integer.parseInt(A[i]);
if(y<=0 && y>=10000){
break;
}

if(y<x){
bw.write(y+" ");
}
}

if(i==n){
break;
}
}

bw.flush();
bw.close();
}
}
public class TEST04{
public static void main(String[] args) throws IOException{
TEST04_03 test04_03 = new TEST04_03();
}
}

평균

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class TEST04_04{
public TEST04_04() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
ArrayList<Integer> array = new ArrayList<>(); //정수형을 값을 저장할 리스트 공간
int n,i,tmp = 0; //n은 과목수 , i는 조건을 위한 수 , tmp는 임시저장소 , sum 최종합
double sum = 0;

while(true){
n = Integer.parseInt(br.readLine());
if(n<=1000) break;
}

while(true){
String[] a = br.readLine().split(" ");
for(i=0;i<n;i++){
tmp = Integer.parseInt(a[i]);
array.add(Integer.parseInt(a[i]));
if(tmp>100 && tmp<=0){
break;
}
}
if(i==n){
break;
}
}

for(i=0;i<array.size()-1;i++){
for(int j=i+1;j<array.size();j++){
if(array.get(i)>array.get(j)){
tmp = array.get(i);
array.set(i, array.get(j));
array.set(j,tmp);

}
}
}//선택정렬 이용

for(i = 0;i<array.size();i++){
sum += array.get(i);
}

bw.write((double)(sum/array.size()/array.get(array.size()-1)*100.0)+"\n");
bw.flush();
bw.close();
}
}

public class TEST04{
public static void main(String[] args) throws IOException{
TEST04_04 test04_04 = new TEST04_04();
}
}

텍스트 추가

\05. 평균은 넘겠지

텍스트 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class TEST04_05{
public TEST04_05() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
ArrayList<Double> number = new ArrayList<>();
int C = Integer.parseInt(br.readLine());
int j = 0;
double hap = 0;

while(true){
ArrayList<Integer> array = new ArrayList<>();
double num = 0;
int sum = 0;
double avg = 0;
String[] x = br.readLine().split(" ");

if(Integer.parseInt(x[0])<1 && Integer.parseInt(x[0])>1000){ break; }
//System.out.println("잘못입력하였습니다.");

for(int i=0;i<=Integer.parseInt(x[0]);i++){
array.add(Integer.parseInt(x[i]));
if(Integer.parseInt(x[i])<0 && Integer.parseInt(x[i])>100){ break; }
//System.out.println("잘못입력하였습니다.");
}

sum = 0;
for(int i=1;i<array.size();i++){
sum = sum + array.get(i);
}

avg = sum/array.get(0);
for(int i=1;i<array.size();i++){
if(array.get(i)>avg){
num++;
}
}
hap = num/array.get(0);
number.add((num/array.get(0))*100);

j++;
if(j==C){ break; }
}

for(int i=0;i<C;i++){
System.out.printf("%.3f",number.get(i));
System.out.println("%");
}

/*
for(int i=0;i<C;i++){
bw.write(number.get(i)+"%"+"\n");
}
bw.flush();
bw.close();
*/
}
}

public class TEST04{
public static void main(String[] args) throws IOException{
TEST04_05 test04_05 = new TEST04_05();
}
}

더하기 사이클

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class TEST04_06{
public TEST04_06() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

int n = 0;
int xx = 0;
int x = 0;
int fin = 0;
int cycle_num = 0;

while(true){
n = Integer.parseInt(br.readLine());
fin = n;
if(n>=0 && n<=99){
break;
}// 아닐경우 무한반복
}

while(true){
xx = n/10;
x = n%10;
n = (xx + x)%10;
n = (x*10)+n;
cycle_num++;
if(fin == n){ break; }
}

bw.write(cycle_num+"\n");

bw.flush();
bw.close();

}
}


public class TEST04{
public static void main(String[] args) throws IOException{
TEST04_06 test04_06 = new TEST04_06();
}
}
Share