PS/code-up

audio 파일 용량 계산

easy16 2022. 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