반응형
문제 출처 :
https://www.acmicpc.net/problem/2999
알고리즘 분석 :
문제 해결에 필요한 사항
1. string
2. 구현
구현을 어떻게 하냐에 따라 풀이가 여러가지로 나뉠 수 있는 문제인 듯 하다.
이 문제는 결국 최대 r과 그에맞는 c를 찾아 그 행렬에 맞게 입력을 재정렬 해주면 된다.
필자같은 경우 r과 c를 구한 후, ans라는 string에 나머지연산을 이용하여 재정렬 하는 방식을 이용했다.
즉, r로 나눈 나머지를 이용하여 세로로 하나씩 추가했다고 생각할 수 있다.
소스 코드 :
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 | #include <iostream> #include <cstdio> #include <string> using namespace std; int main() { string str; cin >> str; int len = str.size(); int r, c; for (int i = 1; i <= len; i++) if (len % i == 0 && i <= len / i) r = i, c = len / i; string ans[10] = { "" }; for (int i = 0; i < len; i++) ans[i % r] += str[i]; for(int i = 0; i < r; i ++) cout << ans[i]; return 0; } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 알고리즘 문제풀이' 카테고리의 다른 글
[2548번] 대표 자연수 (0) | 2017.07.28 |
---|---|
[4198번] 열차정렬 (0) | 2017.07.25 |
[3977번] 축구 전술 (0) | 2017.07.22 |
[4196번] 도미노 (0) | 2017.07.22 |
[6543번] The Bottom of a Graph (0) | 2017.07.22 |