我可以获得有关 C 语言中的“isPalindrome()”函数的一些反馈吗?

发布于 2024-09-30 04:52:08 字数 657 浏览 0 评论 0原文

我正在用 C 语言编写一些有用的函数。其中之一是 isPalindrome()。

我想确定一个数字是否是回文,我应该...

  • 获取数组中的所有数字,
  • 并使用两个索引进行迭代 - 一个从 0 开始,一个到数组计数
  • 递增/递减索引,同时为数组添加下标它们匹配,如果数组计数达到 0,我们就有一个回文(即完成所有数字)。

我想出了......

int isPalindrome(int num) {

    int places[100];
    int i = 0;
    while (num > 0) {
        places[i++] = num % 10; 
        num /= 10;
    }

    int j = 0;
    while (i >= 0 && places[j++] == places[--i]) {
    }
    return i == -1;

}

这通常是如何完成的?

我正在自学 C,虽然我可以知道我的代码何时编译并且不需要花一整天的时间来解决问题,但我没有任何专家的眼睛来告诉我是否“我走在正确的轨道上。

那么,对我的代码有什么改进或建议吗?

非常感谢!

I'm writing some useful functions in C. One of them is isPalindrome().

I figured to determine if a number is a palindrome or not, I should...

  • get all digits in an array
  • iterate through with two indexes - start one at 0 and one to the array count
  • increment/decrement the indexes whilst subscripting the array whilst they match and if the array count gets to 0 we have a palindrome (i.e. finishing going through all digits).

I came up with...

int isPalindrome(int num) {

    int places[100];
    int i = 0;
    while (num > 0) {
        places[i++] = num % 10; 
        num /= 10;
    }

    int j = 0;
    while (i >= 0 && places[j++] == places[--i]) {
    }
    return i == -1;

}

Is this generally how it is done?

I'm learning C by myself, and although I can tell when my code compiles and doesn't take all day to work something out, I don't have any expert eyes to tell me if I'm on the right track.

So, any improvements or suggestions on my code?

Thanks very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Bonjour°[大白 2024-10-07 04:52:08

你只需要循环 while i > j。一旦i <= j,您只需再次检查所有字符。

You only have to loop while i > j. Once i <= j, you are just checking all the characters a second time.

所谓喜欢 2024-10-07 04:52:08

尽管在下面使用内联 ++-- 运算符可能看起来很聪明:

while (i >= 0 && places[j++] == places[--i]) { 
} 

如果将这些放在中,您的代码将更容易阅读循环体:

while (i >= 0 && places[j] == places[i-1]) { 
    j++;
    i--;
} 

这样,代码的读者就不必考虑在条件测试中更改 ij 的值可能产生的副作用。对编译代码的速度可能不会产生可测量的影响(尽管,如果性能对此函数很重要,您应该与编译器检查)。

此外,您还遇到了一个错误,如果 i == 0,您将访问 places[-1]

Although using inline ++ and -- operators in the following might seem clever:

while (i >= 0 && places[j++] == places[--i]) { 
} 

your code will be easier to read if you put those inside the loop body:

while (i >= 0 && places[j] == places[i-1]) { 
    j++;
    i--;
} 

This way, the reader of the code won't have to think about the possible side effects of changing the values of i and j within the conditional test. There will probably be no measurable effect on the speed of the compiled code (although, if performance is important to this function, you should check with your compiler).

Also, you've got a bug where you will access places[-1] if i == 0.

土豪 2024-10-07 04:52:08

我只是使用 sprintf 来“将字符串转换为数字”:

char places[100];
sprintf(places, "%i", num);
i = strlen(places);

I'd just use sprintf to "convert the string to digits":

char places[100];
sprintf(places, "%i", num);
i = strlen(places);
╄→承喏 2024-10-07 04:52:08

在java中

static boolean isPalindrome(String p) {
    return p.equals(new StringBuilder(p).reverse().toString());
}

在c++和c中

int IsPalindrome(char *string) {
    int bottom = 0, top;

    top = strlen(string) - 1;
    while(bottom < top && string[bottom] == string[top]) {
        ++bottom;
        --top;
    }
    return (bottom >= top ? 1:0);
}

注意,如果您需要对数字输入执行此操作,则需要编写itoa函数。或者使用(链接)。

一般都是这样做的。这也适用于所有基地,而不仅仅是 10 个。

In java

static boolean isPalindrome(String p) {
    return p.equals(new StringBuilder(p).reverse().toString());
}

In c++ and c

int IsPalindrome(char *string) {
    int bottom = 0, top;

    top = strlen(string) - 1;
    while(bottom < top && string[bottom] == string[top]) {
        ++bottom;
        --top;
    }
    return (bottom >= top ? 1:0);
}

Note, You need to write itoa function, if you need to do this for a number input. Or use ( link ).

Thats how it is generally done. This would also work for all bases and not only 10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文