用BigInteger類,可以直接進行運算
解决方案 »
- 在方法中定义内部类
- 大家帮忙看看,我这个代码中,最后写的那个注释那个地方。为何一次选择,执行两次该事件??
- DES算法如果解决数据必须是8的倍数的限制
- 问一个java重写的问题
- 如何设置JFileChooser 打开的时候就是这个程序所在的类文件夹?
- 有关jdatastore的java application程序在用native executable builder生成windows下可执行程序报错的问题
- 菜鸟问题,关于对话框的,大家帮帮忙:)
- 一个包里文件引用包外文件的问题?
- 初学JAVA,总觉得界面很难定位,有什么技巧?
- 如何销毁一个object?
- 如何做一个程序的启动画面〉?
- 有关native调用的问题
我见过那贴子
你可以找一下
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;class l_int{
public:
vector<short> data;
l_int(){ data.push_back(0);} l_int(string str){
data.clear();
int j;
for(j=0;j<str.length();j++)
if (str[j]!='0') break; for(int i=str.length()-1;i>=j;i--)
data.push_back( int(str[i])-48 );
} void set_value(string str){
data.clear();
for(int i=str.length()-1;i>=0;i--)
data.push_back( int(str[i])-48 );
} l_int(int a){
data.clear();
do{
short k =a % 10;
a = int(a/10);
data.push_back(k);
}while(a>0);
} ~l_int(){
data.clear();
} l_int operator =(l_int a){
this->data = a.data;
return *this;
} bool operator==(l_int a){
if (this->data==a.data) return true;
return false;
} l_int operator +(l_int a);
l_int operator -(l_int a);
l_int operator *(l_int a);
l_int semi_mul(l_int a, short n, int seg);
int compare( l_int , l_int ); void print(){
vector<short>::iterator lst;
lst = data.end();
do{
lst--;
cout<<*lst;
}while(lst!=data.begin());
}
};l_int l_int::operator +(l_int a )
{
vector<short> data;
l_int b = *this; vector<short>::iterator lst,lst2; if (a.data.size()>b.data.size()) swap(a,b); lst = a.data.begin();
lst2 = b.data.begin();
data.clear();
while(lst!=a.data.end()){
data.push_back(*lst+*lst2); lst++;
lst2++;
} while(lst2!=b.data.end()){
data.push_back(*lst2);
lst2++;
} lst = data.begin();
while( lst!=data.end() ){
if (*lst>=10){
lst2 = lst+1;
if( lst2!=data.end() ) (*lst2)++;
else data.push_back(1); *lst -= 10;
}
lst++;
} l_int res;
res.data = data;
return res;
}l_int l_int::semi_mul(l_int a, short n, int seg)
{
vector<short>::iterator lst;
vector<short> data;
data.push_back(0); if ((n==0)||(a.data==data)){
a.data = data;
return a;
} for(lst = a.data.begin(); lst!=a.data.end();lst++)
(*lst) *= n; data.clear();
for( int i=0;i<seg;i++) data.push_back(0); int ad=0;
for(lst = a.data.begin();lst!=a.data.end(); lst++){ ad = ad+(*lst);
*lst = ad % 10;
ad = int(ad/10);
data.push_back(*lst);
}
if (ad!=0) data.push_back(ad); bool flag = true;
for(lst = data.begin(); lst!=data.end(); lst++)
if (*lst!=0){
flag = false;
break;
} if (flag) {
data.clear();
data.push_back(0);
}
a.data = data;
return a;
}l_int l_int::operator *( l_int a )
{
l_int res(0); vector<short>::iterator lst,lst2;
int seg = 0;
for(lst=a.data.begin();lst!=a.data.end();lst++){
res = res + semi_mul(*this , *lst ,seg);
seg++;
}
return res;
}//remind should return a positive integer;
l_int l_int::operator -(l_int a )
{
if ( compare(*this,a)==-1) cout<<" ERROR! "<<endl; vector<short>::iterator lst; for( lst = a.data.begin(); lst!= a.data.end(); lst++)
*lst *= -1; l_int res = *this + a ; for(lst = res.data.begin();lst!=res.data.end();lst++){
if (*lst<0){
*(lst+1) -=1;
*lst += 10;
}
} lst = res.data.end();
do{
lst--;
if( *lst == 0 )
lst = res.data.erase(lst);
}while(lst!=res.data.begin());
return res;
}//return 1 if bigger , 0 if equal , -1 if smaller
int l_int::compare(l_int a , l_int b)
{
int i = a.data.size();
int j = b.data.size(); if ( i>j )return 1;
else if ( i < j) return -1;
else {
vector<short>::iterator lst,lst2;
lst = a.data.end()-1;
lst2 = b.data.end();
do{
if ( *lst > *lst2) return 1;
else if( *lst<*lst2) return -1;
}while(lst!=a.data.begin());
}
return 0;
}int main()
{
string str;
char ch[1000];
/*while( cin.getline(ch,1000) ){
str = ch;
if(str=="") continue;
l_int a(str);
cin.getline(ch,1000);
str = ch;
l_int b(str); a = a*b;
a.print();
cout<<endl;
}*/
l_int a(9);
l_int r(1);
for(int i=0;i<1000;i++)
{
r = r*a;
r.print();
cout<<endl<<endl;
int j;
cout<<r.data.size()<<"!!!!"<<endl;
}
int j;
cin>>j;
return 0;
}