음준희 블로그
백준 1022 : 소용돌이 예쁘게 출력하기 (C++) 본문
https://www.acmicpc.net/problem/1022
1022번: 소용돌이 예쁘게 출력하기
첫째 줄에 네 정수 r1, c1, r2, c2가 주어진다.
www.acmicpc.net
틀린이유 : 아무생각없이 10^8크기의 배열을 만들어 값을 채워버림
로직
규칙을 찾아 각 좌표에 맞는 값을 얻는 함수를 구현한다.
(0,0)=1^2
(1,1)=3^2
(2,2)=5^2
(n,n)=(1+n*2)^2
임을 이용한다.
범위중 가장 큰 수를 찾아 자리수를 고정하여 출력한다.
#include<bits/stdc++.h>
#include<iostream>
#define MAX 1000000007
#define INF 2000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int > pii;
typedef pair<long long, long long> pll;
typedef pair<double, double> pdd;
int dx[8] = { 0, -1, 0, 1, 0, 0, -1, -1 };
int dy[8] = { -1, 0, 1, 0, -1, -1, 0, 0 };
/*
int dx[8] = { -1, -1, 1, 1, -2, -2, 2, 2 };
int dy[8] = { 2, -2, 2, -2, -1, 1, -1, 1 };
*/
void init() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
}
ll gcd(ll a, ll b) { for (; b; a %= b, swap(a, b)); return a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int getValue(int x, int y) {
int k = max(abs(x), abs(y));
int n = 1 + 2 * k;
if (k == x) {
return n * n - (k - y);
}
else if (k == -y) {
return n * n - (n - 1) - (k - x);
}
else if (k == -x) {
return n * n - (n - 1) * 2 - (k + y);
}
else if (k == y) {
return n * n - (n - 1) * 3 - (k + x);
}
}
int main() {
init();
int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
int mx = max(getValue(x1, y1), max(getValue(x1, y2), max(getValue(x2, y1), getValue(x2, y2))));
int cnt = 0;
while (mx != 0) {
cnt++;
mx /= 10;
}
cout.width(cnt);
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
cout.width(cnt);
cout << getValue(i, j) << " ";
}
cout << "\n";
}
}
'알고리즘 > 백준' 카테고리의 다른 글
| 백준 8916 : 이진 검색 트리 c++ (1) | 2022.12.12 |
|---|---|
| 백준 1933 : 스카이라인 c++ (0) | 2022.12.11 |
| 백준 2166 : 다각형의 면적 (C++) (0) | 2022.04.07 |
| 백준 2162 : 선분그룹 (C++) (0) | 2022.04.07 |
| 백준 6549 : 히스토그램에서 가장 큰 직사각형 (C++) (0) | 2022.04.05 |