高精度
参考资料
例题
洛谷 P1601 A+B Problem(高精)
高精度加法,相当于 a+b problem,不用考虑负数。
Code (1)
#include <bits/stdc++.h>
using namespace std;
const int N=505;
int a[N],b[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s,t;
cin>>s>>t;
int n=s.size(),m=t.size(),k=max(n,m);
for(int i=0;i<n;i++)a[n-i-1]=s[i]-'0';
for(int i=0;i<m;i++)b[m-i-1]=t[i]-'0';
for(int i=0;i<k;i++)
{
a[i]+=b[i];
a[i+1]+=a[i]/10;
a[i]%=10;
}
if(a[k])k++;
for(int i=k-1;i>=0;i--)cout<<a[i];
return 0;
}
洛谷 P1255 数楼梯
楼梯有 阶,上楼可以一步上一阶,也可以一步上二阶。
编一个程序,计算共有多少种不同的走法。
Code (1)
#include <bits/stdc++.h>
using namespace std;
const int N=5005;
int a[2][N]={{1},{2}};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
int len=1;
for(int i=2;i<n;i++)
{
for(int j=0;j<len;j++)
{
a[i&1][j]+=a[i&1^1][j];
}
for(int j=0;j<len;j++)
{
a[i&1][j+1]+=a[i&1][j]/10;
a[i&1][j]%=10;
}
if(a[i&1][len])len++;
}
for(int i=len-1;i>=0;i--)cout<<a[n&1^1][i];
return 0;
}