일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 엔티티직접사용
- 제너릭
- joinfetch
- JPA
- fetchjoin
- 대량쿼리
- JPQL
- Generic
- javascriptcalendar
- jQuery값전송
- jscalendar
- jQueryUI
- 스프링데이터흐름
- JQuery
- 페이징
- 제네릭
- Hibernate
- 자바서블릿
- 벌크연산
- namedQuery
- springflow
- LIST
- jQuery값전달
- 프로젝트생성
- paging
- calendar
- 페치조인
- fullcalendar
- values()
- javaservlet
- Today
- Total
가자공부하러!
알고리즘(2) - 진법 변환, 최대최소 구하기 본문
목차
> 10진수를 입력 받아 2진수로 변환하는 코드를 작성하시오. 단, 1000 이하의 숫자를 입력받는다.
내 코드(C)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | int main() { int a[10], b, b2, c, qtt, rmd, i; printf("10진수 정수 입력 : "); scanf("%d", &b); printf("\n"); //줄 바꿈 b2 = b; //입력받은 값 별도 저장 c = 0; //배열 인덱스 사용을 위한 초기화 while (1) { qtt = b / 2; //몫 rmd = b - qtt * 2; //나머지 a[c] = rmd; //나머지를 배열에 순차적으로 저장 if (qtt == 0) { break; } b = qtt; //몫을 다음 반복연산에 사용하기 위해 b 값 변경 c++; } for (i = c; i >= 0; i--) { printf("a[]=%d i=%d \n",a[i],i); } } | cs |
*틀렸던 부분 :
교재 코드(C)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | int main() { int b, bb, c, mok, nmg, i; int a[10]; scanf("%d", &b); bb = b; c = -1; do { c++; mok = b / 2; nmg = b - mok * 2; a[c] = nmg; b = mok; } while (mok != 0); printf("%d ", bb); for (i = c; i >= 0; i--) { printf("%d ", a[i]); } } | cs |
입력값 : 8
출력값 : 1 0 0 0
> 10진수와 임의의 정수를 입력 받아 임의의 진수로 변환하는 코드를 작성하시오. 단, 1000 이하의 숫자를 입력받는다.
내 코드(java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package Test.java; import java.util.*; public class TestMain { public static void main(String[] args) { // TODO Auto-generated method stub int b,c,d,e,f; char a[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; Scanner scn = new Scanner(System.in); System.out.print("10진수 정수 입력 : "); b = scn.nextInt(); System.out.print("변환할 진수 입력 : "); c = scn.nextInt(); System.out.println(""); d=1; while(d<=b) d *= c; while(true) { if(d>1) d/=c; e=b/d; f=b-e*d; System.out.print(a[e]+" "); if(d!=1) b=f; else break; } System.out.printf("끝!"); } } | cs |
*틀렸던 부분 :
> 전혀 못 풀었음. 손도 못댔단다... 답보고 자바로 베껴씀
교재 코드(C)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | int main() { int b, c, d, e, f; char a[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; scanf("%d %d", &b, &c); d = 1; while (d <= c) { d *= b; } while (1) { if (d > 1) d /= b; e = c / d; f = c - e * d; printf("%c", a[e]); if (d != 1) { c = f; } else { break; } } } | cs |
2-3. 진법 변환(소수점 포함 2진수를 10진수로)
> 10자리로 구성된 2진수를 문자열로 입력받아 10진수로 변환.
> 5번째 자리까지는 소수 이상, 6번째 자리부터 소수이하.
내 코드(java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package Test.java; import java.util.*; import java.math.*; public class TestMain { public static void main(String[] args) { // TODO Auto-generated method stub double b=0,e; int c,d=0; String a = "a"; Scanner scn = new Scanner(System.in); System.out.print("2진수 10자리 입력(앞 5개는 소수 이상, 뒤 5개는 소수 이하) : "); a = scn.next(); System.out.println(a+""); //줄 바꿈 for(c=0;c<10;c++) { d=a.charAt(c)-'0'; e=Math.pow(2.0, (4-c))*d; b+=e; System.out.printf("d=%d e=%f b=%f \n",d,e,b); } System.out.println("2진수 "+a+"를 10진수로 변환한 값 = "+b); System.out.printf("끝!"); } } | cs |
*틀렸던 부분 :
> 19번 줄 a.charAt(c)-'0'을 (int)a.charAt(c)로 사용
교재 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | int main() { char a[11], munja[2]; double d, e; scanf("%s", a); double b = 0; int c = -1; munja[1] = '\0'; while (1) { c++; if (c <= 9) { munja[0] = a[c]; d = atoi(munja); e = d * pow(2, 4 - c); b += e; } else { printf("%8.5f", b); break; } } } | cs |
입력값 : 1010111011
출력값 : 21.84375
2-4. 최대값, 최소값 구하기
> 10개의 수치자료를 입력 받아 배열에 저장한 후 저장된 자료 중 가장 큰 값을 찾는 코드 작성
내 코드(java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package Test.java; import java.util.*; public class TestMain { public static void main(String[] args) { // TODO Auto-generated method stub int a[] = new int[10]; int i,max=0; Scanner scn = new Scanner(System.in); for(i=0; i<10; i++) { //배열 초기값을 입력받는 반복문 System.out.print((i+1)+"번 째 정수 입력 : "); a[i] = scn.nextInt(); } for(i=0; i<10; i++){ //max값과 배열내용을 순 비교 if(a[i] > max) max=a[i]; } System.out.printf("입력한 값 중 가장 큰 수 : "+max); System.out.println(" "); //줄바꿈 System.out.printf("끝!"); System.out.println(" "); //줄바꿈 } } | cs |
*틀렸던 부분 :
> 배열 선언 잘못함(int a[10])
> 스캐너 부분 손코딩 할 때 System.in 인수 안씀
교재 코드(C)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include "pch.h" #include <iostream> #include <stdio.h> #define _CRT_SECURE_NO_WARNINGS // scanf 보안 경고로 인한 컴파일 에러 방지 int main() { int i; int a[10]; int j = -1, max = 0; do { j++; scanf("%d", &a[j]); } while (j < 9); for (i = 0; i <= 9; i++) { if (a[i] > max) max = a[i]; } printf("%d", max); } | cs |
입력값 : 0 1 2 3 4 5 6 7 8 9
출력값 : 9
2-5. 최대값, 최소을 제외한 평점의 평균 구하기
> 7명의 채점 점수 중에서 최하위 점수와 최상위 점수를 제외한 5명 점수의 평균을 구하는 코드 작성. 단, 채점점수는 배열에 저장
내 코드(java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package Test.java; import java.util.*; import java.math.*; public class TestMain { public static void main(String[] args) { // TODO Auto-generated method stub double a[] = new double[7]; int i; double max=0,min=5,avr=0; Scanner scn = new Scanner(System.in); for(i=0; i<7; i++) { //배열 초기값을 입력받는 반복문 System.out.print((i+1)+"번 째 학점 입력 : "); a[i] = scn.nextDouble(); } for(i=0; i<7; i++){ if(a[i] > max) max=a[i]; } for(i=0; i<7; i++){ if(a[i] < min) min=a[i]; } for(i=0;i<7;i++) { avr+=a[i]; } avr=(avr-max-min)/(double)(a.length-2); System.out.println("입력한 값 중 가장 큰 수 : "+max); System.out.println("입력한 값 중 가장 작은 수 : "+min); System.out.println("입력한 값에서 최대값과 최소값을 제외한 값의 평균 : "+avr); System.out.printf("끝!"); System.out.println(" "); //줄바꿈 } } | cs |
*틀렸던 부분 :
교재 코드(C)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include "pch.h" #include <iostream> #include <stdio.h> #include <math.h> #define _CRT_SECURE_NO_WARNINGS int main(){ int m, min, max, hap, avg, i; int a[7]; m = -1; do { m++; scanf("%d", &a[m]); } while (m < 6); min = a[0]; max = a[0]; hap = a[0]; i = 0; while (i < 6) { i++; hap += a[i]; if (a[i] < min) min = a[i]; if (a[i] > max) max = a[i]; } hap = hap - min - max; avg = hap / 5; printf("%d %d %d %d", min, max, hap, avg); } | cs |
입력값 : 3.0 3.0 3.0 3.0 3.0 4.5 2.0
출력값 : 4.5 2.0 3.0
입력값 : 50 50 50 50 50 40 60
출력값 : 40 60 250 50
'공부 > 정보처리기사(실기)' 카테고리의 다른 글
알고리즘(6) - 배열응용 (0) | 2019.04.02 |
---|---|
알고리즘(5) - 병합, 스택, 배열 (0) | 2019.04.02 |
알고리즘(4) - 정렬 (0) | 2019.03.28 |
알고리즘(3) - 배수,보수,그레이코드 등 (0) | 2019.03.28 |
알고리즘(1) - 소수, 약수, 소인수분해 등 (0) | 2019.03.27 |