大数乘法
来源:互联网 发布:matlab 非线性最优化 编辑:程序博客网 时间:2024/05/22 18:55
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; background-color: rgb(255, 255, 255);">Description</span>
#include <iostream>#include <string.h> using namespace std; void multiply(const char *a,const char *b); int main(){ //cout<<"hicjiajia"<<endl; string num1,num2; // 初始状态用string来存储大数 cout<<"现在,来两个大数吧! "<<endl; cin>>num1>>num2; const char *p1=num1.c_str(); // 将string转为 const char * const char *p2=num2.c_str(); // 将string转为 const char * multiply(p1,p2); system("pause"); return 0;} void multiply(const char *a,const char *b){ int i,j,ca,cb,*s; ca=strlen(a); cb=strlen(b); s=(int *)malloc(sizeof(int)*(ca+cb)); //分配存储空间 for (i=0;i<ca+cb;i++) s[i]=0; // 每个元素赋初值0 for (i=0;i<ca;i++) for (j=0;j<cb;j++) s[i+j+1]+=(a[i]-'0')*(b[j]-'0'); for (i=ca+cb-1;i>=0;i--) // 这里实现进位操作 if (s[i]>=10) { s[i-1]+=s[i]/10; s[i]%=10; } char *c=(char *)malloc((ca+cb)*sizeof(char)); //分配字符数组空间,因为它比int数组省! i=0;while(s[i]==0) i++; // 跳过头部0元素 for (j=0;i<ca+cb;i++,j++) c[j]=s[i]+'0'; c[j]='\0'; for (i=0;i<ca+cb;i++) cout<<c[i]; cout<<endl; free(s);}
0 0
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- 大数乘法
- C#操作MsSQL进行大量数据的插入操作
- storm的并发
- Substring with Concatenation of All Words
- SQL语句优化
- PLSQL:[1]plsql中文乱码,显示问号
- 大数乘法
- storm开发经验分享
- 银联 支付宝sdk+demo+文档 支持64位
- mysql 大量数据处理问题
- mac下搭建SVN环境及常用命令
- struts2标签的迭代用法(<s:interator>)的简单用法
- mfc中使用bmp和png位图
- maven 安装本地包到nexus服务器
- hive优化之控制hive任务中的reduce数