UVa 10106 乘积
来源:互联网 发布:延安农村淘宝招聘信息 编辑:程序博客网 时间:2024/05/16 09:52
思路:直接使用bign类型,重载相关操作就可以了。
注意:数组需要开够,不是想当然的输入数据的250位,结果最大是500位。否则会RE错
其他:运算结果是右值,右值只能被const引用指向。
Code:
#include<cstdio>#include<iostream>#include<cstring>#include<string>#define MAXN 500//题目里最多是250位,你没看到代码是i+j啊,所以结果最大是500啊!!!难过RE错误,还一直以为数组开够了 using namespace std;struct bign{ int s[MAXN],len; 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-1-i]-'0'; return *this; } bign operator*(const bign &b) const { bign c; c.len=len+b.len; for(int i=0;i<len;++i) for(int j=0;j<b.len;++j) c.s[i+j]+=s[i]*b.s[j]; for(int i=0;i<c.len-1;++i) { c.s[i+1]+=c.s[i]/10; c.s[i]%=10; } //if(!c.s[c.len-1]) c.len--;//最高位可能是0 while(c.len>1&&!c.s[c.len-1]) c.len--; //注意这里的c.len和上句中的,都不要忘了c. //这里用while而不用if的原因是,if不能处理输入如011这样的情况,虽然不知道题目会不会这样输入 //c.clean(); return c; } void clean() { while(len>1&&!s[len-1]) len--; } string str() const { string res=""; for(int i=0;i<len;++i) res=(char)(s[i]+'0')+res; if(res=="") res="\0"; return res; }};ostream& operator<<(ostream &out,const bign &x){ out<<x.str(); return out; }istream& operator>>(istream &in, bign &x) { string s; in>>s; x=s.c_str(); return in; }int main(){ bign a,b; while(cin>>a>>b) { //bign c=a*b; //cout<<c<<endl; cout<<(a*b)<<endl;//运算的结果是右值,不能传给非const引用 } //system("pause"); return 0;}
0 0
- UVA 10106 乘积
- UVa 10106 乘积
- UVa 10106 乘积
- 最大乘积 Uva 11059
- 最大乘积 Uva 11059
- UVa 11059 最大乘积
- UVa 11059 最大乘积(水)
- 最大乘积(Maximum Product,UVA 11059)
- UVa 11059 最大乘积(枚举)
- 例题7-2 最大乘积 UVa 11059
- 最大乘积(Maximum Product,UVa 11059)
- 最大乘积(Maximum Product,UVA 11059)
- UVa 11509 Maximum Product(最大乘积)
- UVA 11059 Maximum Product 最大乘积
- 最大乘积(Maximum Product, UVa 11059)
- UVa 11059----Maximum Product(最长上升子序列乘积)
- UVA 11542 Square 乘积是平方数(高斯消元)
- UVA 11059 Maximum Product最大乘积(暴力法)
- ZOJ-2512
- android手机获取手机号
- jsp表单提交(post提交方式)中文乱码解决方法
- 常见hash算法的原理
- 『算法学习笔记』13-15th day. 10道习题
- UVa 10106 乘积
- 编程之美系列: 3.9 重建二叉树
- 失败的产品历程
- 游戏服务器之连接接收
- lua中让log的模块开关更人性化
- rabbitmq rpc
- 构造散列函数
- 比较全面的gdb调试命令
- 查找----深入探索散列查找