大数算法——高精度大数幂次

本文介绍了一种计算高精度大数幂运算的算法实现,解决了计算机在处理非常大的数值时可能出现的问题。该算法通过字符串操作实现了任意精度的浮点数乘方计算,并详细展示了如何处理输入输出格式及各种边界情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大数——高精度大数求幂
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. 

This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
Input
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.
Output
The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.
Sample Input
95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

这道题有不少细节点需要注意的,自己代码写的很繁琐,没那么简化,应当适当优化才对。
1.注意第二个样例数据,当前面的整数部分为0时,不输出,
2.确保当输入数据为0时依旧有结果。
3.输入10和10.00验证答案是否达到题目要求,这也是坑
4.判断小数点位置时的算法要好好考虑,不是简单的从前往后数第几位是小数点,

代码如下:
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
using namespace std;
void fun(char str1[] ,int n)//n为幂次,str1已经倒置所有数字
{
        int n1 = strlen(str1);
        int a[5000], b[5000],temp[5000];
        for (int i = 0; i < 5000; i++)
               a[i] = b[i] = 1000, temp[i] = 0;
        for (int i = 0; i < n1; i++)//char to int
        {
               a[i] = b[i] = str1[i] - '0';
        }
        int count = n1, k;
        for (int i = 1; i < n; i++)//幂次
        {
               
               for (int j = 0; j < n1; j++)//底下的数字
               {
                       k = j;
                       for (int j2 = 0; a[j2]!=1000; ++j2)//上面的数字
                       {
                              temp[k] += a[j2] * b[j];
                              k++;
                       }
               }
               for (int i =0; i<k; i++)
               {
                       temp[i + 1] += temp[i] / 10;
                       temp[i] = temp[i] % 10;
               }
               for (int i = 0; i < k || temp[i] != 0; i++)
               {
                       a[i] = temp[i];
                       temp[i] = 0;
               }
        }
        int stri = 0;
        for (int i = k + 1; i >= 0; i--)
        {
               if (a[i] != 1000)
               {
                       str1[stri] = a[i] + '0'; stri++;
               }
        }
}
int main()
{
        char s[5000]; int n;
        memset(s, '\0', sizeof(s));
        while (cin>>s>>n)
        {
               char str1[5000];
               memset(str1, '\0', sizeof(str1));     
               int k=0,pointset=0;
               for (int i = strlen(s)-1; i>=0; i--)//转换到char 字符数组
               {
                       if (s[i] != '.')
                       {
                              str1[k] = s[i];
                              k++;
                       }
                       else pointset = strlen(s)-1-i;
               }
               fun(str1, n);
               pointset *= n;//小数点位置
               int t1 = strlen(str1),zero=t1;
               if(strchr(s,'.')!=NULL)
               for (int i = t1 - 1; i >= t1-pointset; i--)
               {
                       if (str1[i] != '0')break;
                       else zero = i;//最后尾数的0的清除
               }
               int flag = 0;
               if(pointset)
               for (int i = 0; i <= t1-pointset; i++)
               {
                       if (str1[i] == '0')
                              flag = i;//开头的0的个数
                       else break;
               }
               for (int i = flag; i < t1; i++)
               {
                       if (zero == 0) { cout << 0; break; }
                       if (zero == i ) { break; }
                              if (i == t1 - pointset&&pointset)
                              {
                                      cout << ".";
                              }
                              cout << str1[i];
               }
               cout << endl;
        }
        return 0;
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值