运算模板
#include<string>
#include<iostream>
#include<string.h>
using namespace std;
const int MAXN = 10000;
struct BigInteger {
int digit[MAXN];
int length;
BigInteger();
BigInteger(int x);
BigInteger(string str);
BigInteger(const BigInteger &b);
BigInteger operator=(int x);
BigInteger operator=(string str);
BigInteger operator=(const BigInteger &b);
bool operator<=(const BigInteger &b);
bool operator==(const BigInteger &b);
BigInteger operator+(const BigInteger &b);
BigInteger operator-(const BigInteger &b);
BigInteger operator*(const BigInteger &b);
BigInteger operator/(const BigInteger &b);
BigInteger operator%(const BigInteger &b);
friend istream operator>>(istream &in, BigInteger &x);
friend ostream operator<<(ostream out, const BigInteger &x);
};
istream &operator>>(istream &in, BigInteger &x) {
string str;
in >> str;
x = str;
return in;
}
ostream &operator<<(ostream &out, const BigInteger &x) {
for (int i = x.length - 1; i >= 0; i--) {
out << x.digit[i];
}
return out;
}
BigInteger::BigInteger() {
memset(digit, 0, sizeof(digit));
length = 0;
}
BigInteger::BigInteger(int x) {
memset(digit, 0, sizeof(digit));
length = 0;
if (x == 0) {
digit[length++] = x;
}
while (x != 0) {
digit[length++] = x % 10;
x /= 10;
}
}
BigInteger::BigInteger(string str) {
memset(digit, 0, sizeof(digit));
length = str.size();
for (int i = 0; i < length; i++) {
digit[i] = str[length - i - 1] - '0';
}
};
BigInteger::BigInteger(const BigInteger &b) {
memset(digit, 0, sizeof(digit));
length = b.length;
for (int i = 0; i < length; i++) {
digit[i] = b.digit[i];
}
}
BigInteger BigInteger::operator=(int x) {
memset(digit,0,sizeof(digit));
length=0;
if(x==0) digit[length++]=x;
while(x!=0){
digit[length++]=x%10;
x/=10;
}
return *this;
}
BigInteger BigInteger::operator=(string str) {
memset(digit,0,sizeof(digit));
length=str.size();
for(int i=0;i<length;i++){
digit[i]=str[length-i-1]-'0';
}
return *this;
}
BigInteger BigInteger::operator=(const BigInteger& b){
memset(digit,0,sizeof(digit));
length=b.length;
for(int i=0;i<length;i++){
digit[i]=b.digit[i];
}
return *this;
}
bool BigInteger::operator<=(const BigInteger& b) {
if(length<b.length) return true;
else if(b.length<length) return false;
else{
for(int i=length-1;i>=0;--i){
if(digit[i]==b.digit[i]) continue;
else return digit[i]<b.digit[i];
}
}
return true;
}
bool BigInteger::operator==(const BigInteger& b){
if(length!=b.length) return false;
else {
for(int i=length-1;i>=0;--i){
if(digit[i]!=b.digit[i]){
return false;
}
}
}
return true;
}
BigInteger BigInteger::operator+(const BigInteger& b){
BigInteger answer;
int carry=0;
for(int i=0;i<length||i<b.length;i++){
int current=carry+digit[i]+b.digit[i];
carry=current/10;
answer.digit[answer.length++]=current%10;
}
if(carry!=0){
answer.digit[answer.length++]=carry;
}
return answer;
}
BigInteger BigInteger::operator-(const BigInteger& b) {
BigInteger answer;
int carry=0;
for(int i=0;i<length;++i){
int current=digit[i]-b.digit[i]-carry;
if(current<0){
current+=10;
carry=1;
}
else carry=0;
answer.digit[answer.length++]=current;
}
while(answer.digit[answer.length]==0&&answer.length>1){
answer.length--;
}
return answer;
}
BigInteger BigInteger::operator*(const BigInteger& b){
BigInteger answer;
answer.length=length+b.length;
for(int i=0;i<length;++i){
for(int j=0;j<b.length;++j){
answer.digit[i+j]+=digit[i]*b.digit[j];
}
}
for(int i=0;i<answer.length;++i){
answer.digit[i+1]+=answer.digit[i]/10;
answer.digit[i]%=10;
}
while(answer.digit[answer.length]==0 && answer.length>1){
answer.length--;
}
return answer;
}
BigInteger BigInteger::operator/(const BigInteger&b){
BigInteger answer;
answer.length=length;
BigInteger remainder=0;
BigInteger temp=b;
for(int i=length-1;i>=0;--i){
if(!(remainder.length==1&&remainder.digit[0]==0)){
for(int j=remainder.length-1;j>=0;--j){
remainder.digit[j+1]=remainder.digit[j];
}
remainder.length++;
}
remainder.digit[0]=digit[i];
while(temp<=remainder){
remainder=remainder-temp;
answer.digit[i]++;
}
}
while(answer.digit[answer.length]==0 && answer.length>1){
answer.length--;
}
return answer;
}
BigInteger BigInteger::operator%(const BigInteger &b) {
BigInteger remainder=0;
BigInteger temp=b;
for(int i=length-1;i>=0;--i){
if(!(remainder.length==1 && remainder.digit[0]==0)){
for(int j=remainder.length-1;j>=0;j--){
remainder.digit[j+1]=remainder.digit[j];
}
remainder.length++;
}
remainder.digit[0]=digit[i];
while(temp<=remainder){
remainder=remainder-temp;
}
}
return remainder;
}
例题
华中科大复试上机题
描述
实现一个加法器,使其能够输出a+b的值。
输入描述:
输入包括两个数a和b,其中a和b的位数不超过1000位。
输出描述:
可能有多组测试数据,对于每组数据, 输出a+b的值。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
const int MAXN=100000;
struct BigInteger{
int digit[MAXN];
int length;
BigInteger();
BigInteger operator=(string str);
BigInteger operator+(const BigInteger& b);
friend istream& operator>>(istream& in,BigInteger& x);
friend ostream& operator<<(ostream& out,const BigInteger& x);
};
istream& operator>>(istream& in,BigInteger& x){
string str;
in>>str;
x=str;
return in;
}
ostream& operator<<(ostream& out,const BigInteger& x){
for(int i=x.length-1;i>=0;--i){
out<<x.digit[i];
}
return out;
}
BigInteger::BigInteger(){
memset(digit,0,sizeof(digit));
length=0;
}
BigInteger BigInteger::operator=(string str){
memset(digit,0,sizeof(digit));
length=str.size();
for(int i=0;i<length;++i){
digit[i]=str[length-i-1]-'0';
}
return *this;
}
BigInteger BigInteger::operator+(const BigInteger& b){
BigInteger answer;
int carry=0;
for(int i=0;i<length||i<b.length;++i){
int current=carry+digit[i]+b.digit[i];
carry=current/10;
answer.digit[answer.length++]=current%10;
}
if(carry!=0){
answer.digit[answer.length++]=carry;//最后仍有进位
}
return answer;
}
int main(){
BigInteger a,b;
while(cin>>a>>b){
cout<<a+b<<endl;
}
return 0;
}
清华复试上级题
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
//大数加法
string addString(string a,string b)
{
int carry=0;
string res;
int i=a.size()-1;
int j=b.size()-1;
while(i>=0 && j>=0)
{
int num=carry+a[i]-'0'+b[j]-'0';
res+=num%10+'0';
carry=num/10;
i--;j--;
}
while(i>=0)
{
int num=carry+a[i]-'0';
res+=num%10+'0';
carry=num/10;
i--;
}
while(j>=0)
{
int num=carry+b[j]-'0';
res+=num%10+'0';
carry=num/10;
j--;
}
if(carry>0)
res+=to_string(carry);
reverse(res.begin(),res.end());
return res;
}
//大数乘法(x<=10)
string mulString(string a,int x)
{
int carry=0;
string res;
for(int i=a.size()-1;i>=0;i--)
{
int num=carry+(a[i]-'0')*x;
res+=num%10+'0';
carry=num/10;
}
if(carry>0)
res+=to_string(carry);
reverse(res.begin(),res.end());
return res;
}
//组合大数乘法
string MulString(string a,string b)
{
string res;
for(int i=0;i<b.size();i++)
{
int x=b[b.size()-1-i]-'0';
string num=mulString(a,x);
for(int j=0;j<i;j++)
num=mulString(num,10);
res=addString(res,num);
}
return res;
}
int main()
{
int n;
while(cin>>n)
{
string res="1";
for(int i=2;i<=n;i++)
{
res=MulString(res,to_string(i));//to_string将数字常量转化为字符串
}
cout<<res<<endl;
}
return 0;
}
数字阶梯求和 哈工大复试上级
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string addString(string a,string b){
string res;
int carry=0;
int i=a.size()-1;
int j=b.size()-1;
while(i>=0&&j>=0){
int num=carry+a[i]-'0'+b[j]-'0';
res+=num%10+'0';
carry=num/10;
i--;j--;
}
while(i>=0){
int num=carry+a[i]-'0';
res+=num%10+'0';
carry=num/10;
i--;
}
while(j>=0){
int num=carry+b[j]-'0';
res+=num%10+'0';
carry=num/10;
j--;
}
if(carry>0) res+=to_string(carry);
reverse(res.begin(),res.end());
return res;
}
string mulString(string a,int x){//大整数乘法,x<10
int carry=0;
string res;
for(int i=a.size()-1;i>=0;i--){
int num=carry+(a[i]-'0')*x;
res+=num%10+'0';
carry=num/10;
}
if(carry>0) res+=to_string(carry);
reverse(res.begin(),res.end());
return res;
}
string myAdd(int a,int n){//类似快速幂
string res="0";
string temp=to_string(a);
for(int i=0;i<n;i++){
res=addString(res,temp);
temp=mulString(temp, 10);
temp=addString(temp,to_string(a));
}
return res;
}
int main(){
int a,n;
while(cin>>a>>n){
cout<<myAdd(a,n)<<endl;
}
return 0;
}
大整数的因子 北京大学复试
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int divideString(string a,int x){
int remainder=0;
for(int i=0;i<a.size();i++){
int num=remainder*10+a[i]-'0';
a[i]=num/x+'0';
remainder=num%x;
}
if(remainder) return remainder;
return 0;
}
int main(){
string c;
while(cin>>c){
if(c=="-1") break;
int sum=0;
for(int i=2;i<10;i++){
if(divideString(c, i)==0){
printf("%d ",i);
sum++;
}
}
if(!sum) printf("none\n");
else printf("\n");
}
return 0;
}