卡在最大无符号短变量提示上
Stuck on maximum unsigned short variable prompt
大家好 Stack Overflow!
所以我卡在了这个提示上:
"Use a loop to determine the value of n that will produce the largest n! value that could be stored in an unsigned short variable. Print out the maximum value for an unsigned short variable, the value of n that will produce the largest n! that is less than or equal to the maximum value for an unsigned short variable. NOTE: The constant USHRT_MAX contained in limits.h provides the maximum value for an unsigned short variable."
我猜,对于上述提示,它解决了为什么当我在程序中输入一个整数(例如 34)时,我得到 0 作为 34 的阶乘的输出。
判断n的代码我已经写好了!到目前为止输入 n 时的值,但是这个新部分让我感到困惑。
我认为这不会有帮助,但这是出现此提示之前的代码:
#include <stdio.h>
unsigned long Factorial(unsigned int n);
int main(void)
{
int num[11],i=0,factorials[11];
printf("Please enter up to 10 integers (q to quit): ");
//Ask for integers until a 'q' is entered and store integer values entered into array 'num'
while (scanf("%u",&num[i]))
{
//Store the factorial of integers entered into array 'factorials'
factorials[i]=Factorial(num[i]);
//Print numbers out in a two column table
printf("%5u %9u\n",num[i],factorials[i]);
i++;
}
return 0;
}
//Calculates the factorial of 'n'
unsigned long Factorial(unsigned int n)
{
int nFactorial,i=1;
nFactorial=n;
while (i<n)
{
nFactorial=nFactorial*i;
i++;
}
n=nFactorial;
}
无论如何,如果有人能提供帮助,我将不胜感激!我知道这听起来像是一个高难度的问题,所以即使是指针也能提供帮助!
谢谢任何人!
干杯,威尔。
编辑:如果我的代码难以阅读,我提前道歉,我正在努力让它变得更好
编辑:
到目前为止,我想出了这个来回答提示,但它似乎不对。输出值为 8...
//Detemine largest max value of n
for (i=0;Factorial(i)<=USHRT_MAX;i++);
printf("The max value for an unsigned short is %u\n Max value of n: %u\n",USHRT_MAX,i-1);
return 0;
由于您使用的是短函数,因此可以将阶乘和存储在更大的类型中,例如 unsigned long int
。
int main(void)
{
unsigned long int sum = 1;
unsigned int n;
for( n = 1; sum < USHRT_MAX; n++ ) {
sum *=n;
}
printf("%lu\n", sum);
printf("%u\n", n);
}
这有点作弊,因为不能保证 long int
会比 short
大,但很有可能。您可以通过验证来缓解这种情况。
assert( sizeof(unsigned short) < sizeof(unsigned long int) );
non-cheating方法是检查你是否即将溢出。你会想要这样做,但你不能。
USHRT_MAX >= sum * n
sum * n
会溢出。相反,将两边除以 n
并检查。
USHRT_MAX / n >= sum
这将在 sum *= n
溢出之前停止。我们可以通过插入一些数字来验证。 USHRT_MAX = 23,n = 4 和总和 = 6...
23 / 4 >= 6
5 >= 6
请注意,这是整数除法,因此被截断了。这对我们的目的来说很好。
#include <stdio.h>
#include <limits.h>
int main(void)
{
unsigned short sum = 1;
unsigned int n;
for( n = 1; (USHRT_MAX / n) >= sum; n++ ) {
sum *=n;
}
// We went one too far
n--;
printf("%u\n", sum);
printf("%u\n", n);
}
大家好 Stack Overflow!
所以我卡在了这个提示上:
"Use a loop to determine the value of n that will produce the largest n! value that could be stored in an unsigned short variable. Print out the maximum value for an unsigned short variable, the value of n that will produce the largest n! that is less than or equal to the maximum value for an unsigned short variable. NOTE: The constant USHRT_MAX contained in limits.h provides the maximum value for an unsigned short variable."
我猜,对于上述提示,它解决了为什么当我在程序中输入一个整数(例如 34)时,我得到 0 作为 34 的阶乘的输出。
判断n的代码我已经写好了!到目前为止输入 n 时的值,但是这个新部分让我感到困惑。
我认为这不会有帮助,但这是出现此提示之前的代码:
#include <stdio.h>
unsigned long Factorial(unsigned int n);
int main(void)
{
int num[11],i=0,factorials[11];
printf("Please enter up to 10 integers (q to quit): ");
//Ask for integers until a 'q' is entered and store integer values entered into array 'num'
while (scanf("%u",&num[i]))
{
//Store the factorial of integers entered into array 'factorials'
factorials[i]=Factorial(num[i]);
//Print numbers out in a two column table
printf("%5u %9u\n",num[i],factorials[i]);
i++;
}
return 0;
}
//Calculates the factorial of 'n'
unsigned long Factorial(unsigned int n)
{
int nFactorial,i=1;
nFactorial=n;
while (i<n)
{
nFactorial=nFactorial*i;
i++;
}
n=nFactorial;
}
无论如何,如果有人能提供帮助,我将不胜感激!我知道这听起来像是一个高难度的问题,所以即使是指针也能提供帮助!
谢谢任何人!
干杯,威尔。
编辑:如果我的代码难以阅读,我提前道歉,我正在努力让它变得更好
编辑: 到目前为止,我想出了这个来回答提示,但它似乎不对。输出值为 8...
//Detemine largest max value of n
for (i=0;Factorial(i)<=USHRT_MAX;i++);
printf("The max value for an unsigned short is %u\n Max value of n: %u\n",USHRT_MAX,i-1);
return 0;
由于您使用的是短函数,因此可以将阶乘和存储在更大的类型中,例如 unsigned long int
。
int main(void)
{
unsigned long int sum = 1;
unsigned int n;
for( n = 1; sum < USHRT_MAX; n++ ) {
sum *=n;
}
printf("%lu\n", sum);
printf("%u\n", n);
}
这有点作弊,因为不能保证 long int
会比 short
大,但很有可能。您可以通过验证来缓解这种情况。
assert( sizeof(unsigned short) < sizeof(unsigned long int) );
non-cheating方法是检查你是否即将溢出。你会想要这样做,但你不能。
USHRT_MAX >= sum * n
sum * n
会溢出。相反,将两边除以 n
并检查。
USHRT_MAX / n >= sum
这将在 sum *= n
溢出之前停止。我们可以通过插入一些数字来验证。 USHRT_MAX = 23,n = 4 和总和 = 6...
23 / 4 >= 6
5 >= 6
请注意,这是整数除法,因此被截断了。这对我们的目的来说很好。
#include <stdio.h>
#include <limits.h>
int main(void)
{
unsigned short sum = 1;
unsigned int n;
for( n = 1; (USHRT_MAX / n) >= sum; n++ ) {
sum *=n;
}
// We went one too far
n--;
printf("%u\n", sum);
printf("%u\n", n);
}