以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  用C++编写的阶乘  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=45031)


--  作者:diejia
--  发布时间:4/7/2007 8:38:00 PM

--  用C++编写的阶乘
下面是我用C++编写的一个阶乘问题,主要是为了解决大数阶乘导致结果太大而无法保存,例如50的阶乘至少有60位,而int,float都不能保存这么大的数。大家看看我的程序有什么缺点吗?因为我也不知道50阶乘到底是多少,不知道我的程序算不算正确。
#include <iostream>
using namespace std;

#define MAX 25
void Numplus(int plus[],int num)
{
   int count,sum[MAX],ph;
       for(int j=0;j<MAX;j++)
        {
                 sum[j] = 0;
          }
        if (num == 1)
            {
             plus[0]=1;
               
             }
         else
           {
             for(int i=0;i<MAX;i++)
                  {
                    count = plus[i]*num;
                       if(count>999)
                         {
                             plus[i]  = count%1000;
                             sum[i+1] =  count/1000;
                           }
                        else
                         {
                               plus[i] = count;  
                           }
                       }
               for(int m=0;m<MAX;m++)
                {
                     ph=plus[m]+sum[m];   
                     if(ph<=999)
                     {
                         plus[m]=ph;
                      }
                     else if(ph>999)
                      {
                         plus[m] = ph%1000;
                         sum[m+1]=sum[m+1]+(ph/1000);     
                        }
                 }


               }

}
int main (int argc, void* argv[])
{
    int n,num;
    int static plus[MAX];
    cout<<"input plus number:";
    cin>>n;
    for(int i=0;i<MAX;i++)
       {
          plus[i] = 0;
        }
    for(num=1;num<(n+1);num++)
        {
           Numplus(plus,num);//计算阶乘
       
         }
     //输出阶乘
    for(int j=MAX-1;j--;j>=0)
        {
           if((plus[j] == 0)&&(plus[j-1] != 0))
                 {
                  num=j-1;
                 
                  break;
                   }
            else continue;
         }
      cout<<"The plus of "<<n<<" is:";
       
      for(int t=num;t>=0;t--)
          {
                if(plus[t] == 0)
                      {
                        cout<<"000";
                       }
                 else if((plus[t] <= 999)&&(plus[t] > 99))
                       {
                           cout<<plus[t];
                        }
                 else if((plus[t] <= 99)&&(plus[t] > 9))
                        {
                            if(t==num)
                                     {
                                 cout<<plus[t];
                                      }
                             else
                                       {
                                     cout<<"0";
                                     cout<<plus[t];
                                        }   
                          }
                  else if((plus[t]<= 9)&&(plus[t] >= 1))
                          {
                            if(t==num)
                                     {
                                 cout<<plus[t];
                                      }
                            else
                                    {
                                   cout<<"00";
                                   cout<<plus[t];  
                                    }  
                           }
             }
               cout<<endl;

}


--  作者:byy2122
--  发布时间:5/4/2007 11:49:00 AM

--  
我也这两天刚学C++,不大清楚,只是觉得主函数太长,不大好!
--  作者:pingtian
--  发布时间:5/5/2007 3:26:00 PM

--  
输出部分定义成函数吧!
--  作者:卷积内核
--  发布时间:5/6/2007 9:56:00 AM

--  
http://www.ieee.org.cn/dispbbs.asp?boardID=61&replyID=51008&ID=35480&skin=1

参考一下这里,算多少位都可以。


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms