|
50鱼币
原题:
图片;
代码:
char * alphabetBoardPath(char * target){
char board[6][5] = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"};
int i,j;
int x,y;
static char p[1024];
int k=0;
i=j=0;
while(*target!='\0'){
for(x=0;x<6;x++){
for(y=0;y<5;y++){
if(*target==board[x][y])
goto A;
}
}//寻找字母坐标;
A: ;
if(x>i){
while(x>i&&i<4){
p[k++]='D';
i++;
}
}
else if(x<i){
while(x<i){
p[k++]='U';
i--;
}
}
//行
if(y>j){
while(y>j){
p[k++]='R';
j++;
}
}
else if(y<j){
while(y<j){
p[k++]='L';
j--;
}
}
//列;
if(x>i){
p[k++]='D';
}
//z;
p[k++]='!';
i=x;
j=y;
target++;
}
return p;
}
不愧是C语言,^_^
执行用时:0 ms, 在所有 C 提交中击败了100.00% 的用户
内存消耗:5.3 MB, 在所有 C 提交中击败了100.00% 的用户
- char * alphabetBoardPath(char * target){
- static char result[512];
- size_t index = 0;
- ssize_t x = 0, y = 0;
- for(size_t i = 0; target[i]; ++i) {
- size_t num = target[i] - 'a';
- ssize_t cx = num % 5;
- ssize_t cy = num / 5;
- char x_command, y_command;
- if(cx > x) x_command = 'R';
- else if(x > cx) x_command = 'L';
- if(cy > y) y_command = 'D';
- else if(y > cy) y_command = 'U';
- ssize_t x_count = abs(cx - x);
- ssize_t y_count = abs(cy - y);
- if(target[i] != 'z') {
- while(y_count--) result[index++] = y_command;
- while(x_count--) result[index++] = x_command;
- } else {
- while(x_count--) result[index++] = x_command;
- while(y_count--) result[index++] = y_command;
- }
- result[index++] = '!';
- x = cx; y = cy;
- }
- result[index++] = '\0';
- return result;
- }
复制代码
|
最佳答案
查看完整内容
不愧是C语言,^_^
执行用时:0 ms, 在所有 C 提交中击败了100.00% 的用户
内存消耗:5.3 MB, 在所有 C 提交中击败了100.00% 的用户
|