大数加法(主要是想记住模板)
模板可真是个好东西
题目:http://codeforces.com/gym/100735/problem/I?mobile=true
You are given three numbers. Is there a way to replace variables A, B and C with these numbers so the equality A + B = C is correct?
There are three numbers X1, X2 and X3 (1 ≤ Xi ≤ 10100), each on a separate line of input.
Output either "YES", if there is a way to substitute variables A, B and C with given numbers so the equality is correct, or "NO" otherwise.
1
2
3
YES
1
2
4
YES
1
3
5
NO
题意:从3个数中任意选3个数(可以重复选)满足a+b=c;
ps:数据特别大!(某些学了java的同学可真是让人讨厌呐)
ac代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<set> #include<algorithm> #include<map> #define maxn 200005 using namespace std; string sum(string s1,string s2) { if(s1.length()<s2.length()) { string temp=s1; s1=s2; s2=temp; } int i,j; for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--) { s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); //注意细节 if(s1[i]-'0'>=10) { s1[i]=char((s1[i]-'0')%10+'0'); if(i) s1[i-1]++; else s1='1'+s1; } } return s1; } int main() { string a[4]; for(int i=1;i<=3;i++) { cin>>a[i]; getchar(); } int flag=0; for(int i=1;i<=3;i++) { for(int j=1;j<=3;j++) { for(int k=1;k<=3;k++) { if(sum(a[i],a[j])==a[k]) flag=1; } } } if(flag==1)cout<<"YES"<<endl; else cout<<"NO"<<endl; }
然后再藏一下板子吧:
大数加法:博客链接:
http://blog.csdn.net/y990041769/article/details/19545179
string sum(string s1,string s2) { if(s1.length()<s2.length()) { string temp=s1; s1=s2; s2=temp; } int i,j; for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--) { s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); //注意细节 if(s1[i]-'0'>=10) { s1[i]=char((s1[i]-'0')%10+'0'); if(i) s1[i-1]++; else s1='1'+s1; } } return s;
}
大数乘以整数型:博客链接:http://blog.csdn.net/y990041769/article/details/12645953
string Multiply(string s,int x) //大数乘以整形数 { reverse(s.begin(),s.end()); int cmp=0; for(int i=0;i<s.size();i++) { cmp=(s[i]-'0')*x+cmp; s[i]=(cmp%10+'0'); cmp/=10; } while(cmp) { s+=(cmp%10+'0'); cmp/=10; } reverse(s.begin(),s.end()); return s; }
大数除以整形数:
string Except(string s,int x) //大数除以整形数 { int cmp=0,ok=0; string ans=""; for(int i=0;i<s.size();i++) { cmp=(cmp*10+s[i]-'0'); if(cmp>=x) { ok=1; ans+=(cmp/x+'0'); cmp%=x; } else{ if(ok==1) ans+='0'; //注意这里啊。才找出错误 } } return ans; }
大数乘法:http://poj.org/problem?id=2389
思想就是模拟乘法运算,用大数乘以另一个数的每一位然后大数相加就是ans
string sum(string s1,string s2) //大数加法 { if(s1.length()<s2.length()) { string temp=s1; s1=s2; s2=temp; } int i,j; for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--) { s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); //注意细节 if(s1[i]-'0'>=10) { s1[i]=char((s1[i]-'0')%10+'0'); if(i) s1[i-1]++; else s1='1'+s1; } } return s1; } string Mult(string s,int x) //大数乘以整形数 { reverse(s.begin(),s.end()); int cmp=0; for(int i=0;i<s.size();i++) { cmp=(s[i]-'0')*x+cmp; s[i]=(cmp%10+'0'); cmp/=10; } while(cmp) { s+=(cmp%10+'0'); cmp/=10; } reverse(s.begin(),s.end()); return s; } string Multfa(string x,string y) //大数乘法 { string ans; for(int i=y.size()-1,j=0;i>=0;i--,j++) { string tmp=Mult(x,y[i]-'0'); for(int k=0;k<j;k++) tmp+='0'; ans=sum(ans,tmp); } return ans; }
浮点数的n次方:博客链接:http://blog.csdn.net/y990041769/article/details/9262943
string Multiply(string s,long x) //大数乘以整形数 { reverse(s.begin(),s.end()); long cmp=0; for(int i=0; i<s.size(); i++) { cmp=(s[i]-'0')*x+cmp; s[i]=(cmp%10+'0'); cmp/=10; } while(cmp) { s+=(cmp%10+'0'); cmp/=10; } reverse(s.begin(),s.end()); return s; } string Remove_later(string s) //删除一个字符串的后倒0 { int ok=1; for(int i=s.size()-1; i>=0; i--) { if(s[i]=='0'){ s.erase(i); } else if(s[i]=='.') { s.erase(i); ok=0; } else ok=0; if(ok==0) break; } return s; } string factorial(string s,int n) //浮点数的n次方 { if(n==0) return "1"; string cmp="",count=""; long x=0,point=0; for(int i=0; i<s.size(); i++) if(s[i]!='.') { cmp+=s[i]; x=x*10+(s[i]-'0'); } else point=s.size()-1-i; for(int i=1; i<n; i++) { cmp=Multiply(cmp,x); } int ans_point=cmp.size()-n*point; if(ans_point<0) { count+='.'; for(int i=ans_point; i!=0; i++) count+='0'; } string::iterator it=cmp.begin(); if(ans_point>=0&&ans_point<cmp.size()) cmp.insert(it+ans_point,'.'); count+=(Remove_later(cmp)); return count; }
字符串去除后导0函数,前导0可以先反转取后导。题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=272
string Remove_later(string s) //删除一个字符串的后倒0 { for(int i=s.size()-1; i>=0; i--) { if(s[i]=='0') s.erase(i); else break; } return s; }