'PS/code-up'에 해당되는 글 6건

  1. 2022.04.22 성실한 개미
  2. 2022.04.22 설탕과자뽑기(반복문 인덱스 계산)
  3. 2022.04.22 함께 문제 푸는 날
  4. 2022.04.22 수열 3
  5. 2022.04.22 그림파일 용량 계산
  6. 2022.04.22 audio 파일 용량 계산
PS/code-up2022. 4. 22. 23:59

 

 

ex1) 무식하게 풀기

import java.util.Scanner;

class Main{
	
	
	
	public static void main(String args[]) {
		
		Scanner sc = new Scanner(System.in);
		int [] dx = { 0, 1 };
		int [] dy = { 1, 0 }; 
		

		boolean flag = false;
		boolean isBlocked = false;
		
		int [][]arr = new int[11][11];
		
		for ( int i = 1 ; i <= 10; ++i)
			for ( int j = 1 ; j <= 10; ++j)
				arr[i][j]=sc.nextInt();
		int x=2;
		int y=2;
		if (arr[2][2] == 2)
			flag = true;
	
		arr[2][2]=9;
		
		while (!flag && !isBlocked) {
			for ( int i = 0 ; i < 2 ; ++i) {
				int nx = x + dx[i];
				int ny = y + dy[i];
				
				if( arr[nx][ny] != 1) {
					if (arr[nx][ny] == 2 ) flag=true;						
					arr[nx][ny] = 9;
					x=nx;
					y=ny;
					break;
				} 
				
				if (  i==1 && arr[nx][ny] == 1 )
					isBlocked=true;
					
			}
			
		}
		
		
		for (int i = 1 ;  i <= 10; ++i) {
			for ( int j = 1; j <= 10; ++j)
				System.out.print(arr[i][j] +  " ");
			System.out.println();
		}
		
		
		
	}
}

 

ex2) dfs 길찾기

import java.util.Scanner;

class Main{
	
	
	static int [] dx = { 0, 1 };
	static int [] dy = { 1, 0 }; 
	static int [][]arr = new int[11][11];
	public static void main(String args[]) {
		
		Scanner sc = new Scanner(System.in);
		
		
		Main M = new Main();

		
		
		
		
		for ( int i = 1 ; i <= 10; ++i)
			for ( int j = 1 ; j <= 10; ++j)
				arr[i][j]=sc.nextInt();
		int x=2;
		int y=2;
	
		
		M.dfs(2, 2);
		
		
		for (int i = 1 ;  i <= 10; ++i) {
			for ( int j = 1; j <= 10; ++j)
				System.out.print(arr[i][j] +  " ");
			System.out.println();
		}
		
		
		
	}

	private void dfs(int x, int y) {
		
		if( arr[x][y] == 2 ) {
			arr[x][y]=9;
			return;			
		}
		if ( arr[x][y] == 1) {
			return;
		} else {
				arr[x][y] = 9;
				if ( arr[x][y+1] != 1 )
					dfs(x, y+1);
				else
					dfs(x+1, y);
		}
			
		
	}
}

출처 : https://codeup.kr/problem.php?id=1099&rid=0 

 

'PS > code-up' 카테고리의 다른 글

설탕과자뽑기(반복문 인덱스 계산)  (0) 2022.04.22
함께 문제 푸는 날  (0) 2022.04.22
수열 3  (0) 2022.04.22
그림파일 용량 계산  (0) 2022.04.22
audio 파일 용량 계산  (0) 2022.04.22
Posted by easy16
PS/code-up2022. 4. 22. 23:21

 

아래 문제를 통해 반복문 내 인덱스 계산에 따른 성능 향상을 위한 기법을 엿볼 수 있다.

 

배열 index안의 내용을 조작하지 않고, 인덱스의 기본위상(초기값)을 설정하면 성능상의 이점이 있다.

ex1)의 경우 매번 index를 얻기 위해 덧셈 연산을 하지만

ex2)의 경우 덧셈 연산 없이 index를 바로 참조가능.

import java.util.Scanner;

class Main{
	
	public static void main(String args[]) {
		
		Scanner sc = new Scanner(System.in);
		
		
		
		int h = sc.nextInt();
		int w = sc.nextInt();
		int [][]arr = new int[h+1][w+1];
		int n = sc.nextInt();
		
		
		for ( int i = 0 ; i < n ; ++i) {
			int l = sc.nextInt();
			int d = sc.nextInt();
			int x = sc.nextInt();
			int y = sc.nextInt();
			
			//ex1)
			/*
			for ( int j = 0 ; j < l ; ++j ) {
				if( d == 0) {
					arr[x][y+j]=1;
				} else {
					arr[x+j][y]=1;
				}
					
			}
			*/
			//ex2)	
			if( d == 0) {
				for ( int j = y ; j < y+l ; ++j ) 
					arr[x][j]=1;
			} else {
				for ( int j = x ; j < x+l ; ++j ) 
					arr[j][y]=1;
			}
					
			
			
			
		}
		
		
		for (int i = 1 ;  i <= h; ++i) {
			for ( int j = 1; j <= w; ++j)
				System.out.print(arr[i][j] +  " ");
			System.out.println();
		}
		
		
		
	}
}

출처 : https://codeup.kr/problem.php?id=1098&rid=0 

'PS > code-up' 카테고리의 다른 글

성실한 개미  (0) 2022.04.22
함께 문제 푸는 날  (0) 2022.04.22
수열 3  (0) 2022.04.22
그림파일 용량 계산  (0) 2022.04.22
audio 파일 용량 계산  (0) 2022.04.22
Posted by easy16
PS/code-up2022. 4. 22. 22:19

최소공배수를 계산하지 않고도 루프를 통해 최소공배수를 구할 수 있다.

ex) 3, 6, 7

반복 연산이 빠른 컴퓨터의 특징을 이용.

import java.util.Scanner;

class Main{
	
	public static void main(String args[]) {
		
		Scanner sc = new Scanner(System.in);
	
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int day = 1;
		
		while( day%a != 0 ||day%b != 0 ||day%c != 0 ) day++;
		
		System.out.println(day);
		
	}
}

ex)
3 7 9
63

'PS > code-up' 카테고리의 다른 글

성실한 개미  (0) 2022.04.22
설탕과자뽑기(반복문 인덱스 계산)  (0) 2022.04.22
수열 3  (0) 2022.04.22
그림파일 용량 계산  (0) 2022.04.22
audio 파일 용량 계산  (0) 2022.04.22
Posted by easy16
PS/code-up2022. 4. 22. 22:05

 

Stack을 사용할 필요가 없는데.. 

 

import java.util.Scanner;
import java.util.Stack;

class Main{
	
	public static void main(String args[]) {
		
		Scanner sc = new Scanner(System.in);
		Stack<Long> s = new Stack<Long>();
		int a = sc.nextInt(); //시작
		int m = sc.nextInt();//곱할값
		int d= sc.nextInt();//더할값
		int n = sc.nextInt();//몇번째
		
		s.push((long)a);
		for ( int i = 0 ; i < n-1; i++) {
			long top = s.pop();
			s.push((long) top*m+d);			
		}
		
		long res = s.pop();
		System.out.println(res);
			
	}
}

정확한 풀이

import java.util.Scanner;

class Main {

	public static void main(String args[]) {

		Scanner sc = new Scanner(System.in);		
		int a = sc.nextInt(); // 시작
		int m = sc.nextInt();// 곱할값
		int d = sc.nextInt();// 더할값
		int n = sc.nextInt();// 몇번째		
		long i, j =0;
		for (i = a, j = 1; j <= n; i = (i * m) + d, j++)
			if (j == n) break;
			

		System.out.println(i);

	}
}

출처 : https://codeup.kr/problem.php?id=1091&rid=0 

 

'PS > code-up' 카테고리의 다른 글

성실한 개미  (0) 2022.04.22
설탕과자뽑기(반복문 인덱스 계산)  (0) 2022.04.22
함께 문제 푸는 날  (0) 2022.04.22
그림파일 용량 계산  (0) 2022.04.22
audio 파일 용량 계산  (0) 2022.04.22
Posted by easy16
PS/code-up2022. 4. 22. 21:08

 

bmp 용량 = width*height*bits(rgb)

bits : rgb(8bit + 8bit + 8bit)

소수점 2자리 까지 반올림 (단위 : MB)

import java.util.Scanner;

class Main{
	
	public static void main(String args[]) {
		
		Scanner sc = new Scanner(System.in);
		
		int w=sc.nextInt();
		int h=sc.nextInt();
		int b=sc.nextInt();
		
		float size =(float)Math.round((float)( w*h*b)/8/1024/1024 *1000 )/1000;
		
		System.out.println(String.format("%.2f MB", size));
		
	}
}

ex)
100 100 4
0.00 MB

 

정답 : 포멧스트링을 통해 자동 반올림을 사용

import java.util.Scanner;

class Main{
	
	public static void main(String args[]) {
		
		Scanner sc = new Scanner(System.in);
		
		int w=sc.nextInt();
		int h=sc.nextInt();
		int b=sc.nextInt();
		
		//float size =(float)Math.round((float)( w*h*b)/8/1024/1024 *1000 )/1000;
		
		System.out.println(String.format("%.2f MB", (float)( w*h*b)/8/1024/1024));
		
	}
}

 

 

출처 : https://codeup.kr/problem.php?id=1086&rid=0 

'PS > code-up' 카테고리의 다른 글

성실한 개미  (0) 2022.04.22
설탕과자뽑기(반복문 인덱스 계산)  (0) 2022.04.22
함께 문제 푸는 날  (0) 2022.04.22
수열 3  (0) 2022.04.22
audio 파일 용량 계산  (0) 2022.04.22
Posted by easy16
PS/code-up2022. 4. 22. 20:50

 

PCM 파일 용량 계산 :

freq*resolution*channel*duration (단위 :bit)

 

freq : (44.1kHz)

channel : (mono 1 , stereo 2)

resolution : (일반적으로 8bit or 16bit)

 

계산시 값이 커질 수 있으므로 long을 사용.

import java.util.Scanner;

class Main{
	
	public static void main(String args[]) {
		
		Scanner sc = new Scanner(System.in);
	
		int hz  =sc.nextInt();
		int res =sc.nextInt();
		int chan = sc.nextInt();
		int duration = sc.nextInt();
		float size =(float)Math.round((float)((long)hz*res*chan*duration)/8/1024/1024 *10 )/10;
		System.out.println(String.format("%.1f MB", size));
		
	}
}

ex)
48000 32 5 300
2747.0
274.7 MB

정답 : 포멧스트링을 사용하여 반올림 처리

import java.util.Scanner;

class Main{
	
	public static void main(String args[]) {
		
		Scanner sc = new Scanner(System.in);
	
		int hz  =sc.nextInt();
		int res =sc.nextInt();
		int chan = sc.nextInt();
		int duration = sc.nextInt();
		//float size =(float)Math.round((float)((long)hz*res*chan*duration)/8/1024/1024 *10 )/10;
		//System.out.println((float)Math.round((float)((long)hz*res*chan*duration)/8/1024/1024 *10 ));
		System.out.println(String.format("%.1f MB", (float)((long)hz*res*chan*duration)/8/1024/1024));
		
	}
}

 

 

출처 : https://codeup.kr/problem.php?id=1085 

'PS > code-up' 카테고리의 다른 글

성실한 개미  (0) 2022.04.22
설탕과자뽑기(반복문 인덱스 계산)  (0) 2022.04.22
함께 문제 푸는 날  (0) 2022.04.22
수열 3  (0) 2022.04.22
그림파일 용량 계산  (0) 2022.04.22
Posted by easy16