短学期Day.3(模拟与高精度)

https://www.luogu.com.cn/problem/P1045

#include<iostream>
#include<cmath>
using namespace std;
int P;
long long a[510];

long long POW2(int a){
    long long res = 1;
    for (int i = 1; i <= a; i++){
        res *= 2;
    }
    return res;
}

int main()
{
    cin >> P;

    cout << ceil(P * log10(2)) << endl; // 第一问 

    long long tmp = POW2(32);       // tmp=2^32

    a[500] = 1;
    int t32 = P / 32;                 // t32:能乘以2^32的次数 
    int t1 = P - t32 * 32;              // t1: 剩下的,一个一个乘上去 
    for (int T = 1; T <= t32; T++) {
        for (int i = 500; i >= 1; i--){
            a[i] *= tmp;            // 每一位都乘上2^32
        }
        for (int i = 500; i >= 1; i--){
            a[i - 1] += a[i] / 10;
            a[i] %= 10;             // 处理进位
        }
    }
    for (int T = 1; T <= t1; T++) {
        for (int i = 500; i >= 1; i--)   {
            a[i] *= 2;              // 每一位都乘上2
        }
        for (int i = 500; i >= 1; i--){
        
            a[i - 1] += a[i] / 10;
            a[i] %= 10;             // 处理进位
        }
    }
    a[500]--;

    for (int i = 1; i <= 500; i++)
    {
        cout << a[i];
        if (i % 50 == 0) cout << endl;
    }

    return 0;
}

快速幂代码:

//求x^n
int fastPower(int x, int n) {
	    if (n == 0) return 1;
	   int result = fastPower(x, n >> 1);
	   result *= result;
	   return (n & 1) == 0 ? result : result * x;
	
}

https://www.luogu.com.cn/problem/P1303

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
string a1, b1;//用string存入数据
int a[10001], b[10001], i, x, len, j, c[10001];
int main()
{
	cin >> a1 >> b1;
	int lena = a1.size();
	int lenb = b1.size();
	//倒序存入数据
	for (i = 1; i <= lena; i++)a[i] = a1[lena - i] - '0';
	for (i = 1; i <= lenb; i++)b[i] = b1[lenb - i] - '0';
	for (i = 1; i <= lenb; i++)
		for (j = 1; j <= lena; j++)
			c[i + j - 1] += a[j] * b[i];
	for (i = 1; i < lena + lenb; i++)
		if (c[i] > 9)
		{
			c[i + 1] += c[i] / 10;
			c[i] %= 10;
		}
	len = lena + lenb;
	//去除高位的0
	while (c[len] == 0 && len > 1)len--;
	for (i = len; i >= 1; i--)cout << c[i];
	return 0;
}

错位相加代码:

for (i = 1; i <= lenb; i++)
		for (j = 1; j <= lena; j++)
			c[i + j - 1] += a[j] * b[i];

https://www.luogu.com.cn/problem/P1249

#include<iostream>
using namespace std;
long long n, x = 0, s1 = 0, i = 2, t = 1;
int a[10001];
int s[10001];
void cj(int x)
{
    for (int i = 1; i <= t; i++)
        s[i] = s[i] * x;
        //t表示位数,x是乘数
        //下面是进位处理
    for (int i = 1; i <= t; i++)
    {
        if (s[i] >= 10)
        {
            long long x = s[i] / 10;
            s[i + 1] += x;
            s[i] %= 10;
            //位数等于当前位置+1,t=i+1与t++等效
            if (i + 1 > t) t = i + 1;
            
        }
    }
}
int main()
{
    s[1] = 1;
    cin >> n;
    //找出乘数序列
    while (s1 < n)
    {
        a[++x] = i;
        s1 += i;
        i++;
    }
    //多出来的数字的序列的那位为0
    a[s1 - n - 1] = 0;
    for (int i = 1; i <= x; i++)
    {
    //输出第一问
        if (a[i] != 0) cout << a[i] << ' ';
        //累乘
        if (a[i] != 0) cj(a[i]);
    }
    cout << endl;
    for (int i = t; i >= 1; i--)
        cout << s[i];
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值