2013년 11월 4일 월요일

[자바진법변환JAVA알고리즘]자바 10진수를 이진수로

[자바진법변환JAVA알고리즘]자바 10진수를 이진수로

10진수를 이진수로....

방법1 : 비 재귀방법
-------------------------------------------
//10진수를 이진수로...
class DecimalToBinary {
int[] binary(int value) {
int n, i;
int[] b = new int[64];
for(i=63; value>1; i--) {
//나머지
n = value % 2;
//몫
value = value / 2;
if (value < 2){
b[i] = n;
b[i-1] = value;
}
else {
b[i] = n;
}
}
b[0] = i;
return b;
}
public static void main(String[] args) {
long time = System.currentTimeMillis();
if (args.length<1) {
System.out.println("Usage : java DecimalToBinary number ");
System.exit(1);
}
        int value = Integer.parseInt(args[0]);
DecimalToBinary d = new DecimalToBinary();
int[] b = d.binary(value);
System.out.print("10진수 " + value + " 를 이진수로 바꾸면 --> ");
for(int j=b[0]; j < 64; j++) {
System.out.print(b[j]);
}
System.out.println("\n\n총 소요시간 : " + (System.currentTimeMillis()-time) + " millis");
Runtime r = Runtime.getRuntime();
long t = r.totalMemory();
long f = r.freeMemory();
System.out.println("총 메모리 : " + t + " bytes");
System.out.println("남은 메모리 : " + f  + "bytes");
System.out.println("");
}
}
방법2 : 비재귀 방법(비트연산 이용)
---------------------------------------
//10진수를 이진수로...
class DecimalToBinary1 {
int[] binary(int value) {
int i=63;
int[] b = new int[64];
while(value > 0){ //convert decimal to binary representation 
  if((value %2)!=0)  b[i]=1; 
  i--; 
  value>>=1;   //divide decimal by 2 

b[0] = i+1;
return b;
}
public static void main(String[] args) {
long time = System.currentTimeMillis();
if (args.length<1) {
System.out.println("Usage : java DecimalToBinary number ");
System.exit(1);
}
        int value = Integer.parseInt(args[0]);
DecimalToBinary1 d = new DecimalToBinary1();
int[] b = d.binary(value);
System.out.print("10진수 " + value + " 를 이진수로 바꾸면 --> ");
for(int j=b[0]; j < 64; j++) {
System.out.print(b[j]);
}
System.out.println("\n\n총 소요시간 : " + (System.currentTimeMillis()-time) + " millis");
Runtime r = Runtime.getRuntime();
long t = r.totalMemory();
long f = r.freeMemory();
System.out.println("총 메모리 : " + t + " bytes");
System.out.println("남은 메모리 : " + f  + "bytes");
System.out.println("");
}
}

방법3 : 재귀호출 이용
------------------------------------------------------
//10진수를 이진수로...
class DecimalToBinary2 {
int i=63;
static int[] b = new int[64];
void binary(int value) {
if (value > 1){
b[i] = value % 2;
i--;
binary(value/2);
}
else {
b[i] = value;
b[0] = i;
}
}
public static void main(String[] args) {
long time = System.currentTimeMillis();
if (args.length<1) {
System.out.println("Usage : java DecimalToBinary number ");
System.exit(1);
}
        int value = Integer.parseInt(args[0]);
DecimalToBinary2 d = new DecimalToBinary2();
d.binary(value);
System.out.print("10진수 " + value + " 를 이진수로 바꾸면 --> ");
for(int j=b[0]; j < 64; j++) {
System.out.print(b[j]);
}
System.out.println("\n\n총 소요시간 : " + (System.currentTimeMillis()-time) + " millis");
Runtime r = Runtime.getRuntime();
long t = r.totalMemory();
long f = r.freeMemory();
System.out.println("총 메모리 : " + t + " bytes");
System.out.println("남은 메모리 : " + f  + "bytes");
System.out.println("");
}

[개강임박강좌, 오프라인교육장에 오시면 보다 자세히 배울 수 있습니다.]

오라클자바커뮤니티에서 운영하는 개발자 전문교육 ,개인80%환급(
www.onjprogramming.co.kr)

[주간]
  [11/13]SQL초보에서실전전문가까지
  [11/13]안드로이드개발자과정
  [11/18]Spring3.X, MyBatis, Hibernate실무과정
  [11/18]iPhone 하이브리드 앱 개발 실무과정

[평일야간]
  [11/08]C#,ASP.NET마스터
  [11/08]Spring3.X, MyBatis, Hibernate실무과정
  [11/12]iPhone 하이브리드 앱 개발 실무과정
  [11/14]JAVA&WEB프레임워크실무과정

[주말]
  [11/09]C#,ASP.NET마스터
  [11/09]JAVA&WEB프레임워크실무과정
  [11/09]Spring3.X, MyBatis, Hibernate실무과정
  [11/09]웹퍼블리싱 마스터
  [11/16]PL/SQL,오라클힌트,SQL튜닝,사례연구
  [11/16]ASP.NET4.0 MVC 프로그래밍



Spring3.X, MyBatis, Hibernate실무과정 5일 35시간   11-18
Spring3.X, MyBatis, Hibernate실무과정 12일 36시간   11-08
JAVA&WEB프레임워크실무과정 33일 99시간   11-14
자바초보에서안드로이드까지 18일 54시간   11-15
Spring3.X, MyBatis, Hibernate실무과정 5일 35시간   11-09
JAVA&WEB프레임워크실무과정 14일 98시간   11-09






댓글 없음:

댓글 쓰기