大数问题
来源:互联网 发布:税务师考试含金量 知乎 编辑:程序博客网 时间:2024/05/17 08:56
// 实数加减法#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,0,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[],int fax,int b){ int i,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],0,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],Max,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;}
- 【大数问题】大数阶乘
- 大数问题
- 大数问题
- 大数问题
- 大数问题
- 大数问题
- 大数问题
- 大数问题 *
- 大数问题
- 大数问题
- 大数问题
- 大数问题
- 大数问题
- 大数问题
- 大数问题
- 大数问题
- 大数问题
- 大数问题
- 黑马程序员——抽象类的理解
- 关于gpstk空间名封装的问题
- Struts2拦截器 —— prepare拦截器
- tomcat 输入ip可以直接访问项目的方法
- Ubuntu下QT的安装详细教程
- 大数问题
- iOS7下status bar相关问题的解决方法
- Mysql的“Table 'mysql.servers' doesn't exist”的解决方法
- QT实现浏览器插件 调用及回调 (三)
- svn命令行命令
- cache-policies.txt
- 启动U盘更换背景图片和图标的方法
- 值得学习的网址收藏
- Struts2文件上传 —— 单个文件上传