본문 바로가기
java 개발일지

백준 Class 1 java #10951 ~ #1546

by 노랑사랑팽이 2022. 9. 26.

🌼 백준 10951

📝 EOF

End of File

EOF End of File 읽을 수 있는 데이터가 존재하지 않을 때 반복문 종료 
Scanner in; 경우, while(in.hasNextInt()) 로 설정해주면 됨.

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

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str;
		
		while((str = br.readLine()) != null)
		{
			String[] ab = str.split(" ");
			int a = Integer.parseInt(ab[0]);
			int b = Integer.parseInt(ab[1]);
			System.out.println(String.valueOf(a + b));
		}
	
	}
}

 

 

🌼 백준 10952

📝 입력이 "0 0" 일때까지 두 수 더한 값 출력

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

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		while(true)
		{
			String[] ab = br.readLine().split(" ");
			int a = Integer.parseInt(ab[0]);
			int b = Integer.parseInt(ab[1]);
			if(a + b == 0)
				break;
			System.out.println(String.valueOf(a + b));
		}
		
	}
}

 

 

🌼 백준 10998

A * B

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		System.out.println(a*b);
		
	}
}

 

 

🌼 백준 1110

더하기 사이클

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(); //26 
		sc.close();
		int digitTwo; //10의 자리 2
		int digitOne; //1의 자리 6
		int digitSum; // 8
		int cnt = 0;
		int nn = n; // n 복사
		
		while(true)
		{
			digitTwo = n / 10; 
			digitOne = n % 10; 
			digitSum = digitTwo + digitOne;
			n = 10 * digitOne + (digitSum) % 10;
			cnt++;
			
			if(nn == n) 
				break;
				
			
		}
		System.out.println(cnt);
			
	}
}

 

 

🌼 백준 1152

📝 StringTokenizer

빈칸으로 구분

import java.util.Scanner;
import java.util.StringTokenizer;

public class one_1152 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		sc.close();

		StringTokenizer st = new StringTokenizer(str);
//		System.out.println(st.nextToken());
		System.out.println(st.countTokens());
		
	}
}

 

📝 String.split(" ")

빈칸으로 나누기

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String[] str = sc.nextLine().split(" ");
		int c = 0;
		
		for(int i = 0; i < str.length; i++)
		{
			if(str[i] != "")
				c++;
		}
		System.out.println(c);
        
	}
}

 

 

🌼 백준 1157

📝 max값이 같은 경우 count

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.Format;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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 str = br.readLine();
		br.close();
//		String alphabet = "abcdefghijklmnopqrstuvwxyz";
//		String alphaBet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		int[] cnt = new int[26];
		int max = Integer.MIN_VALUE; // Integer가 가질 수 있는 가장 작은 값
		int index = 0;
		char c;
		
		for(int i = 0; i < str.length(); i++)
		{
//			for(int j = 0; j < 26; j++)
//			{
//				if(str.charAt(i) == alphabet.charAt(j) || str.charAt(i) == alphaBet.charAt(j))
//					cnt[j]++;
//			}
			char cur = str.charAt(i);
			if(cur >= 'a' && cur <='z') {
				cnt[cur-'a']++;
				if(max < cnt[cur-'a']) {
					max = cnt[cur-'a'];
				}
			} else {
				cnt[cur-'A']++;
				if(max < cnt[cur-'A']) {
					max = cnt[cur-'A'];
				}
			}
		}
		
		int maxCnt = 0;
		int maxIndex = 0;
		for(int z=0;z<26;z++) {
			if(cnt[z] == max) {
				maxIndex = z;
				maxCnt++;
			}
		}
		
		if(maxCnt >= 2) {
			bw.write('?');
		} else {
			bw.write((char)(maxIndex+'A'));
		}
	
		bw.flush();
		
	}
}

 

📝 List 이용

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.Format;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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 str = br.readLine();
		br.close();
        
		int[] cnt = new int[26];
		int max = Integer.MIN_VALUE;
		int index = 0;
		char c;
		
		for(int i = 0; i < str.length(); i++)
		{
			char cur = str.charAt(i);
			if(cur >= 'a' && cur <='z') {
				cnt[cur-'a']++;
				if(max < cnt[cur-'a']) {
					max = cnt[cur-'a'];
				}
			} else {
				cnt[cur-'A']++;
				if(max < cnt[cur-'A']) {
					max = cnt[cur-'A'];
				}
			}
		}

				
		ArrayList<Character> list = new ArrayList<>();
		for(int z=0;z<26;z++) {
			if(cnt[z] == max) {
				char maxIndex = (char)(z+'A');
				list.add(maxIndex);
			}
		}
		
		if(list.size() == 1) {
			bw.write(list.get(0));
		} else if(list.size()>=2) {
			bw.write('?');
		}


		bw.flush();
		
	}
}

 

 

🌼 백준 11654

📝 아스키코드

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		char a = sc.nextLine().charAt(0);
		sc.close();
		int n = a;
		
		System.out.println(n);
		
	}
}

 

 

🌼 백준 11720

📝 풀이 1  

문자 '56' - '0' = 숫자 56

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String str = sc.nextLine();
		sc.close();
		int[] num = new int[n];
		int sum = 0;
		
		for(int i = 0; i < n; i++)
		{
			num[i] = str.charAt(i) - '0'; // 아스키코드 '0' 는 48
			sum = sum + num[i];
		}
		System.out.println(sum);
		
	}
}

 

📝 풀이 2

Character.getNumericValue(문자)

문자로 입력된 '5' 를 정수형으로 변환

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String str = sc.nextLine();
		sc.close();
		int[] num = new int[n];
		int sum = 0;
		
		for(int i = 0; i < n; i++)
		{
			num[i] = Character.getNumericValue(str.charAt(i));
			sum = sum + num[i];
		}
		System.out.println(sum);
		
	}
}

 

 

🌼 백준 1330

크기비교

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		
		if(a > b)
			System.out.println('>');
		else if(a < b)
			System.out.println('<');
		else
			System.out.println("==");
	}

}

 

 

🌼 백준 1546

평균

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String[] str = sc.nextLine().split(" ");
		sc.close();
		int[] num = new int[n]; 
		int max;
		double sum = 0;
		
		for(int i = 0; i < n; i++)
		{
			num[i] = Integer.parseInt(str[i]);
		}
		
		Arrays.sort(num); // 정렬
		max = num[num.length - 1];
		for(int i = 0; i < n; i++)
		{
			sum += (double)num[i] / max * 100;
		}
		
		System.out.println(sum / n);
		
	}
}

댓글