カテゴリ
全体プログラミング scheme verilog 未分類 以前の記事
2016年 04月2016年 03月 2016年 02月 2016年 01月 2015年 12月 2015年 11月 2015年 10月 2015年 09月 2015年 08月 2015年 07月 2015年 06月 2015年 03月 お気に入りブログ
PHPで競技プログラミングメモ帳
最新のトラックバック
ライフログ
検索
タグ
racket
その他のジャンル
ブログパーツ
最新の記事
外部リンク
ファン
記事ランキング
ブログジャンル
画像一覧
|
先ほどのプログラムを少し短くしてみました。とはいえオリジナルより17行長いです。Cプログラマはオリジナルのほうを好むと思います。
/*
* enc_str_2.c * KIHON JYOUHOU GIJYUTUSYA SIKEN (IPA) * heisei 27 nendo syunki, gogo, toi 9 * arranged by tempurature, 2016. 4.14 */ #define RSIZ 5 /* 換字表の行数 */ #define CSIZ 13 /* 換字表の列数 */ extern void enc_str(char[], char[RSIZ][CSIZ]); struct POSITION { int row; int col; }; #define SET_POS(pos, row1, col1) (pos).row = row1; (pos).col = col1; static int search_row_and_col(char c, struct POSITION *pos, char xchg_t[RSIZ][CSIZ]) { int i, j; for (i = 0; i < RSIZ; i++) { for (j = 0; j < CSIZ; j++) { if (c == xchg_t[i][j]) { pos->row = i; pos->col = j; return 1; /* success */ } } } return 0; /* failed */ } static void exchange_pair(struct POSITION *p0, struct POSITION *p1) { struct POSITION prev0 = {p0->row, p0->col}; struct POSITION prev1 = {p1->row, p1->col}; if (prev0.row == prev1.row) { if (prev0.col == prev1.col) { /* same character */ SET_POS(*p0 ,(prev0.row + 1) % RSIZ, (prev0.col + 1) % CSIZ) SET_POS(*p1, p0->row, p0->col) } else { /* same row */ SET_POS(*p0, prev1.row, prev1.col) SET_POS(*p1, prev0.row, prev0.col) } } else { if (prev0.col == prev1.col) { /* same column */ SET_POS(*p0, prev0.row, (prev0.col + 1) % CSIZ) SET_POS(*p1, prev1.row, (prev1.col + 1) % CSIZ) } else { /* else case */ SET_POS(*p0, prev1.row, prev0.col) SET_POS(*p1, prev0.row, prev1.col) } } } extern void enc_str(char str[], char xchg_t[RSIZ][CSIZ]) { int state = 0; int idx = 0; int idx0, idx1; struct POSITION pos, pos0, pos1; while (str[idx] != '\0') { if (search_row_and_col(str[idx], &pos, xchg_t)) { switch (state) { case 0: idx0 = idx; SET_POS(pos0, pos.row, pos.col) state = 1; break; case 1: idx1 = idx; SET_POS(pos1, pos.row, pos.col) exchange_pair(&pos0, &pos1); str[idx0] = xchg_t[pos0.row][pos0.col]; str[idx1] = xchg_t[pos1.row][pos1.col]; state = 0; break; } } idx++; } if (state == 1) str[idx0] = xchg_t[RSIZ - 1 - pos0.row][CSIZ - 1 - pos0.col]; }
by tempurature
| 2016-04-14 04:48
| プログラミング
| ||||||||||||||||||||||
ファン申請 |
||