热门

最新

红包

立Flag

投票

同城

我的

发布
zchuashifeng
雨枪幻。
3 年前
truezchuashifeng

c语言网.1095.3n+1问题

这个题目描述, 并没有明确表明输入的ij大小符合规范, 只是说between,所以在for循环里区间设定之前要先排序

以下我的代码:
#include <stdio.h>

int cyc_len(int x)
{
int sum = 0;
while(x != 1)
{
sum++;
if(x % 2 == 0)
x /= 2;
else
x = x*3 + 1;
}
return sum+1;
}

void swap(int *x, int *y)
{
int tmp;
if(*x > *y)
{
tmp = *x;
*x = *y;
*y = tmp;
}
}

int main()
{
int x, y;
while(~scanf("%d%d", &x, &y))
{
printf("%d %d ", x, y);
swap(&x, &y);
int i, sum = 0;
for(i=x; i<=y; i++)
{
if(sum < cyc_len(i))
sum = cyc_len(i);
}
printf("%d\n",sum);
}
return 0;
}

CSDN App 扫码分享
分享
评论
点赞
打赏
  • 复制链接
  • 举报
下一条:
eq:equal to 等于ne:not equal to 不等于lt:less than 小于le:less than or equal to 小于等于ge:greater than or equal to 大于等于gt:greater than 大于e-equall-lessg-greatert-thann-no
立即登录