Posted in 面试题 onNovember 21, 2012
答:C++的代码实现如下:
#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& operator vector::const_reverse_iterator iv_iter = v.iv.rbegin();
os for ( ; iv_iter os }
return os;
第52 页共59 页
}
longint& longint::multiply(const int &rv) {
vector::iterator iv_iter = iv.begin();
int overflow = 0, product = 0;
for ( ; 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 exit(0);
}
sscanf(argv[1], “%d”, &l);
for (int i = 2; i result.multiply(i);
}
cout return 0;
}
#include
#include
#include
using namespace std;
class longint {
private:
vector
public:
longint(void) { iv.push_back(1); }
longint& multiply(const int &);
friend ostream& operator };
ostream& operator vector
os for ( ; iv_iter os }
return os;
第52 页共59 页
}
longint& longint::multiply(const int &rv) {
vector
int overflow = 0, product = 0;
for ( ; 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 exit(0);
}
sscanf(argv[1], “%d”, &l);
for (int i = 2; i result.multiply(i);
}
cout return 0;
}
写一个方法1000的阶乘
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@