C++ 冰雹序列程序C+的问题+;
我正在写一个程序,大部分时间我都记下来了。我面临的唯一问题是,我的C++ 冰雹序列程序C+的问题+;,c++,loops,C++,Loops,我正在写一个程序,大部分时间我都记下来了。我面临的唯一问题是,我的max()函数(试图使用扫描算法)在试图找到给定冰雹序列n中的最大整数时返回了完全荒谬的值。看起来,对于我测试的前几个值,结果很好,它们很小,比如1或2,但是如果我输入3或更大,我会得到1153324768,这根本不是答案。有谁能引导我朝着正确的方向修正这个错误吗?我在下面列出了我的代码 #include <cstdio> #include <iostream> #include <algorithm
max()
函数(试图使用扫描算法)在试图找到给定冰雹序列n中的最大整数时返回了完全荒谬的值。看起来,对于我测试的前几个值,结果很好,它们很小,比如1或2,但是如果我输入3或更大,我会得到1153324768,这根本不是答案。有谁能引导我朝着正确的方向修正这个错误吗?我在下面列出了我的代码
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
// Next(n) returns the number that follows n in a hailstone sequence.
// For example, next(7) = 22 and next(8) = 4.
//
// Next requires n > 1, since there is no number that follows 1.
int Next(int n)
{
int remainder;
remainder = n % 2;
if (n>1)
{
if(remainder == 0)
{
return n/2;
}
else
{
return 3 * n + 1;
}
}
else
{
return n;
}
}
// The function writeHailstoneSequence(n) will take the parameter n
// and write the entire hailstone sequence starting from n, all in one line.
void writeHailstoneSequence(int n)
{
printf("The hailstone sequence starting with %d is: %d ", n, n);
while (n > 1)
{
n = Next(n);
printf("%d ", n);
}
}
// The function lengthHailstone(n) will take the parameter n and return the
// the length of the hailstone sequence starting at n.
int lengthHailstone(int n)
{
int length = 1;
while (n > 1)
{
n = Next(n);
length++;
}
return length;
}
//The function largest(n) will take one parameter, integer n, and return the largest value.
int largest(int n)
{
int A[] = {};
int big = A[0];
for(int i = 0; i < n; i++)
{
big = max(big, A[i]);
}
return big;
}
// The function longest(n) will return the longest hailstone sequence starting witht a number from 1 to n.
int longest(int n)
{
int lon = 0;
for (int i = 1; i <= n; i++)
{
lon = lengthHailstone(n);
}
return lon;
}
// The function largestHailstone(n) returns the largest value that occurs in a hailstone sequence that starts
// with a number from 1 to n.
int biggestHailstone(int n)
{
int biggest = 0;
for (int i = 1; i <= n; i++)
{
biggest = largest(n);
}
return biggest;
}
int main()
{
int n;
printf("What number shall I start with?\n");
scanf("%d", &n);
writeHailstoneSequence(n);
printf("\nThe length of the sequence is: %d\n", lengthHailstone(n));
printf("The largest number in the sequence is %d\n", largest(n));
printf("The longest hailstone sequence starting with a number up to %d has a length %d\n", n, longest(n));
printf("The longest hailstone sequence starting with a number up to %d begins with %d", n, biggestHailstone(n));
return 0;
}
#包括
#包括
#包括
使用名称空间std;
//Next(n)返回冰雹序列中n后面的数字。
//例如,next(7)=22,next(8)=4。
//
//下一步需要n>1,因为1后面没有数字。
int Next(int n)
{
整数余数;
余数=n%2;
如果(n>1)
{
如果(余数==0)
{
返回n/2;
}
其他的
{
返回3*n+1;
}
}
其他的
{
返回n;
}
}
//函数writeHailstoneSequence(n)将采用参数n
//从n开始,把整个冰雹序列写在一行。
无效写入序列(int n)
{
printf(“从%d开始的冰雹序列是:%d”,n,n);
而(n>1)
{
n=下一个(n);
printf(“%d”,n);
}
}
//函数lengthHailstone(n)将采用参数n并返回
//从n开始的冰雹序列的长度。
国际长度冰雹(国际北)
{
整数长度=1;
而(n>1)
{
n=下一个(n);
长度++;
}
返回长度;
}
//函数max(n)将接受一个参数整数n,并返回最大值。
最大整数(整数n)
{
int A[]={};
int big=A[0];
对于(int i=0;i
主要调查方向:
您未能将冰雹序列捕获到数组A中。“有人能引导我朝正确的方向修正此错误吗?”NP,当然可以。使用调试器逐行检查代码,并观察变量值的变化方式和位置。int A[]={};
-此行是错误的。我不太确定,但它可能创建了一个0元素的数组(或者是未定义的行为)。您试图在那里实现什么?您试图从何处检索数字?除了一个int之外,您不会向此函数传递任何内容。可能您想计算序列的元素并找到其中的最大值?代码不会编译。来自g++:“int A[]={};//错误:零大小数组'A'”另外,请记住C++不支持:“错误:ISO C++禁止可变长度数组”a’-Werror = VLA]考虑向量。我认为你需要把雹值放进数组“A[]”中。当函数扫描最大时,数组A是空的(int n)是空的。显然你没有传递任何值。