1,2,3...n 相乘 n为手动输入值 怎么写啊

解决方案 »

  1.   

    int myValue=1;
    for(int i=1;i<n;i++)
    {
    myValue+=myValue*i;
    }
      

  2.   

    int Factorial( int number )
    {
      return number == 1 ? 1 : number * Factorial( number - 1 );
    }
      

  3.   

    sorry,应该是这样的            int value = 1;
                for (int i = 1; i < 11; i++)
                {
                    value = value * i;
                }
                MessageBox.Show(value.ToString());
      

  4.   

    int Factorial( int number )
    {
      return number == 1 ? 1 : number * Factorial( number - 1 );
    }--------------------
    高人就是妙
      

  5.   

    public static long factorial(int n)
            {
                if (n < 0)
                {
                    return -1;//无效
                }
                if (n > 255)
                {
                    return -2;//溢出
                }
                if (n == 0)
                {
                    return 0;
                }
                long Result = 1;
                for (int i = 1; i <= n; i++)
                {
                    Result = Result * i;
                }
                return Result;
            }
      

  6.   

    不好意思,修正一下        public static long factorial(int n)
            {
                if (n < 0)
                {
                    return -1;//无效
                }
                if (n > 20)
                {
                    return -2;//溢出
                }
                if (n == 0)
                {
                    return 0;
                }
                long Result = 1;
                for (int i = 1; i <= n; i++)
                {
                    Result = Result * i;
                }
                return Result;
            }
      

  7.   

    int Factorial( int number )
    {
      return number == 1 ? 1 : number * Factorial( number - 1 );
    }输入0的话,会如何?
      

  8.   

    lxcnn(过客)
    如果输入的值大于20而实际情况又要知道最后准确的结果,怎么办?(就是我非要这个程序能算出确切的数值)
      

  9.   

    总得有个范围啊,大的我就没研究了,楼主可以搜索一下大数乘法的资料,有很多的,还要考虑到算法或者看下这两个
    http://happycode.cn/cat_20/2007-03-21/22226.html
    http://topic.csdn.net/t/20050321/17/3868983.html
      

  10.   

    用斯特林公式算数目较大的情况
    下面的函数计算n的阶乘的近似算法,n越大,结果越精确,计算结果如下
    1000!=4.02e2567
    4000!=1.829e12673
    10000!=2.846e35659
    所需时间均为瞬间n<10结果不很准确
    #include<math.h>
    #define PI 3.1415926535
    void jc(int n,int *pIndex,double *pPa)
    {
        double s;
        if(n<=1)
        {
        *pIndex=0;
        *pPa=1.0;
        return;
        }
        s=0.5*log(2*PI*n)+n*(log(n)-1);
        s=s/log(10.0);
        *pIndex=(int)s;
        *pPa=pow(10,s-(int)s);
    }
    int main(int argc, char* argv[])
    {
            int n=1000,In;
            double Pa;
            jc(n,&In,&Pa);
            printf("%fe%d\n",Pa,In);
            getch();
            return 0;
    }
    //---------------------------------------------------------------------------
      

  11.   

    斯特林公式---  n! = sqrt(2*PI*n)*pow(n/e,n);其中e位自然对数常数此公式已经相当精确了,还要更精确的话可以用斯特林公式的高阶形式。
      

  12.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace Console
    {
        class Program
        {
            static void Main(string[] args)
            {            int i = Int32.Parse(System.Console.ReadLine());
                int k=1;
                for (int j = 1; j <= i; j++)
                {
                    k += k * j;
                }
                System.Console.WriteLine(k.ToString());
                System.Console.ReadLine();
               
            }
        }
    }
      

  13.   

    int Factorial( int number )
    {
    return number == 1 ? 1 : number * Factorial( number - 1 );
    }--------------
    完全没经过大脑
      

  14.   

    阶乘其实最简单,就看你的思路了,hehehe,什么大数什么递归什么循环,都是误人子弟的狗屁
      

  15.   

    n值过大肯定会溢出
    我原来做过一个大数阶乘的程序 仅供参考#include< iostream >
    #include<iostream>  
    using namespace std;
    void rebuild(int* &a,int n)                   //增加数组的元素个数函数
    {
     int *mid;
     mid=a;
     int i;
     a=new int [n+1];
     for(i=0;i<n;i++)
     {
      a[i]=mid[i];
     }
     a[n]=0;
    }
    int main()
    {
     int nodenum(1),i,j,carry(0),n,m(-1);   
     int *A=new int[2];
     A[0]=1;
     A[1]=0;
     cout<<"请输入您所要计算的阶乘数:"<<endl;
     cin>>n;
     for(i=1;i<=n;i++)
     {
      for(j=0;j<nodenum;j++)               //计算阶乘
      {
       if(j==m)
       {
       A[j]=A[j]*i;
       A[j]+=carry;                       //加进位
       carry=0;                          //加完进位后置为0
       m=-1;
       }
       else {A[j]=A[j]*i;}
       if(A[j]>=1000)
       {
        carry=A[j]/1000;                  //取进位
        A[j]=A[j]%1000;                  //取千位以后的数
       }
       if(carry>0)
       {
        m=j+1;
    if(j==nodenum-1)
    {nodenum++;rebuild(A,nodenum);}   //增加数组元数个数
       }
      }
     }
     cout<<A[nodenum-1]<<',';              //输出结果
     for(i=nodenum-1;i>=1;i--)
     {
      if(A[i-1]==0){cout<<0<<0<<0<<',';}
      else if(A[i-1]>=100){cout<<A[i-1]<<',';}
      else if(A[i-1]<=9){cout<<0<<0<<A[i-1]<<',';}
      else 
      {
       cout<<0<<A[i-1]<<',';
      }
     }
     return 0;
    }
      

  16.   

    using System;
    class c 
    {
        static void Main(string[] args)
        {
            string y = Console.ReadLine();
            long x = Int64.Parse(y);
            
            
            Console.WriteLine(JiSuan(x));
            Console.ReadLine();
        }    static long JiSuan(long x)
        {
            if (x == 1)
                return 1;
            else
                return x * JiSuan(x - 1);
        }
    }
      

  17.   

    Stirling逼近吧
    N! = sqrt(2*n*PI)*pow(n/e,n);
      

  18.   

    我已经用C做过一个n阶层算法的程序,无论用C++还是C#,思路是一样的,上面的程序我也没细看,但应该没问题.
    简单地说 就是数组的1个元素存放1个位的数字,逢10进1.
      

  19.   

    附上C的CODE#include<stdio.h>void main()
    {
    int n,i,j,a,temp,max;
    char c;
    do
    {
    int jc[1000]={1};
    printf("输入阶层数:n=");
    scanf("%d",&max);
    getchar();
    for(n=1;n<=max;n++)
    {
    for(i=0;i<1000;i++)
    {
    jc[i]=jc[i]*n;
    }
    for(i=0;i<1000;i++)
    {
    a=jc[i],jc[i]=0;
    for(j=i;a>0;j++)
    {
    jc[j]=a%10+jc[j];
    a=a/10;
    }
    }
    }
    for(i=999;i>=0;i--)
    {
    if(jc[i]!=0)
    {
    temp=i;
    break;
    }
    }
    printf("%d!=",max);
    for(i=temp;i>=0;i--)
    {
    printf("%d",jc[i]);
    }
    do
    {
    printf("\n是否继续?y/n");
    scanf("%c",&c);
    getchar();
    }while(c!='y'&&c!='Y'&&c!='n'&&c!='N');
    }while(c=='y'||c=='Y');
    }