如何解决我如何减少代码的执行时间,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 举报,一经查实,本站将立刻删除。