JAVA BigDecimal

import java.util.*;
import java.math.*; public class Main { public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String str = cin.next();
int n = cin.nextInt();
BigDecimal res = new BigDecimal(1);
final BigDecimal bd = new BigDecimal(str);
for(int i = 0; i < n; ++i){
res = res.multiply(bd);
}
str = res.stripTrailingZeros().toPlainString();
System.out.println(str.startsWith("0.") ? str.substring(1) : str);
}
}

C++

#include<bits/stdc++.h>
using namespace std; string multi(string a,string b)
{
int i,j,arr[],len=a.length()+b.length();
memset(arr,,sizeof(arr));
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
for(i=;i<a.length();i++)
{
for(j=;j<b.length();j++)
{
arr[i+j]+=(a[i]-'')*(b[j]-'');
}
}
for(i=;i<len;i++)
{
arr[i+]+=arr[i]/;
arr[i]%=;
}
string ret=string(len,'');
for(i=;i<len;i++)
ret[i]+=arr[i];
reverse(ret.begin(),ret.end());
return ret;
} string strpow(string x,int p)
{
string ret="";
while(p)
{
if(p&)
ret=multi(ret,x);
x=multi(x,x);
p>>=;
}
return ret;
} int main()
{
string a;
int n,i,index;
while(cin>>a>>n)
{
index=a.find('.');
if(index==-)
index=;
else
{
a=a.substr(,index)+a.substr(index+);
index=(a.length()-index)*n;
}
a=strpow(a,n);
a=a.substr(,a.length()-index)+"."+a.substr(a.length()-index);
for(i=;i<a.length();i++)
{
if(a[i]!='')
break;
}
a=a.substr(i);
for(i=a.length()-;i>=;i--)
{
if(a[i]=='.')
{
a=a.substr(,i);
break;
}
else if(a[i]!='')
{
a=a.substr(,i+);
break;
}
}
cout<<a<<endl;
}
}

追求效率?开发时间

追求性能?运行时间、消耗内存

51nod 1873 高精度计算的更多相关文章

  1. 51nod 1873 初中的算术【Java BigDecimal/高精度小数】

    1873 初中的算术 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 Noder现在上初三了,正在开始复习中考.他每天要计算型如 (a× a× a× ...

  2. 51Nod 1873 初中的算术

    大神的字符串快速幂 #include <iostream> #include <string> #include <algorithm> #include < ...

  3. 51nod 1162 质因子分解

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1162 数据范围大约是2^97,需要高精度计算 可以使用pollard- ...

  4. 【51Nod 1244】莫比乌斯函数之和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...

  5. 51Nod 1268 和为K的组合

    51Nod  1268  和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...

  6. 51Nod 1428 活动安排问题

    51Nod   1428  活动安排问题 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活 ...

  7. 51Nod 1278 相离的圆

    51Nod 1278 相离的圆 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278 1278 相离的圆 基 ...

  8. 【51Nod 1501】【算法马拉松 19D】石头剪刀布威力加强版

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1501 dp求出环状不连续的前缀和,剩下东西都可以算出来,比较繁琐. 时间 ...

  9. 【51Nod 1622】【算法马拉松 19C】集合对

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1622 简单题..直接暴力快速幂 #include<cstdio&g ...

随机推荐

  1. java超强分页标签演示

    最近在做一个项目,用到了一个分页,于是动手写了个分页标签,先将代码贴出来,供大家交流,写的不好,请见谅!. 以下是java标签类,继承自SimpleTagSupport package com.lyn ...

  2. Bus of Characters(栈和队列)

    In the Bus of Characters there are nn rows of seat, each having 22 seats. The width of both seats in ...

  3. 第四次作业之psp

    psp 进度条 博文字数累积折线图 代码行数累积折线图 psp饼状图

  4. 【android】实现手指滑动来切换activity(转)

    http://code.eoe.cn/115 1.jpg外部引用 原始文档 MainActivity.java外部引用 原始文档 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  5. java — 排序算法

    1.冒泡排序 比较相邻元素,如果第一个比第二个大,就交换位置,每一次交换,当前 package BubbleSort; public class Test { public static void m ...

  6. java.lang.ClassNotFoundException: com.google.gson.Gson 问题解决

    我是这么解决:把gson.jar放到WEB-INF/lib目录下. 放在其他目录就会报错.

  7. GPS定位,经纬度附近地点查询–C#实现方法

              摘要:目前的工作是需要手机查找附近N米以内的商户,功能如下图数据库中记录了商家在百度标注的经纬度(如:116.412007,39.947545),最初想法以圆心点为中心点,对半径做 ...

  8. PAT 甲级 1032 Sharing

    https://pintia.cn/problem-sets/994805342720868352/problems/994805460652113920 To store English words ...

  9. ZOJ 1403 F-Safecracker

    https://vjudge.net/contest/67836#problem/F "The item is locked in a Klein safe behind a paintin ...

  10. Windows2008安装启用无线网卡

    昨天给本子换了系统来着,本来想法是好的,想在本子上安装Hyper-v来搭建多平台VPS,这样的话就能玩多个系统了,对于我自己来说对娱乐没啥兴趣,扯多了,正文 笔记本安装什么都很顺利,但是无线网卡把我难 ...