对称数函数不按预期工作

huangapple go评论55阅读模式
英文:

Symmetrical number function not working as expected

问题

以下是代码的翻译部分:

bool problem30(int n){
    bool result = false;
    char* a = (char*)malloc(sizeof(char) * 100);
    sprintf(a, "%c", n);
    int lengh = strlen(a);
    for (int i = 0; i <= lengh; i++)
    {
        if (a[i] == a[lengh - i - 1])
        {    
            return true;
        }   
        return false;
    }
}

请注意,我已经更正了代码中的一些拼写错误和格式问题,但仍然需要进一步的调试以确保其正确性。如果您需要关于修复代码错误的帮助,请提出具体的问题。

英文:
bool problem30(int n){
    bool result = false;
    char* a = (char*)malloc(sizeof(char) * 100);
    sprintf(a, &quot;%c&quot;, n);
    int lengh = strlen(a);
    for (int i = 0; i &lt;= lengh; i++)
    {
        if (a[i] == a[lengh - i - 1])
        {    
            return true;
        }   return false;
    }

I want to check for symmetrical numbers in the above function but when I run the code, all the numbers return symmetrical and I don't know where I have gone wrong.

Could some one please help me to fix it? Thank you.

答案1

得分: 1

以下是翻译好的内容:

有函数存在一些问题。

首先,“return”在第一对数字上返回,而不考虑其余数字如何。

例如:7657不是对称的,但是由于您的函数在第一次循环时返回,它会说它是。可以通过将结果设置为“true”来修复此问题。实际上,我会从result == true开始,然后测试它是否不对称。

其次,您的malloc表达式有点多余,但在正确的轨道上。Malloc无论如何都需要字节,而size_of(char) == size_of(byte)......但这两者都不需要,因为"Malloc需要字节"。

类似这样的内容(更新/使用库和主函数):

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

bool problem30(int n){
    bool result = true;
    char* a = (char *)malloc(100);
    sprintf(a, "%d", n);
    
    int length = strlen(a);
    for (int i = 0; i <= length; i++)
    {
        if (a[i] != a[length - i - 1])
        {    
            result = false;
        }  
    }
    return result;
}

int main() {
    // 示例 1
    printf("%d\n", problem30(7558));
    
    // 示例 2
    printf("%d\n", problem30(7557));

    return 0;
}

希望这有所帮助。

英文:

There are a couple of problems with the function.

Firstly "return" is returning on the first pair regardless of what the rest of the number is.

For Example: 7657 isn't symmetrical however as your function returns on the first loop it will say it is. This can be fix by instead setting result "true". In fact I would start with result == true and test if it was not symmetrical.

Secondly your malloc expression is a bit excessive, but on the right track. Malloc takes bytes anyway a size_of(char) == size_of(byte)... but both of these are not required as "Malloc takes bytes"

Something like this (updated /w libraries and main):

#include &lt;stdio.h&gt;
#include &lt;stdbool.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

bool problem30(int n){
    bool result = true;
    char* a = (char *)malloc(100);
    sprintf(a, &quot;%d&quot;, n);
    
    int length = strlen(a);
    for (int i = 0; i &lt;= length; i++)
    {
        if (a[i] != a[length - i - 1])
        {    
            result = false;
        }  
    }
    return result;
}

int main() {
    // Example 1
    printf(&quot;%d\n&quot;,problem30(7558));
    
    // Example 2
    printf(&quot;%d\n&quot;,problem30(7557));

    return 0;
}

huangapple
  • 本文由 发表于 2023年5月29日 23:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定