鱼C论坛

 找回密码
 立即注册
查看: 1303|回复: 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% 的用户
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% 的用户
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-8 20:14:32 | 显示全部楼层    本楼为最佳答案   
不愧是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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-8 20:16:05 | 显示全部楼层
原题和运行结果
微信图片_20210808201047.jpg
微信图片_20210808201234.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-8 20:47:56 | 显示全部楼层
图片上就是完整的原题?
看不出来输入 "code"
和输出有什么联系
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-8 20:50:25 | 显示全部楼层
还有,你发的代码不完整
还有,你刷题的那个网址发一下,是当前问题的地址
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

刷题的网址:https://leetcode-cn.com/problems/alphabet-board-path/
想知道小甲鱼最近在做啥?请访问 -> 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

char * alphabetBoardPath(char * target){

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

class Solution {
public:
    string alphabetBoardPath(string target) {
        string result;
        ssize_t x = 0, y = 0;
        for(const auto &i: target) {
            size_t num = (i - 'a');
            ssize_t cx = num % 5;
            ssize_t cy = num / 5;
            string y_command, x_command;
            if(cy > y) y_command = "D";
            else if(y > cy) y_command = "U";
            if(cx > x) x_command = "R";
            else if(x > cx) x_command = "L";
            ssize_t y_count = std::abs(cy - y);
            ssize_t x_count = std::abs(cx - x);
            while(y_count--) result += y_command;
            while(x_count--) result += x_command;
            result += "!";
            y = cy; x = cx;
        }
        return result;
    }
};
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

class Solution {
public:
    string alphabetBoardPath(string target) {
        string result;
        ssize_t x = 0, y = 0;
        for(const auto &i: target) {
            size_t num = i - 'a';
            ssize_t cx = num % 5;
            ssize_t cy = num / 5;
            string 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 = std::abs(cx - x);
            ssize_t y_count = std::abs(cy - y);
            if(i != 'z') {
                while(y_count--) result += y_command;
                while(x_count--) result += x_command;
            } else {
                while(x_count--) result += x_command;
                while(y_count--) result += y_command;
            }
            result += "!";
            x = cx; y = cy;
        }
        return result;
    }
};
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-9 01:09:44 | 显示全部楼层
你用 static 存储的字符串
忘了字符串以 '\0' 结束了
#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++;
    }
    p[k++] = '\0';
    return p;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 22:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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