这次是高精除高精 !!!
其实没有非常大的变化
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n,a[501],b[501],c[501],ans,i; void init(int a[])//定义init函数,将数据存入 { string s; cin>>s; a[0]=s.length(); for(int i=1;i<=a[0];i++) a[i]=s[a[0]-i]-'0'; } void print(int a[])//输出函数 { int i; if(a[0]==0) { cout<<0<<endl; return; } for(int i=a[0];i>0;i--) cout<<a[i]; cout<<endl; return; } int compare (int a[],int b[])//进行比较 若a[0]>b[0]返回1否则返回-1(a[0]!=b[0]) { int i; if(a[0]>b[0]) return 1; if(a[0]<b[0]) return -1; for(int i=a[0];i>0;i--)//若a[i]>b[i]返回1否则返回-1(a[i]!=b[i]) { if(a[i]>b[i]) return 1; if(a[i]<b[i]) return -1; } return 0; } void jian(int a[],int b[]) { int flag,i; flag=compare(a,b); if(flag==0) { a[0]=0; return ; } if(flag==1) { for(int i=1;i<=a[0];i++) { if(a[i]<b[i]) { a[i+1]--; a[i]+=10; } a[i]-=b[i]; } while(a[0]>0&&a[a[0]]==0) a[0]--; return ; } } void numcpy(int p[],int q[],int det) { for(int i=1;i<=p[0];i++) q[i+det-1]=p[i]; q[0]=p[0]+det-1; } void chugao(int a[],int b[],int c[])//求值 { int i,tmp[501]; c[0]=a[0]-b[0]+1; for(int i=c[0];i>0;i--) { memset(tmp,0,sizeof(tmp)); numcpy(b,tmp,i); while(c[0]>0&&c[c[0]]==0) c[0]--; return ; } } int main() { init(a); init(b); chugao(a,b,c); print(c); return 0; }
给大家一道好玩的题目:
洛谷P1932
a , b = int(input()),int(input()) print (a + b) print (a - b) print (a * b) print (a // b) print (a % b) //最好玩的程序语言了,笑
正解:
#include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<ctime> using namespace std; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } struct BigInteger { static const int BASE=100000000; //高进制 static const int WIDTH=8; //高进制位数 vector<long long>s; BigInteger() { //构造函数:初始赋0 *this=0; } BigInteger(const int& num) { // 构造函数 *this=num; } //赋值 BigInteger operator = (int num) { s.clear(); do { s.push_back(num%BASE); num/=BASE; } while(num>0); return *this; } BigInteger operator = (const string& str) { s.clear(); int x,len=(str.length()-1)/WIDTH+1; for(int i=0; i<len; i++) { int end=str.length()-i*WIDTH; int start=max(0,end-WIDTH); sscanf(str.substr(start,end-start).c_str(),"%lld",&x); s.push_back(x); } return *this; } //比较 bool operator < (const BigInteger& b) { if(s.size()<b.s.size())return true; if(s.size()>b.s.size())return false; for(int i=s.size()-1; i>=0; i--) { if(s[i]<b.s[i])return true; if(s[i]>b.s[i])return false; } return false; } bool operator >= (const BigInteger& b) { return !(*this<b); } bool operator == (const BigInteger& b) { if(s.size()!=b.s.size())return false; for(int i=0; i<s.size(); i++) if(s[i]!=b.s[i])return false; return true; } //高精加哦 BigInteger operator + (const BigInteger& b) { BigInteger c; c.s.clear(); for(int i=0,g=0; ; i++) { if(g==0&&i>=s.size()&&i>=b.s.size())break; int x=g; if(i<s.size())x+=s[i]; if(i<b.s.size())x+=b.s[i]; c.s.push_back(x%BASE); g=x/BASE; } return c; } //高精减 BigInteger operator - (const BigInteger& b) { BigInteger c; c=*this; for(int i=0; i<c.s.size(); i++) { int tmp; if(i>=b.s.size())tmp=0; else tmp=b.s[i]; if(c.s[i]<tmp) { c.s[i+1]-=1; c.s[i]+=BASE; } c.s[i]-=tmp; } while(c.s.back()==0&&c.s.size()>1)c.s.pop_back(); return c; } void operator -= (const BigInteger& b) { *this=*this-b; } //高精乘 BigInteger operator * (const BigInteger& b) { BigInteger c; c.s.resize(s.size()+b.s.size()); for(int i=0; i<s.size(); i++) for(int j=0; j<b.s.size(); j++)c.s[i+j]+=s[i]*b.s[j]; for(int i=0; i<c.s.size()-1; i++) { c.s[i+1]+=c.s[i]/BASE; c.s[i]%=BASE; } while(c.s.back()==0&&c.s.size()>1)c.s.pop_back(); return c; } friend istream& operator >> (istream& input,BigInteger& x) { string s; if(!(input>>s))return input; x=s; return input; } friend ostream& operator << (ostream& output,const BigInteger& x) { output<<x.s.back(); for(int i=x.s.size()-2; i>=0; i--) { char buf[20]; sprintf(buf,"%08d",x.s[i]); for(int j=0; j<strlen(buf); j++)output<<buf[j]; } return output; } }; //高精除(%,余数啦) BigInteger Copy(const BigInteger& b,int x) { BigInteger t; t.s.resize(b.s.size()+x); for(int i=0; i<b.s.size(); i++)t.s[i+x]=b.s[i]; return t; } BigInteger Divide(const BigInteger& a,const BigInteger& b,BigInteger& mod) { BigInteger c; c.s.resize(a.s.size()-b.s.size()+1); mod=a; int Pow[(int)log2(BigInteger::BASE)+5]; Pow[0]=1; for(int i=1; i<=log2(BigInteger::BASE); i++)Pow[i]=Pow[i-1]*2; for(int i=c.s.size()-1; i>=0; i--) { BigInteger t; t=Copy(b,i); for(int j=log2(BigInteger::BASE); j>=0; j--) if(mod>=t*Pow[j]) { c.s[i]+=Pow[j]; mod-=t*Pow[j]; } } while(c.s.back()==0&&c.s.size()>1)c.s.pop_back(); return c; } BigInteger a,b; int main() { cin>>a>>b; if(a<b)cout<<a+b<<endl<<'-'<<b-a<<endl<<a*b<<endl<<0<<endl<<a<<endl; else { BigInteger c,d; c=Divide(a,b,d); cout<<a+b<<endl<<a-b<<endl<<a*b<<endl<<c<<endl<<d<<endl; } return 0; }//其实不看我的也行啦