大数问题
发布时间:2020-12-14 03:56:23 所属栏目:大数据 来源:网络整理
导读:// 实数加减法#includecstdio#includeiostream#includealgorithmusing namespace std;string rever(string a){ string b; for(int i = 0;ia.size();i++) b = a[i] + b; return b;}int main(){ // freopen("input.txt","r",stdin); int n,m; string a,b; whil
// 实数加减法 #include<cstdio> #include<iostream> #include<algorithm> using namespace std; string rever(string a) { string b; for(int i = 0;i<a.size();i++) b = a[i] + b; return b; } int main() { // freopen("input.txt","r",stdin); int n,m; string a,b; while(cin>>a>>b) { int i = 0,j = 0; while(a[i++]!='.'&&i<=a.size()); if(i <= a.size()) { n = a.size()-i; a.erase(a.begin()+i-1); } else n = 0; // cout<<a<<" "<<b<<endl; while(b[j++]!='.'&&j<=b.size()); if(j <= b.size()) { m = b.size()-j; b.erase(b.begin()+j-1); } else m = 0; int mmax = max(n,m); while(n<m){ a = a + '0'; n++; } while(m<n){ b = b + '0'; m ++; } int g; a = rever(a); b = rever(b); char c[100008]; int cn = 0; string s=""; // puts("in"); for(i=0,g=0;g||i<max(a.size(),b.size());i++) { int x = g; if(i<a.size())x+=a[i]-'0'; if(i<b.size())x+=b[i]-'0'; c[cn++] = x%10 +'0' ; s = c[cn-1] + s; g = x/10; } s.insert(s.end()-mmax,'.'); for( i=s.size()-1;i>=0;i--) if(s[i]>'0'&&s[i]<='9') break; else{ char cs = s[i]; s.erase(s.begin()+i); if(cs == '.')break; } cout<<s<<endl; } } 大数加法: #include<string> #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> using namespace std; const int maxn = 1000; struct bign { int len,s[maxn]; bign() { memset(s,sizeof(s)); len = 1; } bign operator = (const char *num) { len = strlen(num); for(int i=0;i<len;i++)s[i] = num[len-i-1] - '0'; return *this; } bign operator = (int num) { // cout<<"in"<<endl; char s[maxn]; sprintf(s,"%d",num); *this = s; return *this; } bign(int num){*this = num;} bign(const char *num){ *this = num ;} string str()const { string res = ""; for(int i=0;i<len;i++) res = (char)(s[i] + '0') + res; if (res == "")res = "0"; return res; } bign operator + (const bign& b)const { bign c; c.len = 0; for(int i=0,g = 0;g|| i<max(len,b.len);i++) { int x = g; if(i<len) x+= s[i]; if(i<b.len) x+=b.s[i]; c.s[c.len++] = x%10; g = x/10; } return c; } void clean() { while(len > 1 && !s[len-1]) len--; } bign operator += (const bign& b) { *this = *this + b; return *this; } }; istream& operator >>(istream &in,bign& x) { string s; in>>s; x = s.c_str(); return in; } ostream& operator << (ostream &out,const bign& x) { out << x.str(); return out; } bign f[1002]; int main() { for(int i=0;i<1000;i++) f[i] = 0; f[1] = 1; f[0] = 1; for(int i=2;i<=1000;i++) f[i] = f[i-1]+f[i-2]; int m; while(cin>>f[1]>>f[2]) { cout<<f[1]+f[2]<<endl; } }大数乘除法: #include<stdio.h> #include<cstring> #include<iostream> const int base = 10000; const int Max = 100; using namespace std; void Mul(int a[],int fax,int b) { int i,arr = 0; for(i = fax -1;i >= 0;i--) { arr +=b*a[i]; a[i] = arr%base; arr /= base; } } void Div(int a[],div = 0; for(int i=0;i<fax;i++) { div = div*base + a[i]; a[i] = div / b; div %= b; } } int main() { int n,m,i; int a[101][Max]; memset(a[1],Max*sizeof(int)); for( i=2,a[1][Max-1] =1;i<101;i++) { memcpy(a[i],a[i-1],Max*sizeof(int)); Mul(a[i],Max,4*i-2); Div(a[i],i+1); } while(cin>>n) { for( i=0;i<100&&a[n][i]==0;i++); cout<<a[n][i++]; for(;i<Max;i++) { printf("%04d",a[n][i]); } cout<<endl; } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Delphi调用C#写的webservice
- Delphi 2009 安装序列号
- .NET 2.0 - WinForm Control - DataGridView 编程36计(一)
- 创建一个哈希,其中键是一个数组,值是一个数组数组
- delphi – 是否有一些Indy最佳做法可以避免生成标记为垃圾邮
- 干货分享:史上最全大数据分析工具
- Delphi iOS & macOS App 名称支持国际化多语显示
- protoc-gen-lua Extensions 中只有repeated 导致 Crash
- haskell – 用于将数据处理表示为管道的框架
- delphi – 菜单项是否可以接收OnClick事件,即使它没有启用?