我无法想象如何构建这样的算法.
算法"对于N个元素的每个排列,蛮力推动旅行商问题,其中边缘由元素的顺序决定"具有这样的复杂性吗?
我需要使用堆栈计算 n 的阶乘,并且我编写的代码不返回任何结果。我也不知道 pop stack 真正做了什么(它的第二个参数是什么)所以我只是在那里使用了一个随机值。我使用int **x;
是因为我不知道该放什么pop(&mystack,*x);
。
#include <iostream>
using namespace std;
int n;
int aux;
int aux1;
int aux2;
int **x;
typedef struct {
int content[100];
int top;
} stack;
stack mystack;
int push(stack *somestack,int somevalue)
{
if (somestack->top+1>=100)
return 1;
(*somestack).top++;
(*somestack).content[(*somestack).top]=somevalue;
return 0;
}
int pop(stack *somestack, int *oldvalue)
{
if((*somestack).top==0)
{
return 1;
}
*oldvalue=(*somestack).content[(*somestack).top];
return 0;
}
int main()
{
cout<<"n=";
cin>>n;
push(&mystack,n);
int direction=1;
while(mystack.top>=1)
{
if((direction==1)&&(mystack.content[mystack.top]>1))
{
aux=mystack.content[mystack.top];
push(&mystack,aux-1); …
Run Code Online (Sandbox Code Playgroud) double factorial(int x) {
if (x >= 1)
return x * factorial(x - 1);
else
return 1;
}
Run Code Online (Sandbox Code Playgroud)
当我试图获得1000000的阶乘时,它会导致分段错误.请任何人都可以帮助我?
我的Java代码应该让用户输入一个数字,然后计算该数字的阶乘,我需要使用"for loop"当我输入数字5时,它告诉我,当它应该是120时,阶乘是6.我试图观察分解循环的教程,但它们不会工作,我认为它是因为我有"do"命令从调用中获取值
这是代码:
static Scanner kboard = new Scanner(System.in); //variable to read in values
public static void main(String[] args) {
int choice = 0;
String dummy = "";
String forename = "";
String surname = "";
int number = 0;
do {
System.out.println("1. display the user name, 2. calculate factorial, 3. exit");
choice = kboard.nextInt();
dummy = kboard.nextLine(); //strips out the return
if (choice == 1) {
forename = getforename();
surname = getsurname();
displaydetails(forename, surname);
}
if (choice == 2) …
Run Code Online (Sandbox Code Playgroud) 我知道有多种方法可以计算整数的阶乘,也有一个math
模块.但是我试图将一个返回错误结果的简单函数放在一起.我很想知道这里出了什么问题.例如,如果我将2作为参数传递它返回3,如果3则返回8.
>>>def factorial(n):
if n > 0:
result = n * n-1
factorial(n-1)
return result
>>>factorial (2)
3
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
import java.util.Scanner;
public class factorial {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
int x = 0, y;
System.out.println("enter factorial number");
if(in.hasNextInt()){
x = in.nextInt();// check if the scanner's next token is an int
}//end if
y = factorial(x);
System.out.println("factorials is "+y);
}//end main
private static int factorial(int n) {
// TODO Auto-generated method stub
if(n==0||n==1){
return 1;
}
else
return n*factorial(n-1);//recursive call
}//end factorial
}//end class
Run Code Online (Sandbox Code Playgroud) 这是我的代码 这在输出控制台上给出了 30 次堆栈溢出错误
fun main(args:Array<String>){
var no:Int=Integer.parseInt(readLine())//read input from user and convert to Integer
var ans:Int=calculateFact(no) //call function and store to ans variable
println("Factorial of "+no+" is "+ans) //print result
}
fun calculateFact(no:Int):Int //function for recursion
{
if(no==0) {
return 1 }
return (no*calculateFact(no))
}
Run Code Online (Sandbox Code Playgroud)
我不知道什么是错误解决plz
#function
def fact(x):
a = 1
b = 1
if x == 0:
a = 1
if x < 0:
print('enter valid whole number!')
if x > 0:
while b < x:
a = a * b
b += 1
yield a
#main
z = input('Enter a number')
g = (fact(n) for n in range (0,int(z)))
print(next(g))
Run Code Online (Sandbox Code Playgroud)
当我运行上述程序时,输出在这样的某个块处显示一个生成器对象:
#function
def fact(x):
a = 1
b = 1
if x == 0:
a = 1
if x < 0:
print('enter valid …
Run Code Online (Sandbox Code Playgroud) 我读了下面的代码,但我不明白它是什么意思"||" 在这方面:
function factorial(numero) {
numero = numero || 1
return numero * factorial(numero - 1)
}
Run Code Online (Sandbox Code Playgroud)
我理解逻辑运算符,但是如果传递任何参数,我没有找到调用函数的意义.这就是为什么我的问题的原因.
下面是代码,请指出我错在哪里.我已声明,定义了这个功能,我不知道出了什么问题.
#include<stdio.h>
int factorial(int b); /* Declaration */
int main()
{
int num;
printf("Enter number: ");
scanf("%d", &num);
printf("%d",factorial(num));
return 0;
}
int factorial(int b) /*Function definition*/
{
return b*factorial(b-1);
}
Run Code Online (Sandbox Code Playgroud) 什么是最快的算法和代码实现来计算以下表达式的值?
N!/(q!)r
我的代码
public static double timesbyf(int n,int q,int qt,int qp1,int qp1t)
{
int totaltimes=qt+qp1t;
double ans=1.0d;
for(int i=1;i<=totaltimes;i++)
{
if(i<=qt)
{
for(int j=q;j>0;j--)
{
ans=ans*((double)n/(double)j);
n--;
}
}
else
{
for(int j=qp1;j>0;j--)
{
ans=ans*((double)n/(double)j);
n--;
}
}
}
while(n>0)
{
ans=(ans*n)%3046201;
n--;
}
return ans;
}
Run Code Online (Sandbox Code Playgroud)
也就是说,n!
除以q!
r
时间.
我给了n≤3×10 6并且q <n,并且保证(q!)r将干净地划分n!.
我写了一个没有给出正确结果的程序.
main()
{
int i=1,n,s=1;
printf("enter the value of n");
scanf("%d",&n);
while(i<=n)
{
s=s*i;
i++;
if (i==n+1)
{
break;
}
}
printf("factorial of n=",s);
}
Run Code Online (Sandbox Code Playgroud)
它给出的结果如下图所示.
我正在学习 Java 脚本,有一个关于获取用户输入的数字的阶乘的练习,但由于某种原因我总是得到答案是 = 1
这是我的代码:
<SCRIPT>
function factorial(num){
for (n=1; n<=num; n++){
return fact*n;
}
}
var myNum, fact;
myNum = parseFloat(window.prompt('Enter positive integer : ',''));
fact = 1;
document.write('the factorial of the number is = '+ factorial(myNum));
</SCRIPT>
Run Code Online (Sandbox Code Playgroud)