본문 바로가기

Java 공부

Power Java Compact_3장_Programming Exercise_연습

src.zip
0.00MB

 

 

파워 자바 컴팩트 3장

Programming Exercise

p.108 ~ p.111 답

 

 

 

1. 1부터 100사이의 정수 중에서 3 또는 4의 배수의 합을 계산하는 프로그램을 작성하라.

 

public class threeandfour {
	public static void main(String[] ar) {
		int total = 0;

		for(int i = 1; i <= 100; i++){ // 1~100
			if(i%3 == 0 || i%4 == 0){
				total += i;
			}
		}
		System.out.println(total);
	}
}

 


 

2. 사용자가 입력한 값이 1, 2,... , 9이면 "ONE", "TWO",,... , "NINE"과 같이 출력하는 프로그램을 작성하라.

 

 

import java.util.*;

public class ProgrammingExercise32 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.print("정수를 입력하세요: ");
		short input = scan.nextShort();
		
		switch(input) {
			case 1: System.out.println("ONE");break;
			case 2: System.out.println("TWO");break;
			case 3: System.out.println("THREE");break;
			case 4: System.out.println("FOUR");break;
			case 5: System.out.println("FIVE");break;
			case 6: System.out.println("SIX");break;
			case 7: System.out.println("SEVEN");break;
			case 8: System.out.println("EIGHT");break;
			case 9: System.out.println("NINE");break;
			default:System.out.println("OTHER");break;
		}
	}
}

 


 

3. 2개의 주사위를 던지는 게임이 있다고 가정하자.

2개 주사위의 합이 6이 되는 경우는 몇 가지나 있을까?

합이 6이 되는 경우의 수를 출력하는 프로그램을 작성해보자.

 

public class ProgrammingExercise33 {
	public static void main(String[] ar) {
		for(int i = 1; i < 6; i++) {
			short j = 6;
			System.out.println("(" + i + "," + (j-i) + ")");
		}
	}
}

 

4. 사용자로부터 키를 입력받아 표준 체중을 계산한 후에 사용자의 체중과 비교하여

저체중인지, 표준인지, 과체중인지를 판단하는 프로그램을 작성하라.

표중 체중 계산식은 다음을 사용하라.

 

표준 체중 = (키 - 100) * 0.9

 

import java.util.*;

public class ProgrammingExercise34 {
	public static void main(String[] ar) {
		Scanner scan = new Scanner(System.in);
		
		System.out.print("키를 입력하세요: ");
		short 키 = scan.nextShort();
		
		System.out.print("몸무게를 입력하세요: ");
		short 몸무게 = scan.nextShort();
		
		double 표준체중 = (키 - 100) * 0.9;
		
		if(표준체중 < 몸무게) {
			System.out.print("과체중입니다.");
		}else {
			System.out.print("정상");
		}
		scan.close();
	}
}

 

5. 1차 방정식 3x + 10y = 100을 만족하는 모든 해를 구하는 프로그램을 작성해 보자.

여기서 x와 y는 정수이고

(0 <= x <= 10), (0 <= y <= 10) 이라고 가정하자. 

 

public class ProgrammingExercise35 {
	public static void main(String[] ar) {
		
		for(int x = 0; x <= 10; x++) {
			for(int y = 0; y <= 10; y++) {
				if((3*x)+(10*y) == 100) {
					System.out.print("("+ x + ", " + y + ")");
				}
			}
		}
	}
}

 


 

6. 2와 100 사이에 있는 모든 소수(Prime Number)를 찾는 프로그램을 작성하라.

주어진 정수 k를 2부터 k-1까지의 숫자로 나누어서 나머지가 0인 것이 하나라도 있으면 소수가 아니다.

 

public class ProgrammingExercise36 {
	public static void main(String[] ar) {
		boolean notsosu = false;
		
		for(int x = 2; x <= 100; x++) {
			for(int y = 2; y < x; y++) {
				if(x%y == 0) { // 소수가 아닐 경우
					notsosu = true;
					break;
				}
			}
			
			if(notsosu == false) {
				System.out.print(x + " ");
			}
			notsosu = false;
		
		}
	}
}

 

7. 피타고라스의 정리는 직각 삼각형에서 직각을 낀 두변의 길이를 a, b라고 하고,

빗변의 길이를 c 라고 하면 a^2+b^2=c^2의 수식이 성립한다는 것이다.

각 변의 길이가 100보다 작은 삼각형 중에서 피타고라스의 정리가 성립하는 직각 삼각형은 몇 개나 있을까?

3중 반복분을 이용하여 피타고라스의 정리를 만족하는 3개의 정수를 찾도록 한다.

public class ProgrammingExercise37 {
	public static void main(String[] ar) {
		
		for(int a = 0; a < 100; a++) {
			for(int b = 0; b < 100; b++) {
				for(int c = 0; c < 100; c++) {
					if((a*a) + (b*b) == (c*c) && (a*b*c > 0)) {
						System.out.println(a + " " + b + " " + c);
					}
				}
			}
		}
	}
}

 

8. 간단한 계산기 프로그램을 작성하여 보자.

먼저 사용자로부터 하나의 문자를 입력받는다.

이어서 사용자로부터 2개의 숫자를 입력받는다.

 

사용자로부터 받은 문자가 '+'이면 두 수의 덧셈을,

 

문자가 '-' 이면 뺄셈을,

 

문자가 '/' 이면 나눗셈을 수행하도록 작성하라.

 

(if-else문을 사용하라.

나눗셈의 경우, 분모가 0이 아닌지를 먼저 검사하여야 한다.)

 

import java.util.*;
public class ProgrammingExercise38 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		System.out.print("연산을 입력하세요: ");
		char 연산 = scan.nextLine().charAt(0);
		

		System.out.print("숫자 2개를 입력하세요: ");
		double input1 = scan.nextDouble();
		double input2 = scan.nextDouble();
		
		switch(연산) {
		case '+':
			System.out.println(input1 + "+" + input2 + "=" + (input1+input2));
			break;
		
		case '/':
			System.out.println(input1 + "/" + input2 + "=" + (input1/input2));
			break;
			
		case '*':
			System.out.println(input1 + "*" + input2 + "=" + (input1*input2));
			break;
			
		case '-':
			System.out.println(input1 + "-" + input2 + "=" + (input1-input2));
			break;
			
		case '%':
			System.out.println(input1 + "%" + input2 + "=" + (input1%input2));
			break;
			
		default:System.out.println("유효하지 않은 연산자입니다.");
			break;
		}
	}
}

 

9. 피보나치 수열 문제

 

import java.util.Scanner;

public class ProgrammingExercise39 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int i[] = {0, 1};
		
		System.out.print("출력할 항의 개수:");
		short select = scan.nextShort();
		
		for(int a = 0; a<select;a++) {
			System.out.print(i[0] + " ");
			int next = i[0] + i[1];
			i[0] = i[1];
			i[1] = next;
		}
	}
}

 

10. {1.0, 2.0, 3.0, 4.0}과 같은 초기값을 가지는 double형의 배열을 생성한다.

모든 배열 요소를 출력한 후에 모든 요소를 더하여 합을 출력하고

요소 중에서 가장 큰 값을 찾아서 출력하는 프로그램을 작성하라.

 

 

public class ProgrammingExercise310 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double array[] = {1.0, 2.0, 3.0, 4.0};
		double hap = 0;
		double max = 0;
		
		for(int i = 0; i < array.length; i++) {
			System.out.print(array[i] + " ");
			hap += array[i];
			if(max < array[i]) {max = array[i];}
		}
		System.out.println("\n합은: " + hap);
		System.out.println("최대값은: " + max);
	}
}

 

11. "Hello", "Java", "World"를 가지고 있는 문자열의 배열을 생성해 보자.

화면에 배열 요소를 출력하는 프로그램을 작성해본다.

 

public class ProgrammingExercise311 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String array[] = {"Hello", "Java", "World"};
		
		for(int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
	}
}

 

12. 학생들의 성적을 받아서 평균을 구하는 프로그램을 작성하라.

 

import java.util.*;
public class ProgrammingExercise312 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		short s[] = {0,0,0,0,0};
		double hap = 0;
		
		
		for(int i = 0; i<s.length;i++) { // 성적 입력
			System.out.print("성적를 입력하세요: ");
			s[i] = scan.nextShort();
		}
		
		
		
		for(int i = 0; i<s.length;i++) {
			hap += s[i];
		}

		double avg = (hap /(double) s.length);
		
		
		System.out.println("합계: " + hap);
		System.out.print("평균: " + avg);
		
	}
}

 

 

내용 끝.