您的位置:首页 > 其它

UVA10494大数类模板

2015-07-27 17:29 169 查看
做了几个题,重载了一下加法,乘法,除法,减法,虽然都不是很标准,但是那几道题目都A了,

这道题不用模板其实很好做,一边截取,一边做除法存下了,同时做mod就行了。

贴下我自己敲的模板和这道题的简单做法:

#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<set>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define LL long long
#define INT (1<<31)-1;//2147483647
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)
{
char s[maxn];
sprintf(s,"%d",num);
//  cout<<"GG"<<endl;
*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;
}
bign operator += (const bign &b)
{
*this=*this+b;
return *this;
}
bign operator * (const bign &b) const
{
bign c;
int flag=0;
if(b.s[b.len-1]==0||s[len-1]==0)
flag=1;
for(int i=0;i<b.len;i++)
{
bign temp;
temp.len=i;
for(int j=0,g=0;g||j<len;j++)
{
int x=g;
if(j<len) x=x+s[j]*b.s[i];
temp.s[temp.len++]=x%10;
g=x/10;
}
c+=temp;
}
if(flag)
c=0;
return c;
}
bign operator - (const bign &b) const
{
bign c=0;
c.len=0;
for(int i=0,g=0;g||i<b.len;i++)
{
int temp=s[i];
temp+=g;
if(temp>=b.s[i])
{
c.s[c.len++]=temp-b.s[i];
g=0;
}
else
{
c.s[c.len++]=temp+10-b.s[i];
g=-1;
}
}
return c;
}
bign operator /(const int &b) const
{
bign c;
c.len=0;
LL temp=0;
int num[maxn];
int llen=0;
memset(num,0,sizeof(num));
int i=len-1;
while(i>=0)
{
temp=temp*10+s[i];
if(temp>=b)
break;
i--;
}
temp=(temp-s[i])/10;
for(;i>=0;i--)
{
temp=temp*10+s[i];
if(temp>=b)
{
num[llen++]=temp/b;
temp=temp%b;
}
else
{
num[llen++]=0;
}
}
for(int i=0;i<llen;i++)
c.len=llen;
for(int i=0;i<llen;i++)
c.s[i]=num[llen-i-1];
return c;
}
bool operator < (const bign &b) const
{
if(len!=b.len) return len<b.len;
for(int i=len-1;i>=0;i--)
if(s[i]!=b.s[i]) return s[i]<b.s[i];
return 0;
}
bool operator >(const bign &b) const
{
return b<*this;
}
bool operator >=(const bign &b) const
{
return !(*this<b);
}
bool operator == (const bign &b) const
{
return !(*this<b)&&!(b<*this);
}
void clean()
{
while(len>0&&s[len-1]==0) len--;
}
};
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;
}
int main()
{
bign a;
int b;
char s;
while(cin>>a>>s>>b)
{
bign temp=b;
if(b==1)
{
if(s=='%')
cout<<'0'<<endl;
if(s=='/')
cout<<a<<endl;
}

else if(a<temp)
{
if(s=='/')
cout<<0<<endl;
if(s=='%')
cout<<a<<endl;
}
else
{
if(s=='/')
cout<<(a/b)<<endl;
else
{
bign temp=10;
bign tt=a-a/b*b;
tt.clean();
cout<<tt<<endl;
}
}
}
return 0;
}
#include <cstdio>
#include <cstring>
const int maxsize=10000000;
char s[maxsize],ans[maxsize];
int main()
{
char c;
long long b,a;
memset(s,0,maxsize);
while(scanf("%s %c %lld",s,&c,&b)!=EOF)
{
memset(ans,0,maxsize);
int i;
a=0;
int j=0;
int len=strlen(s);
for(i=0;i<len;++i)
{
a=a*10+s[i]-'0';
ans[j++]=a/b+'0';
a=a%b;
}
if(c=='%')
printf("%lld\n",a);
else
{
i=0;
while(ans[i]=='0')++i;
if(ans[i]==0)--i;
for(;i<=j-1;++i)
printf("%c",ans[i]);
printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: