条件运算函数的无限 while 循环问题
Infinite while loop issue with a conditional operation function
我的代码陷入了无限循环。当前数减半,当下一个数为偶数时,函数应执行2n+1。如果是奇数,它应该执行 3n + 1。一旦执行了任何一个操作,它应该再次减半并循环直到 n = 1。代码如下:
#include "stdio.h"
#include "assert.h" // ?
long int hailstone(long int k);
int main(void) {
long int n = 77;
hailstone(n);
return 0;
}
long int hailstone(long int k) {
while (k != 1) {
k = k/2;
if (k % 2 == 0) {
k = 2 * k + 1;
printf("%lu", k);
} else if (k % 2 != 0) {
k = 3 * k + 1;
printf("%lu", k);
} else if (k == 1) {
printf("blue sky!");
}
}
}
特定断言是否有助于编译器按预期执行代码?
Would a particular assertion help the compiler execute the code as expected?
没有
断言用于停止某些事件的执行。
您的代码过于复杂且错误。
基本上可以归结为这个(试一试):
long int hailstone(long int k) {
while (k != 1) {
k = k/2;
printf("k divided by 2: %lu\n", k);
if (k % 2 == 0) {
k = 2 * k + 1;
printf("k after k = 2 * k + 1 %lu\n", k);
}
}
}
输出:
k divided by 2: 38
k after k = 2 * k + 1 77
k divided by 2: 38
k after k = 2 * k + 1 77
k divided by 2: 38
k after k = 2 * k + 1 77
...
只是盲目应用definitions:
#include <stdio.h>
void hailstone(long int k);
int main(void) {
long int n = 77;
hailstone(n);
return 0;
}
long int func(long int k)
{
if (k % 2 == 0)
return k / 2;
else
return 3 * k + 1;
}
void hailstone(long int k) {
while (k != 1)
{
printf("k = %d\n", k);
k = func(k);
}
}
据我了解你的代码
如果 n 的值是 pair ,你做这个 n/2 ,如果不是你显示 3*n+1
我的代码:
输出:116,58,29,44,22,11,17,26,13,20,10,5,8,4,2,1
#include "stdio.h"
#include "assert.h" // ?
long int hailstone(long int k);
int main(void)
{
long int n = 77;
hailstone(n);
return 0;
}
long int hailstone(long int k)
{
while (k != 1)
{
if ( k % 2 ==0)
{
printf("k = %lu\n",k/2);
k/=2; // k=k/2
}
else
{
k = 3 * k + 1;
}
}
printf("blue sky!\n");
}
我的代码陷入了无限循环。当前数减半,当下一个数为偶数时,函数应执行2n+1。如果是奇数,它应该执行 3n + 1。一旦执行了任何一个操作,它应该再次减半并循环直到 n = 1。代码如下:
#include "stdio.h"
#include "assert.h" // ?
long int hailstone(long int k);
int main(void) {
long int n = 77;
hailstone(n);
return 0;
}
long int hailstone(long int k) {
while (k != 1) {
k = k/2;
if (k % 2 == 0) {
k = 2 * k + 1;
printf("%lu", k);
} else if (k % 2 != 0) {
k = 3 * k + 1;
printf("%lu", k);
} else if (k == 1) {
printf("blue sky!");
}
}
}
特定断言是否有助于编译器按预期执行代码?
Would a particular assertion help the compiler execute the code as expected?
没有
断言用于停止某些事件的执行。
您的代码过于复杂且错误。
基本上可以归结为这个(试一试):
long int hailstone(long int k) {
while (k != 1) {
k = k/2;
printf("k divided by 2: %lu\n", k);
if (k % 2 == 0) {
k = 2 * k + 1;
printf("k after k = 2 * k + 1 %lu\n", k);
}
}
}
输出:
k divided by 2: 38
k after k = 2 * k + 1 77
k divided by 2: 38
k after k = 2 * k + 1 77
k divided by 2: 38
k after k = 2 * k + 1 77
...
只是盲目应用definitions:
#include <stdio.h>
void hailstone(long int k);
int main(void) {
long int n = 77;
hailstone(n);
return 0;
}
long int func(long int k)
{
if (k % 2 == 0)
return k / 2;
else
return 3 * k + 1;
}
void hailstone(long int k) {
while (k != 1)
{
printf("k = %d\n", k);
k = func(k);
}
}
据我了解你的代码
如果 n 的值是 pair ,你做这个 n/2 ,如果不是你显示 3*n+1
我的代码:
输出:116,58,29,44,22,11,17,26,13,20,10,5,8,4,2,1
#include "stdio.h"
#include "assert.h" // ?
long int hailstone(long int k);
int main(void)
{
long int n = 77;
hailstone(n);
return 0;
}
long int hailstone(long int k)
{
while (k != 1)
{
if ( k % 2 ==0)
{
printf("k = %lu\n",k/2);
k/=2; // k=k/2
}
else
{
k = 3 * k + 1;
}
}
printf("blue sky!\n");
}