寫一個(gè)方法1000的階乘

時(shí)間:2018-12-31 12:00:00 資料大全 我要投稿

寫一個(gè)方法1000的階乘

  答:C++的代碼實(shí)現(xiàn)如下:

  #include

  #include

  #include

  using namespace std;

  class longint {

  private:

  vector iv;

  public:

  longint(void) { iv.push_back(1); }

  longint& multiply(const int &);

  friend ostream& operator<<(ostream &, const longint &);

  };

  ostream& operator<<(ostream &os, const longint &v) {

  vector::const_reverse_iterator iv_iter = v.iv.rbegin();

  os << *iv_iter++;

  for ( ; iv_iter < v.iv.rend(); ++iv_iter) {

  os << setfill(’0′) << setw(4) << *iv_iter;

  }

  return os;

  }

  longint& longint::multiply(const int &rv) {

  vector::iterator iv_iter = iv.begin();

  int overflow = 0, product = 0;

  for ( ; iv_iter < iv.end(); ++iv_iter) {

  product = (*iv_iter) * rv;

  product += overflow;

  overflow = 0;

  if (product > 10000) {

  overflow = product / 10000;

  product -= overflow * 10000;

  }

  iv_iter = product;

  }

  if (0 != overflow) {

  iv.push_back(overflow);

  }

  return *this;

  }

  int main(int argc, char **argv) {

  longint result;

  int l = 0;

  if(argc==1){

  cout << “like: multiply 1000″ << endl;

  exit(0);

  }

  sscanf(argv[1], “%d”, &l);

  for (int i = 2; i <= l; ++i) {

  result.multiply(i);

  }

  cout << result << endl;

  return 0;

  }

【寫一個(gè)方法1000的階乘】相關(guān)文章:

1.學(xué)習(xí)英語(yǔ)的一個(gè)奇特方法

2.寫磨練的優(yōu)秀作文1000字

3.寫搶紅包的優(yōu)秀作文1000字

4.寫一個(gè)贊美秋天的句子

5.只有一個(gè)地球讀后感1000

6.遇見另一個(gè)自己1000字

7.遇見另一個(gè)自己作文1000

8.寫中文推薦信的思路方法