求1000阶乘
用C编写的各种方法越多越好!精益求精!谢谢[ 本帖最后由 二0一一628 于 2011-7-14 10:48 编辑 ]
#include <vector> typedef unsigned long uint32_t; typedef unsigned long long uint64_t; struct foo { foo( uint32_t val ) { val_.push_back( val ); } foo& operator*=( uint32_t val ) { uint64_t tmp = 0; for( std::vector<uint32_t>::iterator itor=val_.begin(); itor!=val_.end(); ++itor ) { tmp += *itor * (uint64_t)val; *itor = tmp%1000000000; tmp /= 1000000000; } if( tmp != 0 ) val_.push_back( (uint32_t)tmp ); return *this; } friend std::ostream& operator<<( std::ostream& os, const foo& f ); private: std::vector<uint32_t> val_; }; #include <iostream> #include <iomanip> std::ostream& operator<<( std::ostream& os, const foo& f ) { os << f.val_.back(); char fillchar = os.fill( '0' ); for( std::vector<uint32_t>::const_reverse_iterator itor=f.val_.rbegin()+1; itor!=f.val_.rend(); ++itor ) os << std::setw(9) << *itor; os.fill( fillchar ); return os; } foo fact( uint32_t n ) { foo r = 1; for( uint32_t i=1; i<=n; ++i ) r *= i; return r; } int main( void ) { using namespace std; cout << fact(1000) << endl; return 0; }