C++ 冰雹序列c++;计算序列长度所需的函数。无限循环问题

C++ 冰雹序列c++;计算序列长度所需的函数。无限循环问题,c++,algorithm,loops,C++,Algorithm,Loops,我需要编写一个函数,计算并返回以前在已有函数中计算的冰雹序列的长度。 我尝试过的每件事都给了我一个“22”的无限循环。 不允许使用数组。每件事都必须用循环来完成,每个函数只有一个循环 我主要尝试使用前面的length++函数;加上。 但我不知道该怎么办 #include <cstdio> using namespace std; // The function next(n)takes an integer value n and // returns the number th

我需要编写一个函数,计算并返回以前在已有函数中计算的冰雹序列的长度。 我尝试过的每件事都给了我一个“22”的无限循环。 不允许使用数组。每件事都必须用循环来完成,每个函数只有一个循环

我主要尝试使用前面的length++函数;加上。 但我不知道该怎么办

#include <cstdio>
using namespace std;

// The function next(n)takes an integer value n and 
// returns the number that follows n in a hailstone sequence. 
// For example: next(7) = 22 and next(22) = 11.

int next(int n)
{
  if (n > 1)
  {            
    if ((n % 2) == 0 )
    {
      n = n / 2;
    }
      else
      {
        n = 3 * n + 1;
      }
    printf("%i ",n); 

  }          
  return 0;
}

// The function hailstone reads int n and 
// prints its entire hailstone sequence.

void hailstone(int n)
{
  while(n>1)
  {
    next(n);
  }
} 

int length(int n)
{
  int length = 1;
  return length;
}

int main()
{
  int n; 

  printf("What number shall I start with?");
  scanf("%i", &n);

  printf("The hailstone sequence starting at %i is: ", n);
  hailstone(n);


  printf("The length of the sequence is: %i", length(n));

  return 0;
}
#包括
使用名称空间std;
//函数next(n)取整数值n和
//返回冰雹序列中n后面的数字。
//例如:next(7)=22和next(22)=11。
int next(int n)
{
如果(n>1)
{            
如果((n%2)=0)
{
n=n/2;
}
其他的
{
n=3*n+1;
}
printf(“%i”,n);
}          
返回0;
}
//函数“冰雹”读取int n和int n
//打印整个冰雹序列。
虚空冰雹(国际北)
{
而(n>1)
{
下一步(n);
}
} 
整数长度(整数n)
{
整数长度=1;
返回长度;
}
int main()
{
int n;
printf(“我应该从什么数字开始?”);
scanf(“%i”和“&n”);
printf(“从%i开始的冰雹序列是:,n);
冰雹(n);
printf(“序列的长度为:%i”,长度(n));
返回0;
}

问题在于您没有更改
n
值。试试这个:

int next(int n)
{
  if (n > 1)
  {            
    // As before
  }          
  return n;
}
注意
返回n
返回序列中的下一个值。其次,我们需要:

void hailstone(int n)
{
  while(n>1)
  {
    n = next(n);
  }
} 
我将此更改为
n=next(n),因此我们在序列中拾取新值

此外,
长度
可通过以下公式计算:

int hailstone(int n)
{
  int length = 0;
  while(n>1)
  {
    n = next(n);
    length++;
  }
  return length;
} 

这会计算我们调用
next()
的次数。首先,在
冰雹
的循环中,您不会修改
n
,因为
next
函数的参数为。您可以将
next
的参数改为a,或者利用返回值,即

int next(int n)
{
    // ...       
    return n;
        // ^
}
并在循环中使用它来修改
n

void hailstone(int n)
{
    while (n > 1)
    {
        n = next(n);
     // ^^^
    }
} 
其次,要计算长度,您应该在循环期间记录它,并且再次利用返回值将长度信息传递给调用方

unsigned hailstone(int n) // return type is changed to unsigned
{
    unsigned length = 0;
    while (n > 1)
    {
        n = next(n);
        ++length;
    }
    return length;
} 
然后,您可以在
main
中打印长度信息:

printf("The length of the sequence is: %u", hailstone(n));
函数
next()
将整数作为参数,调用该函数时,该值被复制到一个完全独立的变量中。这意味着对函数中变量的任何更改都不会对其值用于调用函数的变量产生任何影响

您应该使用这样的引用:
void next(int&n)
(您将在以后进入C++的过程中了解引用),使用全局变量,或者,根据上下文,我认为最好在每次调用
next()
结束时返回新的n值。只需替换行
返回0next()
中使用
返回n
(不确定为什么函数返回整数,然后才返回0),然后使用此行调用函数:
n=next(n)

对于长度,您可以在函数
hailstone()
中使用一个计数器变量,在循环中递增它,然后只返回长度

新守则:

#include <cstdio>
using namespace std;

// The function next(n)takes an integer value n and 
// returns the number that follows n in a hailstone sequence. 
// For example: next(7) = 22 and next(22) = 11.

int next(int n)
{
  if (n > 1)
  {            
    if ((n % 2) == 0 )
    {
      n = n / 2;
    }
    else
    {
      n = 3 * n + 1;
    }
    printf("%i ",n);

  }          
  return n; //return the new value of n
}

// The function hailstone reads int n and 
// prints its entire hailstone sequence.

int hailstone(int n)
{
  int length = 0; //counter variable
  while(n>1)
  {
    n = next(n); //update n with the new value
    length++; //increment counter
  }
  return length; //return length to be used later
} 

int main()
{
  int n; 

  printf("What number shall I start with?");
  scanf("%i", &n);

  int length; //variable to store the length
  printf("The hailstone sequence starting at %i is: ", n);
  length = hailstone(n); //save the length from the function call

  printf("The length of the sequence is: %i", length);

  return 0;
}
#包括
使用名称空间std;
//函数next(n)取整数值n和
//返回冰雹序列中n后面的数字。
//例如:next(7)=22和next(22)=11。
int next(int n)
{
如果(n>1)
{            
如果((n%2)=0)
{
n=n/2;
}
其他的
{
n=3*n+1;
}
printf(“%i”,n);
}          
return n;//返回n的新值
}
//函数“冰雹”读取int n和int n
//打印整个冰雹序列。
国际冰雹(国际北)
{
int length=0;//计数器变量
而(n>1)
{
n=next(n);//用新值更新n
长度+++;//增量计数器
}
返回长度;//稍后使用的返回长度
} 
int main()
{
int n;
printf(“我应该从什么数字开始?”);
scanf(“%i”和“&n”);
int length;//用于存储长度的变量
printf(“从%i开始的冰雹序列是:,n);
长度=冰雹(n);//保存函数调用的长度
printf(“序列的长度为:%i”,长度);
返回0;
}

请注明最佳匹配答案:如果没有实际答案,您可以留下评论,说明其与您的期望不符的原因。或者,更好的是,你的问题和补充细节。