알고리즘 풀이/백준

[백준][Java] 1022번 소용돌이 예쁘게 출력하기

배게 2018. 5. 10. 12:55
728x90

코드가 너무 더럽습니다.


첫번째 제출은 예쁘게 출력하기라는 뜻을 모른 상태로

숫자에 공백을 안 넣어서 틀렸습니다.


 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
	static int max_unit=0;
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int x1,y1,x2,y2;
		StringTokenizer st = new StringTokenizer(br.readLine());
		x1=Integer.parseInt(st.nextToken());
		y1=Integer.parseInt(st.nextToken());
		x2=Integer.parseInt(st.nextToken());
		y2=Integer.parseInt(st.nextToken());
		
		int max;
		
		if(Integer.parseInt(CoodVal(x1,y1))>Integer.parseInt(CoodVal(x2,y2))) max= Integer.parseInt(CoodVal(x1,y1));
		else max=Integer.parseInt(CoodVal(x2,y2));
		
		while(true) {
			max/=10;
			max_unit++;
			if(max==0) break;
		}
		
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		for(int i=x1; i<=x2; i++) {
			String oneL=CoodVal(i,y1)+" ";
			for(int j=y1+1; j<=y2; j++) {
				oneL+=CoodVal(i,j)+" ";
			}
			bw.write(oneL);
			bw.newLine();
		}
		bw.flush();
		bw.close();
	}
	
	private static String CoodVal(int a, int b) {
		int max=0;
		if(Math.abs((int)a)>Math.abs((int)b)) max = Math.abs((int)a);
		else max=Math.abs((int)b);
		
		int max_cood=max;	// 좌표
		max = 2 * max + 1;

		int n = max; // n*n  길이 (크기)
		max = (int)Math.pow((double)max, (double)2);
		
		int start = max;		// Max value in this stage
		int res;
		
		if(a==max_cood && b>-max_cood) {
			res= start-(max_cood-b);
		}
		else if(b==-max_cood && a>-max_cood) {
			start-=(n-1);
			res= start-(max_cood-a);
		}
		else if(a==-max_cood && b<max_cood) {
			start-=2*(n-1);
			res= start-(max_cood+b);
		}
		else{
			start-=3*(n-1);
			res= start-(max_cood+a);
		}
		int unit=0,temp=res;
		while(true) {
			temp/=10;
			unit++;
			if(temp==0) break;
		}
		String res_st=Integer.toString(res);
		for(int i=0; i<max_unit-unit; i++) {
			res_st=" "+res_st;
		}
		
		return res_st;
	}
}