Doit! 알고리즘 (3)

업데이트:


chap01-12

package chap01;
public class Q12 {

	public static void main(String[] args) {
		System.out.print("   |");
		for (int i = 1; i <= 9; i++)
			System.out.printf("%3d", i);
		System.out.println("\n---+---------------------------");

		for (int i = 1; i <= 9; i++) {
			System.out.printf("%2d |", i);
			for (int j = 1; j <= 9; j++)
				System.out.printf("%3d", i * j);
			System.out.println();
		}
	}
}



chap01-13

package chap01;
public class Q13 {

	public static void main(String[] args) {
		System.out.print("   |");
		for (int i = 1; i <= 9; i++)
			System.out.printf("%3d", i);
		System.out.println("\n---+---------------------------");

		for (int i = 1; i <= 9; i++) {
			System.out.printf("%2d |", i);
			for (int j = 1; j <= 9; j++)
				System.out.printf("%3d", i + j);
			System.out.println();
		}
	}
}



chap01-14

package chap01;
import java.util.Scanner;

public class Q14 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("사각형을 출력합니다.");
		System.out.print("단 수 : ");
		int n = sc.nextInt();
		
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}



chap01-15

package chap01;
import java.util.Scanner;

public class Q15 {
	static void triangleLB(int n) {
		for(int i = 0; i < n; i++) {
			for(int j = 0; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
	
	static void triangleLU(int n) {
		for(int i = 0; i < n; i++) {
			for(int j = n; j > i; j--) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
	
	static void triangleRU(int n) {
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < i; j++) {
				System.out.print(" ");
			}
			for(int k = n; k > i; k--) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
	
	static void triangleRB(int n) {
		for(int i = 0; i < n; i++) {
			for(int j = i; j < n -1; j++) {
				System.out.print(" ");
			}
			for(int k = 0; k <= i; k++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("몇 단 삼각형? : ");
		int n = sc.nextInt();
		
		triangleLB(n);
		System.out.println("-------------------------");
		triangleLU(n);
		System.out.println("-------------------------");
		triangleRU(n);
		System.out.println("-------------------------");
		triangleRB(n);
	}
}



chap01-16

package chap01;
import java.util.Scanner;

public class Q16 {
	
	static void spira(int n) {
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n - i + 1; j++) {
				System.out.print(' ');
			}
			for(int k = 1; k  <= (i-1)*2+1; k++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("피라미드 단 수 입력 : ");
		int n = sc.nextInt();
		
		spira(n);
	}
}



chap01-17

package chap01;
import java.util.Scanner;

public class Q17 {
	
	static void npira(int n) {
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n - i + 1; j++) {
				System.out.print(' ');
			}
			for(int k = 1; k <= (i-1) * 2 + 1; k++) {
				System.out.print(i % 10);
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("피라미드 단 수 입력 : ");
		int n = sc.nextInt();
		
		npira(n);
	}
}



태그:

카테고리:

업데이트:

댓글남기기