鱼C论坛

 找回密码
 立即注册
查看: 1525|回复: 9

[已解决]请问为啥会多一个感叹号呢?

[复制链接]
发表于 2021-8-8 20:14:31 | 显示全部楼层 |阅读模式
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;
}

最佳答案
2021-8-8 20:14:32
不愧是C语言,^_^

执行用时:0 ms, 在所有 C 提交中击败了100.00% 的用户
内存消耗:5.3 MB, 在所有 C 提交中击败了100.00% 的用户
  1. char * alphabetBoardPath(char * target){
  2.     static char result[512];
  3.     size_t index = 0;
  4.         ssize_t x = 0, y = 0;
  5.         for(size_t i = 0; target[i]; ++i) {
  6.             size_t num = target[i] - 'a';
  7.             ssize_t cx = num % 5;
  8.             ssize_t cy = num / 5;
  9.             char x_command, y_command;
  10.             if(cx > x) x_command = 'R';
  11.             else if(x > cx) x_command = 'L';
  12.             if(cy > y) y_command = 'D';
  13.             else if(y > cy) y_command = 'U';
  14.             ssize_t x_count = abs(cx - x);
  15.             ssize_t y_count = abs(cy - y);
  16.             if(target[i] != 'z') {
  17.                 while(y_count--) result[index++] = y_command;
  18.                 while(x_count--) result[index++] = x_command;
  19.             } else {
  20.                 while(x_count--) result[index++] = x_command;
  21.                 while(y_count--) result[index++] = y_command;
  22.             }
  23.             result[index++] = '!';
  24.             x = cx; y = cy;
  25.         }
  26.         result[index++] = '\0';
  27.         return result;
  28. }
复制代码

最佳答案

查看完整内容

不愧是C语言,^_^ 执行用时:0 ms, 在所有 C 提交中击败了100.00% 的用户 内存消耗:5.3 MB, 在所有 C 提交中击败了100.00% 的用户
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-8 20:14:32 | 显示全部楼层    本楼为最佳答案   
不愧是C语言,^_^

执行用时:0 ms, 在所有 C 提交中击败了100.00% 的用户
内存消耗:5.3 MB, 在所有 C 提交中击败了100.00% 的用户
  1. char * alphabetBoardPath(char * target){
  2.     static char result[512];
  3.     size_t index = 0;
  4.         ssize_t x = 0, y = 0;
  5.         for(size_t i = 0; target[i]; ++i) {
  6.             size_t num = target[i] - 'a';
  7.             ssize_t cx = num % 5;
  8.             ssize_t cy = num / 5;
  9.             char x_command, y_command;
  10.             if(cx > x) x_command = 'R';
  11.             else if(x > cx) x_command = 'L';
  12.             if(cy > y) y_command = 'D';
  13.             else if(y > cy) y_command = 'U';
  14.             ssize_t x_count = abs(cx - x);
  15.             ssize_t y_count = abs(cy - y);
  16.             if(target[i] != 'z') {
  17.                 while(y_count--) result[index++] = y_command;
  18.                 while(x_count--) result[index++] = x_command;
  19.             } else {
  20.                 while(x_count--) result[index++] = x_command;
  21.                 while(y_count--) result[index++] = y_command;
  22.             }
  23.             result[index++] = '!';
  24.             x = cx; y = cy;
  25.         }
  26.         result[index++] = '\0';
  27.         return result;
  28. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-8 20:16:05 | 显示全部楼层
原题和运行结果
微信图片_20210808201047.jpg
微信图片_20210808201234.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-8 20:47:56 | 显示全部楼层
图片上就是完整的原题?
看不出来输入 "code"
和输出有什么联系
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-8 20:50:25 | 显示全部楼层
还有,你发的代码不完整
还有,你刷题的那个网址发一下,是当前问题的地址
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-8 23:28:45 | 显示全部楼层
人造人 发表于 2021-8-8 20:50
还有,你发的代码不完整
还有,你刷题的那个网址发一下,是当前问题的地址

刷题的网址:https://leetcode-cn.com/problems/alphabet-board-path/
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-9 00:07:27 | 显示全部楼层
人造人 发表于 2021-8-8 20:50
还有,你发的代码不完整
还有,你刷题的那个网址发一下,是当前问题的地址

完整代码:#include <stdio.h>

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;
}

int main()
{
        char a[1024];
        char *p;
        scanf("%[^\n]",a);
       
        p=alphabetBoardPath(a);
       
        printf("%s\n",p);
       
        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-9 00:42:33 | 显示全部楼层
武德 发表于 2021-8-9 00:07
完整代码:#include  

char * alphabetBoardPath(char * target){

为什么要模拟走一遍,直接算出来不就好了
  1. #include <cmath>

  2. class Solution {
  3. public:
  4.     string alphabetBoardPath(string target) {
  5.         string result;
  6.         ssize_t x = 0, y = 0;
  7.         for(const auto &i: target) {
  8.             size_t num = (i - 'a');
  9.             ssize_t cx = num % 5;
  10.             ssize_t cy = num / 5;
  11.             string y_command, x_command;
  12.             if(cy > y) y_command = "D";
  13.             else if(y > cy) y_command = "U";
  14.             if(cx > x) x_command = "R";
  15.             else if(x > cx) x_command = "L";
  16.             ssize_t y_count = std::abs(cy - y);
  17.             ssize_t x_count = std::abs(cx - x);
  18.             while(y_count--) result += y_command;
  19.             while(x_count--) result += x_command;
  20.             result += "!";
  21.             y = cy; x = cx;
  22.         }
  23.         return result;
  24.     }
  25. };
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-9 00:58:01 | 显示全部楼层
还有先后顺序的说,改一改
  1. #include <cmath>

  2. class Solution {
  3. public:
  4.     string alphabetBoardPath(string target) {
  5.         string result;
  6.         ssize_t x = 0, y = 0;
  7.         for(const auto &i: target) {
  8.             size_t num = i - 'a';
  9.             ssize_t cx = num % 5;
  10.             ssize_t cy = num / 5;
  11.             string x_command, y_command;
  12.             if(cx > x) x_command = "R";
  13.             else if(x > cx) x_command = "L";
  14.             if(cy > y) y_command = "D";
  15.             else if(y > cy) y_command = "U";
  16.             ssize_t x_count = std::abs(cx - x);
  17.             ssize_t y_count = std::abs(cy - y);
  18.             if(i != 'z') {
  19.                 while(y_count--) result += y_command;
  20.                 while(x_count--) result += x_command;
  21.             } else {
  22.                 while(x_count--) result += x_command;
  23.                 while(y_count--) result += y_command;
  24.             }
  25.             result += "!";
  26.             x = cx; y = cy;
  27.         }
  28.         return result;
  29.     }
  30. };
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-9 01:09:44 | 显示全部楼层
你用 static 存储的字符串
忘了字符串以 '\0' 结束了

  1. #include <stdio.h>

  2. char * alphabetBoardPath(char * target){
  3.     char board[6][5] = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"};
  4.     int i,j;
  5.     int x,y;
  6.     static char p[1024];
  7.     int k=0;
  8.     i=j=0;
  9.     while(*target!='\0'){
  10.         for(x=0;x<6;x++){
  11.             for(y=0;y<5;y++){
  12.                 if(*target==board[x][y])
  13.                     goto A;
  14.             }
  15.         }//寻找字母坐标;
  16. A: ;

  17.        if(x>i){
  18.            while(x>i&&i<4){
  19.                p[k++]='D';
  20.                i++;
  21.            }
  22.        }
  23.        else if(x<i){
  24.            while(x<i){
  25.                p[k++]='U';
  26.                i--;
  27.            }
  28.        }
  29.        //行
  30.        if(y>j){
  31.            while(y>j){
  32.                p[k++]='R';
  33.                j++;
  34.            }
  35.        }
  36.        else if(y<j){
  37.            while(y<j){
  38.                p[k++]='L';
  39.                j--;
  40.            }
  41.        }
  42.        //列;
  43.        if(x>i){
  44.            p[k++]='D';
  45.        }
  46.        //z;
  47.        p[k++]='!';
  48.        i=x;
  49.        j=y;
  50.        target++;
  51.     }
  52.     p[k++] = '\0';
  53.     return p;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 21:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表