회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
www
jr313jjr
2년 전
0
save code save code save code save code save code
#include <stdio.h> #include <string.h> #define MAXLINES 100 char *lineptr[MAXLINES]; #define NUM 1 #define RE 2 #define FOLD 4 #define DIC 8 static int option = 0; int readlines(char *lineptr[], int); void writelines(char *lineptr[], int); void sort(void *lineptr[], int, int, int (*comp)(void *, void *)); int numcmp(char *, char *); int ccmp(char *, char *); /* FUNCTION MAIN */ int main(int argc, char *argv[]) { int nlines, c; while(--argc > 0 && (*++argv)[0] == '-') while(c = *++argv[0]) switch (c) { case 'n': option |= NUM; break; case 'r': option |= RE; break; case 'f': option |= FOLD; break; case 'd': option |= DIC; break; default: printf("Usage: Wrong command\n"); argc = 0; break; } if((nlines = readlines(lineptr, MAXLINES)) >= 0) { if(option & NUM) sort((void**) lineptr, 0, nlines-1, (int (*)(void*,void*)) numcmp); else sort((void**) lineptr, 0, nlines-1, (int (*)(void*,void*)) ccmp); writelines(lineptr, nlines); return 0; } else { printf("Too many lines\n"); return 1; } } #define MAXLEN 1000 int get(char *, int); char *alloc(int); /* FUNCTION READLINES */ int readlines(char *lineptr[], int maxlines) { int len, nlines; char *p, line[MAXLEN]; nlines = 0; while((len = get(line, MAXLEN)) > 0) if(nlines >= maxlines || (p = alloc(len)) == NULL) return -1; else { line[len-1] = '\0'; strcpy(p, line); lineptr[nlines++]= p; } return nlines; } /* FUNCTION GETLINES */ int get(char *s, int lim) { int i, c; for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; i++) *s++ = c; if(c == '\n') { *s++ = c; i++; } *s = '\0'; return i; } #define ALLOCSIZE 10000 static char allocbuf[ALLOCSIZE]; static char *allocp = allocbuf; /* FUNCTION ALLOCATE MEMORY */ char *alloc(int n) { if(allocbuf + ALLOCSIZE - allocp >= n) { allocp += n; return allocp - n; } else printf("FULL\n"); } /* FUNCTION WRITELINES */ void writelines(char *lineptr[], int nlines) { printf("=======SORT=======\n"); if(option & RE) while(nlines-- > 0) printf("%s\n", lineptr[nlines]); else while(nlines-- > 0) printf("%s\n", *lineptr++); } /* FUNCTION SORT */ void sort(void *v[], int left, int right, int (*comp)(void *, void *)) { int i, last; void swap(void *v[], int, int); if(left >= right) return; swap(v, left, (left + right)/2); last = left; for(i = left+1; i <= right; i++) if((*comp)(v[i], v[left]) < 0) swap(v, ++last, i); swap(v, left, last); sort(v, left, last-1, comp); sort(v, last+1, right, comp); } /* FUNCTION SWAP */ void swap(void *v[], int i, int j) { int *temp; temp = v[i]; v[i] = v[j]; v[j] = temp; } #include <stdlib.h> /* FUNCTION NUMCMP */ int numcmp(char *s1, char *s2) { double v1, v2; v1 = atof(s1); v2 = atof(s2); if(v1 < v2) return -1; else if(v1 > v2) return 1; else return 0; } #include <ctype.h> /* FUNCTION CCMP */ int ccmp(char *s1, char *s2) { char v1, v2; do{ if(option & DIC) { while(!isalnum(*s1) && *s1 != ' ' && *s1 != '\0') *s1++; while(!isalnum(*s2) && *s2 != ' ' && *s2 != '\0') *s2++; } v1 = (option & FOLD) ? tolower(*s1) : (*s1); *s1++; v2 = (option & FOLD) ? tolower(*s2) : (*s2); *s2++; if(v1 == v2 && v1 == '\0') return 0; }while(v1 == v2); return v1 - v2; }
댓글을 작성하려면
로그인
해야 합니다.
jr313jjr 2년 전
save code save code save code save code save code