package javalesson.com.javalesson.ch01test;
public class T02RectBasic {
public static void main(String[] args) {
int array[][] = new int[5][5];
int row, col;
int i=1;
for(row=0;row<5;row++){
if(row%2==0){
for(col=0;col<5;col++){
array[row][col]=i++;
}
}else{
for(col=0;col<5;col++){
array[row][4-col]=i++;
}
}
}
//출력
for(row=0;row<5;row++){
for(col=0;col<5;col++){
System.out.printf("%3d",array[row][col]);
}
System.out.println();
}
}
}
package jungbo;
import java.util.InputMismatchException;
import java.util.Scanner;
public class T00Test{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int deg;
while(true){
System.out.print("차수를 입력하세요. : ");
try{
deg = scan.nextInt();
break;
}catch(InputMismatchException e){
System.out.println("숫자만 입력해야 합니다.");
}
}
scan.close();
int array[][] = new int[deg][deg];
int row, col;
int i=1;
for(row=0;row<deg;row++){
if(row%2==0){
for(col=0;col<deg;col++){
array[row][col]=i++;
}
}else{
for(col=0;col<deg;col++){
array[row][(deg-1)-col]=i++;
}
}
}
//출력
for(row=0;row<deg;row++){
for(col=0;col<deg;col++){
System.out.printf("%3d",array[row][col]);
}
System.out.println();
}
}
}
'JAVA > 알고리즘 예제' 카테고리의 다른 글
[JAVA] 삽입 정렬(Insertion Sort) (1) | 2016.09.01 |
---|---|
[JAVA] 순위(석차) 계산기 (1) | 2016.09.01 |
[JAVA] 버블 정렬(Bubble Sort) (1) | 2016.08.31 |
[JAVA] 선택 정렬(Selection Sort) (1) | 2016.08.31 |
[JAVA] 마방진 (1) | 2016.08.31 |