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 Rnwhere R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12
548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201大致题意:
#include<iostream> using namespace std; int main() { char ch[6]; //用字符数组来接收浮点数 int power; //阶数 while(cin>>ch>>power) { int result[125]={0}; int tail=5,head=0; int after=0; while(ch[tail]=='0') tail--; while(ch[head]!='.') head++; after=tail-head; //小数点后面的数的位数 int i=0; int base=0; int p=1; int pow=power; while(tail>=0)//小数点后数的个数 { if(ch[tail]!='.')//把所有数存入数组 { result[i]=ch[tail]-'0';//去真是数值 base+=(ch[tail]-'0')*p;//存储小树点末尾的数 i++;//记录小数点后数的个数 p*=10; } tail--; } /** 逆序存储所有非0数 int q = 0 ; while(q<i) { cout<<result[q] ; q++ ; } **/ while(power>1)//计算阶乘 { tail=0; while(tail<i) { result[tail]=base*result[tail]; tail++; } int j=0; int before=0; int mid=0; while(j<i) { mid=(result[j]+before)%10; before=(result[j]+before)/10; result[j]=mid; // cout<<mid<<endl ; j++; } while(before>0) { result[i]=before%10; before/=10; i++; } power--; } after*=pow;//小数点的总位数 /** while(i>=0) { cout<<result[i]; i--; } **/ if(after>=i)//移位数大于总数的位数 { int zero=after-i;//小数点后添0的个数 cout<<"."; while(zero>0) { cout<<"0"; zero--; } i--; while(i>=0) { cout<<result[i]; i--; } } else if(after<i) { i--; while(i>=0) { if(after-i==1) cout<<"."<<result[i]; else cout<<result[i]; i--; } } cout<<endl; } }我写了一些注释,可以调用pow函数,这个计算式自己写的:
#include<stdio.h> int main() { char ch[6]; int power ; while(scanf("%s%d",&ch,&power)!=EOF) { int result[125]={0}; int tail=5,head=0; int after=0; while(ch[tail]=='0') tail--; while(ch[head]!='.') head++; after=tail-head; //小数点后面的数的位数 int i=0; int base=0; int p=1; int pow=power; while(tail>=0)//小数点后数的个数 { if(ch[tail]!='.')//把所有数存入数组 { result[i]=ch[tail]-'0';//去真是数值 base+=(ch[tail]-'0')*p;//存储小树点末尾的数 i++;//记录小数点后数的个数 p*=10; } tail--; } /** 逆序存储所有非0数 int q = 0 ; while(q<i) { cout<<result[q] ; q++ ; } **/ while(power>1)//计算阶乘 { tail=0; while(tail<i) { result[tail]=base*result[tail]; tail++; } int j=0; int before=0; int mid=0; while(j<i) { mid=(result[j]+before)%10; before=(result[j]+before)/10; result[j]=mid; // cout<<mid<<endl ; j++; } while(before>0) { result[i]=before%10; before/=10; i++; } power--; } after*=pow;//小数点的总位数 /** while(i>=0) { cout<<result[i]; i--; } **/ if(after>=i)//移位数大于总数的位数 { int zero=after-i;//小数点后添0的个数 printf("%c",'.') ; while(zero>0) { printf("%d",0) ; zero--; } i--; while(i>=0) { printf("%d",result[i]) ; i--; } } else if(after<i) { i--; while(i>=0) { if(after-i==1) printf("%c%d",'.',result[i]) ; else printf("%d",result[i]) ; i--; } } printf("\n"); } }scanf("%s%d",&ch,&power)!=EOF
import java.math.BigDecimal; import java.util.Scanner; /** * 高精度计算 * @author tanlvxu * */ public class Main{ /** * @param args */ public static void main(String[] args) { Scanner cin = new Scanner(System.in) ; while(cin.hasNext()) { BigDecimal x = cin.nextBigDecimal() ; BigDecimal y = BigDecimal.valueOf(1.0) ; int n = cin.nextInt() ; for(int i=1;i<=n;i++) { y = y.multiply(x) ; } String out = new String(y.toPlainString()) ; boolean flag = false ; int q = out.length() - 1 ; //去掉后面的0 while(out.charAt(q)=='0'){ q-- ; } if(out.charAt(q)=='.') { q-- ; } int p = 0 ; //去掉前面多余的0 while(out.charAt(p)=='0') p++ ; for(int i=p;i<=q;i++) { System.out.print(out.charAt(i)) ; } System.out.println(); } } }或者也可以这样:
import java.math.BigDecimal; import java.util.Scanner; public class Demo5 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin = new Scanner(System.in) ; while(cin.hasNext()) { BigDecimal x = cin.nextBigDecimal() ; int n = cin.nextInt() ; x = x.pow(n); String str = x.toPlainString() ; int q = str.length() - 1 ; while(str.charAt(q)=='0') { q -- ; } if(str.charAt(q)=='.') { q-- ; } int p = 0 ; while(str.charAt(p)=='0') { p++ ; } for(;p<=q;p++) { System.out.print(str.charAt(p)); } System.out.println() ; } } }
BigDecimal |
pow(int n) 返回其值为 (thisn) 的 BigDecimal,准确计算该幂,使其具有无限精度。 |
相关推荐
1001bit Tools (1001bit 建筑工具集),1001bit_pro v2.1建模插件中文汉化版(比suapp还好用还全面的SketchUp插件,支持SketchUp8.0,2013,2014,2015) 可以自由添加减少要的图标,从此不再被各种各样眼花缭乱的插件所...
oracle1001oracle1001
sketchup 1001插件,2014-15汉化版,自用没有问题sketchup 1001插件,2014-15汉化版,自用没有问题sketchup 1001插件,2014-15汉化版,自用没有问题sketchup 1001插件,2014-15汉化版,自用没有问题sketchup 1001...
1001 bit tools 1.1.5 简体中文版
Intel_Rapid_STorage_9.5.4.1001 INTEL 5系列用的AHCI驱动WINDOWS系统用的内含64位32位是.INF驱动文件 不是.EXE文件。INF可以在PE或安装原版系统时加载驱动用! INTEL 5系列主板用的。如H55 主板 等5系列主板的IRST ...
TDS1001B示波器的详细图解使用教程,讲解全面,详细明了,可以让你快速上手TDS1001B示波器
将 1001bit_pro v2.1 文件夹上面的文件及子文件夹,复制到 sketchup 2013/8.0 程序下的 Plugins 文件夹下面。 2. Setkchup 2014 的安装 将 1001bit_pro v2.1 文件夹上面的文件及子文件夹,复制到 C:\...
DS1001 信号电平表使用说明书 天津市德力电子仪器有限公司
利用JK触发器设计,输入一段信号,每当检测到1001后输出高电压。
UWB dwm1001
1001序列检测器实验报告,内含modelsim仿真代码和测试代码,Multisim仿真截图。
yrzb-1001-v141222-1.apk
笔记本蓝屏,网上查到的 “装的xp sp3,经常运行一段时间后,就会蓝屏, iastor.sys提示, 0X000000d1,必须重启。...iastor.sys在 C:\WINDOWS\system32\drivers目录下,用7.03.1001替换8.6.0.1007。”
我的PAT乙级练习题1001代码记录,题目地址:https://www.patest.cn/contests/pat-b-practise/1001
图片展示1001序列检测器,简明易懂,形象生动。
TDT 1001-2012 地籍调查规程.zip
百瑞BR1001 datasheet。蓝牙SoC,M3核,128KRAM,4M Flash,最高48M
hdu1001解题报告
TCCSW1001-2020
驱动程序-打印机驱动-固网hp1001打印服务器驱动.zip