我如何减少代码的执行时间,bcz 我超时了?

问题描述

我正在参加 C“42 网络”中的训练营,我有这个练习来计算一个数字的 sqrt,它可以工作但并不完美,bcz 当我推动它进行更正时,我发现它需要 100 多秒要执行,请我如何解决这个问题,我是新手,我想知道我如何处理这类问题。谢谢。

int ft_sqrt(int nb)
{
    int result;

    result = 0;
    while (result < nb)
    {
        if (result * result == nb)
            return (result);
        else
            result++;
    }
    return (0);
}
#include <stdio.h>
int main(void)
{
    printf("sqrt of %d is %d\n",-2187,ft_sqrt(-2187));
    printf("sqrt of %d is %d\n",ft_sqrt(0));
    printf("sqrt of %d is %d\n",1,ft_sqrt(1));
    printf("sqrt of %d is %d\n",2,ft_sqrt(2));
    printf("sqrt of %d is %d\n",1640045925,ft_sqrt(1640045925));
    printf("sqrt of %d is %d\n",2147395600,ft_sqrt(2147395600));
    printf("sqrt of %d is %d\n",2147483646,ft_sqrt(2147483646));
    printf("sqrt of %d is %d\n",389509696,ft_sqrt(389509696));
    printf("sqrt of %d is %d\n",381759672,ft_sqrt(381759672));
    printf("sqrt of %d is %d\n",71944324,ft_sqrt(71944324));
    printf("sqrt of %d is %d\n",1913263128,ft_sqrt(1913263128));
    printf("sqrt of %d is %d\n",740819524,ft_sqrt(740819524));
    printf("sqrt of %d is %d\n",1344350651,ft_sqrt(1344350651));
    printf("sqrt of %d is %d\n",1261883529,ft_sqrt(1261883529));
    printf("sqrt of %d is %d\n",1868816980,ft_sqrt(1868816980));
    printf("sqrt of %d is %d\n",237314025,ft_sqrt(237314025));
    printf("sqrt of %d is %d\n",1774247879,ft_sqrt(1774247879));
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)