回复 9楼 linlulu001
201612121335改这组数据
程序代码:
4 d e f a b c b d e c d f abcdef
[此贴子已经被作者于2016-12-12 13:35编辑过]
[fly]存在即是合理[/fly]
4 d e f a b c b d e c d f abcdef
[此贴子已经被作者于2016-12-12 13:35编辑过]
[此贴子已经被作者于2016-12-12 14:21编辑过]
#include "fun.h" // 最终字符串长度 int getLength(char array[][3], int len) { int num = 0; bool flags[CHAR_MAX] = {false}; for (int i = 0; i < len; ++i) { flags[array[i][0]] = true; flags[array[i][1]] = true; flags[array[i][2]] = true; } for (int i = 0; i < CHAR_MAX; ++i) { if (flags[i]) num++; } return num; } // 判断array后两列是否包含字符ch bool isIn(char ch, char array[][3], int len) { for (int i = 0; i < len; ++i) { if (ch == array[i][1]) return true; if (ch == array[i][2]) return true; } } // 获取第一个字符 char findFirst(char array[][3], int len) { for (int i = 0; i < len; ++i) { if (!isIn(array[i][0], array, len)) return array[i][0]; } return -1; } // 从数组中删除ch,并前移其他项 void change(char ch, char array[][3], int len) { for (int i = 0; i < len; ++i) { if (ch == array[i][0]) { array[i][0] = array[i][1], array[i][1] = array[i][2], array[i][2] = -1; } if (ch == array[i][1]) { array[i][1] = array[i][2], array[i][2] = -1; } if (ch == array[i][2]) { array[i][2] = -1; } } } void fun(char array[][3], int len, char *str) { int ans_len = getLength(array, len); for (int i = 0; i < ans_len; ++i) { str[i] = findFirst(array, len); change(str[i], array, len); } str[ans_len] = '\0'; }
#include "fun.h" void getString(char array[][3], int len, char *str) { int pos = 0; bool flags[CHAR_MAX] = {false}; for (int i = 0; i < len; ++i) { flags[array[i][0]] = true; flags[array[i][1]] = true; flags[array[i][2]] = true; } for (int i = 0; i < CHAR_MAX; ++i) { if (flags[i]) str[pos++] = (char) i; } str[pos] = '\0'; } void change(char *p1, char *p2) { char tmp = *p1; while (p1 < p2) { *p1 = *(p1 + 1); p1++; } *p2 = tmp; } bool fix(char array[][3], int len, char *str) { for (int i = 0; i < len; ++i) { char *p1 = strchr(str, array[i][0]); char *p2 = strchr(str, array[i][1]); char *p3 = strchr(str, array[i][2]); if (p2 > p3) { change(p3, p2); return true; } if (p1 > p2) { change(p2, p1); return true; } } return false; } void fun(char array[][3], int len, char *str) { getString(array, len, str); while (fix(array, len, str)); }
[此贴子已经被作者于2016-12-12 18:12编辑过]