Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Stack
- buffer
- C programming
- s
- 메모리구조
- coding test
- Serialization
- 알기쉬운 알고리즘
- Graph
- JSON
- 이스케이프 문자
- list 컬렉션
- 윤성우의 열혈 자료구조
- insertion sort
- Algorithm
- Selection Sorting
- stream
- R
- datastructure
- C 언어 코딩 도장
- 혼자 공부하는 C언어
- 윤성우 열혈자료구조
- 이것이 자바다
Archives
- Today
- Total
Engineering Note
[BOJ:2259] 부등호 코드 FROM WHILE TO FOR CLEAN코드 본문
Programming Language/Clean_Code
[BOJ:2259] 부등호 코드 FROM WHILE TO FOR CLEAN코드
Software Engineer Kim 2021. 10. 30. 23:32#include <stdio.h>
int main()
{
int max[10], min[10], k, cur_max=9, cur_min=0;
char sign[12];
scanf("%d", &k);
for (int i = 0; i < k; i++)
scanf(" %c", &sign[i]);
int i = 0, count=0, sub=0;
// count: 현재 위치에서 '<'가 몇개 나오는지 개수
// sub: '<'가 처음 나오는 위치에서 count 값. count 값이 변화하기 때문에 새 변수로 저장. '<'가 끝나는 순간 cur_max에서 빼주기 위해 설정.
while (1)
{
if (sign[i] == '>')
{
max[i] = cur_max--;
if (sub)
{
cur_max -= sub;
sub = 0;
}
}
else if(sign[i]=='<')
{
if (count == 0)
{
int j = i;
while (sign[j] == '<')
{
j++;
count++;
}
sub = count;
max[i] = cur_max - count;
count--;
}
else
{
max[i] = cur_max - count;
count--;
}
}
else
{
max[i] = cur_max;
break;
}
i++;
}
for (int i = 0; i <= k; i++)
printf("%d", max[i]);
printf("\n");
int add=0;
i = 0, count=0;
while (1)
{
if (sign[i] == '<')
{
min[i] = cur_min++;
if (add)
{
cur_min += add;
add = 0;
}
}
else if (sign[i] == '>')
{
if (count == 0)
{
int j = i;
while (sign[j] == '>')
{
j++;
count++;
}
add = count;
min[i] = cur_min + count;
count--;
}
else
{
min[i] = cur_min + count;
count--;
}
}
else
{
min[i] = cur_min;
break;
}
i++;
}
for (int i = 0; i <= k; i++)
printf("%d", min[i]);
return 0;
}
위의 코드를 아래로 변경
#include <stdio.h>
int main() {
//freopen("input.txt", "rt", stdin);
int k, i =0,cnt = 0;
char curMax = '9', curMin = '0';
char str[11],max[11],min[11];
scanf("%d", &k);
for (int i = 0; i < k; ++i) {
scanf(" %c", &str[i]);
}
//부등호의 개수는 k개
//마지막 부등호배열의 인덱스 번호는 k-1
//마지막 숫자의 인덱스 번호는 k
//마지막 숫자 까지 입력하고 인덱스 i가 k+1이 되면 while문 종료
while (i != k+1) {
if (str[i] == '>') {
max[i++] = curMax--;
}
else if (str[i] == '<') {
for (int j = i; str[j] == '<'; ++j) {
++cnt;
}
int temp = cnt;
for (; cnt >= 0; --cnt) {
max[i++] = curMax - cnt;
}
cnt = 0;
curMax -= ++temp;
}
else //'>' 다음 str[i]가 널이라면 max[i]에 현재 최대값 넣고 종료
max[i++] = curMax;
}
max[i] = NULL;
i = 0;
while (i != k+1) {
if (str[i] == '<') {
min[i++] = curMin++;
}
else if (str[i] == '>') {
for (int j = i; str[j] == '>'; ++j) {
++cnt;
}
int temp = cnt;
for (; cnt >= 0; --cnt) {
min[i++] = curMin + cnt;
}
cnt = 0;
curMin += ++temp;
}
else //'>' 다음 str[i]가 널이라면 min[i]에 현재 최소값넣고 종료
min[i++] = curMin;
}
min[i] = NULL;
printf("%s\n%s",max,min);
return 0;
}
'Programming Language > Clean_Code' 카테고리의 다른 글
[Clean Code] 훌륭한 소프트웨어 기법 (0) | 2022.02.15 |
---|---|
[Short Code] 숫자를 바라보는 관점에 따른 알고리즘의 변화 (0) | 2022.01.23 |
if문 하나 if_else 문으로 가독성 증가 (0) | 2021.10.30 |
Comments