gpt4 book ai didi

c - uVa100- '3n+1' ~ 超过时间限制

转载 作者:太空宇宙 更新时间:2023-11-04 07:06:41 26 4
gpt4 key购买 nike

<分区>

最近学完了C语言的基础。我开始解决 uVa 在线判断问题。我解决了 3n+1 问题,在我的编译器中运行它。它做得很好。但是当我在线提交时它说“超过时间限制”。我似乎无法放置解决方案。

代码如下:

#include<stdio.h>
int execute(int x, int y)
{
int i, n, k, maxCycle=0;

for(i=x; i<=y; ++i)
{
n=1;
k=i;
while(k!=1)
{
if(k%2==1)
k=3*k+1;
else
k=k/2;
n++;
}
if(n>=maxCycle)
maxCycle=n;
}
return maxCycle;
}

int main()
{
while(1)
{
int a, b, max;
scanf("%d %d", &a, &b);
max=execute(a, b);
printf("%d %d %d\n", a, b, max);
}
return 0;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com