10번
입력 받은 정수로 배열을 만든뒤 배열의 값을 스택에 넣고 모두 pop
int main(void) {
StackType s;
init(&s);
int num, i , inp;
scanf("%d",&num);
for(i=0;i<num;i++){
scanf("%d",&inp);
push(&s,inp);
}
while(!is_empty(&s)){
printf("%d ",pop(&s));
}
return 0;
}
11번
문제오류가 있는듯 함. 두번째 케이스인 (((((() 일때는 1 2 3 4 5 6 6이 출력
cnt 변수를 이용하여 '(' 일때는 스택에 넣고 출력 후 cnt++ , ')'일때는 스택 pop한걸 출력
void num_ch(char exp[]){
StackType s;
init(&s);
int i, ch;
int cnt = 1;
int len = strlen(exp);
for (i=0; i<len;i++){
ch = exp[i];
if (ch == '('){
push(&s,cnt);
printf("%d ",cnt);
cnt++;
}
else{
printf("%d ",pop(&s));
}
}
}
int main(void) {
char p[SIZE];
scanf("%s",p);
num_ch(p);
return 0;
}
12번
count 변수를 이용해 문자 빈도수 측정
문자를 확인하고 스택이 비었거나 스택 맨 위의 문자가 넣으려는 문자와 같다면 스택에 넣은 후 count += 1
스택 맨위의 문자와 다르다면 스택의 문자들을 다 pop 한뒤 빈도수 count와 pop한 문자 출력
루프문이 끝나면 남아있는 스택의 문자와 빈도수 출력
void run_length(char exp[]){
StackType s;
init(&s);
int i, ch ;
char result;
int count = 0 ;
int len = strlen(exp);
for(i=0;i<len;i++){
ch = tolower(exp[i]);
if (is_empty(&s) || peek(&s)==ch){
push(&s,ch);
count += 1;
}
else{
while(!is_empty(&s)){
result = pop(&s);
}
printf("%d%c",count,result);
push(&s,ch);
count = 1;
}
}
printf("%d%c",count,pop(&s));
}
int main(void) {
char p[SIZE];
scanf("%s",p);
run_length(p);
return 0;
}
13번
입력 : 122233
출력 : 123
스택 2개(s1,s2) 를 이용하여 풀이함
문자를 하나씩 스택(s1)에 넣는데 ,
스택이 비어있지 않고 스택 맨위의 문자가 현재 넣으려는 문자와 같다면 (중복이라면)넣지않음(continue)
맨위의 문자가 현재 넣으려는 문자와 다르면 push
이후 s1의 문자열 (321) 들을 다시 정순으로 출력하기위해 s2에 s1의 원소들을 push
s2의 원소들을 모두 pop 해주면 123이 출력이 됨
void remove_duplication(char exp[]){
StackType s1,s2;
init(&s1);
init(&s2);
int i, ch ;
int len = strlen(exp);
for(i=0;i<len;i++){
ch = exp[i];
if(!is_empty(&s1) and peek(&s1) == ch){
continue;
}
else{
push(&s1,ch);
}
}
while(!is_empty(&s1)){
push(&s2,pop(&s1));
}
while(!is_empty(&s2)){
printf("%c",pop(&s2));
}
}
int main(void) {
char p[SIZE];
scanf("%s",p);
remove_duplication(p);
return 0;
}
14번
스택 사이즈 확인을 위해 스택의 모든 원소들을 하나씩 pop하면서 count+=1
int count_size(StackType *s){
if (is_empty(s)){
return 0;
}
else{
int count = 0;
while(!is_empty(s)){
pop(s);
count += 1;
}
return count;
}
}
int main(void) {
StackType s;
init(&s);
push(&s,1);
push(&s,2);
push(&s,3);
printf("%d",count_size(&s));
return 0;
}
16번
isalpha() = 대상문자가 알파벳인지 검사
isspace() = 대상문자가 공백인지 검사
입력받은 문자열을 스택에 넣고 , 스택에서 pop한 것과 문자열의 첫번째 문자부터 검사해서
하나라도 틀리면 회문이 아님->return 0
모두 같으면 회문 -> return 1
int pelindrome(char exp[]){
StackType s1;
init(&s1);
int i ,j, ch;
int len = strlen(exp);
for(i=0;i<len;i++){
ch = exp[i];
if (isalpha(ch) || isspace(ch) ){ //isspace로 공백처리
push(&s1,ch);
}
}
j = 0;
while(exp[j]!=NULL){
if(isalpha(exp[j] || isspace(exp[j]) )){
if(exp[j] != pop(&s1)){
return 0;
}
}
j++;
}
return 1;
}
int main(void) {
char p[SIZE];
printf("문자열을 입력하시오 : ");
scanf("%s",p);
if (pelindrome(p)){
printf("회문입니다.");
}
else{
printf("회문이 아닙니다.");
}
return 0;
}
'CS' 카테고리의 다른 글
[C언어로 쉽게 풀어 쓴 자료구조] 연습문제 5장 - 큐(소스코드 9~11) (1) | 2023.12.05 |
---|
10번
입력 받은 정수로 배열을 만든뒤 배열의 값을 스택에 넣고 모두 pop
int main(void) { StackType s; init(&s); int num, i , inp; scanf("%d",&num); for(i=0;i<num;i++){ scanf("%d",&inp); push(&s,inp); } while(!is_empty(&s)){ printf("%d ",pop(&s)); } return 0; }
11번
문제오류가 있는듯 함. 두번째 케이스인 (((((() 일때는 1 2 3 4 5 6 6이 출력
cnt 변수를 이용하여 '(' 일때는 스택에 넣고 출력 후 cnt++ , ')'일때는 스택 pop한걸 출력
void num_ch(char exp[]){ StackType s; init(&s); int i, ch; int cnt = 1; int len = strlen(exp); for (i=0; i<len;i++){ ch = exp[i]; if (ch == '('){ push(&s,cnt); printf("%d ",cnt); cnt++; } else{ printf("%d ",pop(&s)); } } } int main(void) { char p[SIZE]; scanf("%s",p); num_ch(p); return 0; }
12번
count 변수를 이용해 문자 빈도수 측정
문자를 확인하고 스택이 비었거나 스택 맨 위의 문자가 넣으려는 문자와 같다면 스택에 넣은 후 count += 1
스택 맨위의 문자와 다르다면 스택의 문자들을 다 pop 한뒤 빈도수 count와 pop한 문자 출력
루프문이 끝나면 남아있는 스택의 문자와 빈도수 출력
void run_length(char exp[]){ StackType s; init(&s); int i, ch ; char result; int count = 0 ; int len = strlen(exp); for(i=0;i<len;i++){ ch = tolower(exp[i]); if (is_empty(&s) || peek(&s)==ch){ push(&s,ch); count += 1; } else{ while(!is_empty(&s)){ result = pop(&s); } printf("%d%c",count,result); push(&s,ch); count = 1; } } printf("%d%c",count,pop(&s)); } int main(void) { char p[SIZE]; scanf("%s",p); run_length(p); return 0; }
13번
입력 : 122233
출력 : 123
스택 2개(s1,s2) 를 이용하여 풀이함
문자를 하나씩 스택(s1)에 넣는데 ,
스택이 비어있지 않고 스택 맨위의 문자가 현재 넣으려는 문자와 같다면 (중복이라면)넣지않음(continue)
맨위의 문자가 현재 넣으려는 문자와 다르면 push
이후 s1의 문자열 (321) 들을 다시 정순으로 출력하기위해 s2에 s1의 원소들을 push
s2의 원소들을 모두 pop 해주면 123이 출력이 됨
void remove_duplication(char exp[]){ StackType s1,s2; init(&s1); init(&s2); int i, ch ; int len = strlen(exp); for(i=0;i<len;i++){ ch = exp[i]; if(!is_empty(&s1) and peek(&s1) == ch){ continue; } else{ push(&s1,ch); } } while(!is_empty(&s1)){ push(&s2,pop(&s1)); } while(!is_empty(&s2)){ printf("%c",pop(&s2)); } } int main(void) { char p[SIZE]; scanf("%s",p); remove_duplication(p); return 0; }
14번
스택 사이즈 확인을 위해 스택의 모든 원소들을 하나씩 pop하면서 count+=1
int count_size(StackType *s){ if (is_empty(s)){ return 0; } else{ int count = 0; while(!is_empty(s)){ pop(s); count += 1; } return count; } } int main(void) { StackType s; init(&s); push(&s,1); push(&s,2); push(&s,3); printf("%d",count_size(&s)); return 0; }
16번
isalpha() = 대상문자가 알파벳인지 검사
isspace() = 대상문자가 공백인지 검사
입력받은 문자열을 스택에 넣고 , 스택에서 pop한 것과 문자열의 첫번째 문자부터 검사해서
하나라도 틀리면 회문이 아님->return 0
모두 같으면 회문 -> return 1
int pelindrome(char exp[]){ StackType s1; init(&s1); int i ,j, ch; int len = strlen(exp); for(i=0;i<len;i++){ ch = exp[i]; if (isalpha(ch) || isspace(ch) ){ //isspace로 공백처리 push(&s1,ch); } } j = 0; while(exp[j]!=NULL){ if(isalpha(exp[j] || isspace(exp[j]) )){ if(exp[j] != pop(&s1)){ return 0; } } j++; } return 1; } int main(void) { char p[SIZE]; printf("문자열을 입력하시오 : "); scanf("%s",p); if (pelindrome(p)){ printf("회문입니다."); } else{ printf("회문이 아닙니다."); } return 0; }
'CS' 카테고리의 다른 글
[C언어로 쉽게 풀어 쓴 자료구조] 연습문제 5장 - 큐(소스코드 9~11) (1) | 2023.12.05 |
---|