백준 TEST06_1차원 배열 사용하기

baekjoon_java

백준 TEST06_1차원 배열 사용하기



필요한 패키지 및 외부 클래스

1
2
3
4
5
6
7
package test_java;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

단어의개수

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
class TEST06_01 {
public TEST06_01() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String n = br.readLine();
int count=1;
for(int i=0;i<n.length();i++){
char chrInput = n.charAt(i);

if(n.charAt(i) ==' '){
count++;
}

if (chrInput < 0x61 && chrInput > 0x7A) { break; }
// 영문(소문자)가 아닐경우 무한 반복!
else if (chrInput < 0x41 && chrInput > 0x5A) { break; }
// 영문(대문자)가 아닐 경우 무한 반복!
}// for end

//count++;
if(n.charAt(0)==' ' && n.charAt(n.length()-1)==' '){
count--;
count--;
}
else if(n.charAt(0)==' ' || n.charAt(n.length()-1)==' '){
count--;
}

bw.write(count+"\n");
bw.flush();
bw.close();
}
}

숫자의 개수

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
class TEST06_02{
public TEST06_02() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int[] num = new int[10];
int A = Integer.parseInt(br.readLine());
int B = Integer.parseInt(br.readLine());
int C = Integer.parseInt(br.readLine());

//bw.write( A * B * C +"\n");

String sum = A * B * C +"";

//bw.write(sum);
char[] a = sum.toCharArray();

for(int i=0;i<a.length;i++){
switch(a[i]){
case '0' : num[0]++;
break;
case '1' : num[1]++;
break;
case '2' : num[2]++;
break;
case '3' : num[3]++;
break;
case '4' : num[4]++;
break;
case '5' : num[5]++;
break;
case '6' : num[6]++;
break;
case '7' : num[7]++;
break;
case '8' : num[8]++;
break;
case '9' : num[9]++;
break;
}
}

for(int i=0;i<10;i++){
bw.write(num[i]+"\n");
}
bw.flush();
bw.close();
}
}

OX퀴즈

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 TEST06_03{
public TEST06_03() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

int t_case = Integer.parseInt(br.readLine());
int[] num = new int[t_case];
String[] ox = new String[t_case];
// O- 0x4F X-0x58

while(true){
int i = 0;
for(i=0;i<t_case;i++){
int j = 0;
ox[i] = br.readLine();
char[] chrInput= ox[i].toCharArray();
if(ox[i].length() > 80 || ox[i].length() < 0){
break; //MAIN FOR BREAK
}
for(j=0;j<ox[i].length();j++){
if(chrInput[j] == 'O' ||chrInput[j] == 'X') continue;
else break; //SUB FOR BREAK
}
if(j!=ox[i].length()) break; //MAIN FOR BREAK
}
if(i==t_case) break; //O혹은 X가 아닐경우 처음 부터 다시
}//while end

for(int i=0;i<t_case;i++){
char[] sub = ox[i].toCharArray();

int score = 0;
for(int j=0;j<ox[i].length();j++){
if(sub[j] == 'O'){
score++;
num[i] += score;
}
else
score = 0;
}// for - j end

bw.write(num[i] + "\n");
}//for - i end

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

}
}

음계

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
class TEST06_04{
public TEST06_04() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

String[] m = br.readLine().split(" ");
String[] x = new String[8];
String[] y = new String[8];
int count = 0 ;

for(int i=0;i<m.length;i++){
x[i] = String.valueOf(i+1);
y[i] = String.valueOf(8-i);
if(x[i].equals(m[i])){
count++;
}

if(y[i].equals(m[i])){
count--;
}
}

// bw.write(m.length+"\n");
// bw.write(x[3]+"\n");

if(m[0].equals("1") && count == 8){
bw.write("ascending");
}
else if(m[0].equals("8") && count == -8){
bw.write("descending");
}
else{
bw.write("mixed");
}
bw.flush();
bw.close();
}
}

평균 점수

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
class TEST06_05{
public TEST06_05() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

int sum = 0;

while(true){
int i=0;
for(i=0;i<5;i++){
int n = Integer.parseInt(br.readLine());

if(n<0 || n%5!=0){ break; }

if(n<40) n=40;

sum += n;
}//main for end

if(i==5){ break; }
}//while end

bw.write(sum/5 +"\n");

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

Main

1
2
3
4
5
6
7
8
9
public class TEST06 {
public static void main(String[] args) throws IOException{
// TEST06_01 test06_01 = new TEST06_01();
// TEST06_02 test06_02 = new TEST06_02();
// TEST06_03 test06_03 = new TEST06_03();
// TEST06_04 test06_04 = new TEST06_04();
TEST06_05 test06_05 = new TEST06_05();
}
}
Share