高精度×单精度
参考题目:阶乘数码 - 洛谷
代码:
#include <iostream>
using namespace std;
int main()
{
int t;cin>>t;
while(t--)
{
int a[4001]={0};a[1]=1;
int i,j;//循环体
int q=1;//位数
int jw=0;//进位
int num;cin>>num;
int tar;cin>>tar;
long long cnt=0;
for(i=2;i<=num;i++)
{
for(j=1;j<=q;j++)
{
a[j]=a[j]*i+jw;
jw=a[j]/10;
a[j]%=10;
}
while(jw>0) //处理最大数位, 若进位就在加一位
{
a[j]=jw%10;
jw/=10;
j++;
}
q=j-1;
}
for(int o=1;o<=q;o++)
{
if(a[o]==tar)cnt++;
}
cout<<cnt<<endl;
}
system("pause");
}
高精度 × 高精度
#include<iostream>
using namespace std;
#include<string>
int a[10001];
int b[10001];
int c[10001];
int main()
{
string s1, s2;
cin >> s1>> s2;
int len1 = s1.size(); int len2 = s2.size();
int len = len1 + len2;
for (int i = 1; i <=len1; i++)
{
a[i] = s1.at(len1 - i) - '0';
}
for (int i = 1; i <= len2; i++)
{
b[i] = s2.at(len2 - i) - '0';
}
for(int i=1;i<=len1;i++)
for (int j = 1; j <= len2; j++)
{
c[i + j - 1] += a[i] * b[j];
}
for (int o = 1; o < len; o++)
{
if (c[o] > 9)
{
c[o + 1] += c[o] / 10;
c[o] %= 10;
}
}
while (c[len] == 0 && len > 1)len--;
for (int i = len; i >= 1; i--)cout << c[i];
/*cout << c[1];
cout << 'len1' << ' ' << len1 << ' ' << len2 << endl;*/
}
最简单的 高精度+高精度: A+B Problem(高精) - 洛谷
#include<iostream>
using namespace std;
#include<string>
int a[10001];
int b[10001];
int c[10001];
int main()
{
string s1, s2;
cin >> s1 >> s2;
int len1 = s1.size();
int len2 = s2.size();
for (int i = 1; i <= len1; i++)
{
a[i] = s1.at(len1 - i) - '0';
}
for (int i = 1; i <= len2; i++)
{
b[i] = s2.at(len2 - i) - '0';
}
int len = len1 + len2;
for (int i = 1; i <= len; i++)
{
c[i] = a[i] + b[i];
}
for (int i = 1; i <= len+1; i++)
{
if (c[i] > 9)
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
}
while (c[len] == 0 && len > 1)len--;
for (int i = len; i >= 1; i--)cout << c[i];
}