자바로 백준 풀면서 입력 부분이나 자주 나오는 처리 부분을 매번 작성하기 귀찮아서 intellij `live tempalte`에 자주 사용하는 기능들을 클래스로 담아서 만들었다. 문제 풀면서 더 추가할 예정. 사실상 입력만들때만 자주 쓰게된다.
class Solution extends Algorithm{
public Solution(){
$END$
}
}
class Algorithm{
Buf buf = new Buf();
public<T> void fill(T input, T[][] array){
for (T[] ts : array) {
Arrays.fill(ts, input);
}
}
public Float readFloat() {
return buf.readFloat();
}
public Double readDouble() {
return buf.readDouble();
}
public int[][] readIntArray(int n, int m){
int[][] a = new int[n][m];
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
a[i][j] = buf.readInt();
}
}
return a;
}
public int readInt(){
return buf.readInt();
}
public String read(){
return buf.read();
}
public char[][] readIndexOf(int n, int m){
char[][] a = new char[n][m];
for (int i = 0; i < n; i++){
String s = buf.read();
a[i] = s.toCharArray();
}
return a;
}
public boolean checkArrayIndexOut(int arrayMaxX, int arrayMaxY, int x, int y){
return 0 <= x && x < arrayMaxX && 0 <= y && y < arrayMaxY;
}
public boolean is(int x, int y){
return x==y;
}
public boolean is(Object x, Object y){
return x.equals(y);
}
public <T,R> boolean is(T T, R R, BiPredicate<T,R> function){
return function.test(T,R);
}
public <T> void applyToArray(T[] array, Function<T,T> function){
for (int i = 0; i < array.length; i++) {
array[i] = function.apply(array[i]);
}
}
public void if_(BooleanSupplier b, Runnable consumer){
if(b.getAsBoolean()){
consumer.run();
}
}
//최대 값이 여러 개라면 첫 인덱스 반환
public int findMaxIdx(int[] array){
int max = Integer.MIN_VALUE;
int maxIdx = 0;
for (int i = 0; i<array.length; i++){
if (array[i] > max){
max = array[i];
maxIdx = i;
}
}
return maxIdx;
}
public int findMax(int[] array){
//Arrays.stream(array).max().getAsInt();
int max = Integer.MIN_VALUE;
for (int i = 0; i<array.length; i++){
if (array[i] > max){
max = array[i];
}
}
return max;
}
public int sumArray(int[][] array) {
int sum = 0;
for (int[] row : array) {
for (int value : row) {
sum += value;
}
}
return sum;
}
public int sumArray(int[] array) {
int sum = 0;
for (int value : array) {
sum += value;
}
return sum;
}
static class Point{
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void add(int x, int y) {
this.x += x;
this.y += y;
}
public double distanceTo(Point point) {
return Math.sqrt(Math.pow(this.x - point.x, 2) + Math.pow(this.y - point.y, 2));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return x == point.x && y == point.y;
}
}
static class Buf{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
public String read(){
while(st == null || !st.hasMoreTokens()){
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
return null;
}
}
return st.nextToken();
}
public Integer readInt(){
return Integer.parseInt(read());
}
public float readFloat(){
return Float.parseFloat(read());
}
public Double readDouble() {
return Double.parseDouble(read());
}
}
}
'백준' 카테고리의 다른 글
| 백준#1206 사람의 수(java) (0) | 2024.08.10 |
|---|---|
| 백준#1637 날카로운 눈 (java) (0) | 2024.03.22 |
| 백준#1477 휴게소 세우기 (java) (0) | 2024.03.20 |
| 백준#2887 행성터널 (java) (0) | 2024.03.13 |
| 백준#1185 유럽 여행 (Java) (0) | 2024.03.12 |