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. Redis+Keepalived高可用方案详细分析

    背景 目前,Redis集群的官方方案还处在开发测试中,未集成到稳定版中.且目前官方开发中的Redis Cluster提供的功能尚不完善(可参考官方网站或http://www.redisdoc.com/ ...

  2. instanceof 运算符简介

    文章摘自: http://www.ibm.com/developerworks/cn/web/1306_jiangjj_jsinstanceof/ https://developer.mozilla. ...

  3. 手机站测试工具(node服务器)

    最近在工作中遇到手机站测试的问题,于是就写了一个node服务外加一个第三方的转二维码功能,欢迎拍砖~ 项目地址:https://github.com/finderL/webserver

  4. JavaScript中的事件代理/委托

    事件委托在JS高级程序设计中的定义为"利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件" 如何理解上面的这句话呢,在网上,大牛们一般都使用收快递这个例子来解释的, ...

  5. 深入理解Java对象序列化(转载)

    原文地址:http://developer.51cto.com/art/201202/317181.htm 1. 什么是Java对象序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般 ...

  6. jquery.validate 中文乱码解决方法

    第一种.就是所说的引用jquery.validate.messages_cn.js                    下载地址:http://files.cnblogs.com/files/pin ...

  7. 用svmpredict输出的结果为空

    源程序:

  8. SpringBoot事件监听

    代码演示: package com.boot.event.eventdemo; import org.springframework.boot.SpringApplication; import or ...

  9. 【bzoj4278】[ONTAK2015]Tasowanie 贪心+后缀数组

    题目描述 给定两个数字串A和B,通过将A和B进行二路归并得到一个新的数字串T,请找到字典序最小的T. 输入 第一行包含一个正整数n(1<=n<=200000),表示A串的长度. 第二行包含 ...

  10. Elasticsearch cat Apis

    1._cat列入所有有效命令 GET /_cat 返回:有个猫...所以不难想象为啥是cat api =^.^= /_cat/allocation /_cat/shards /_cat/shards/ ...