1001ITeye - 超凡娱乐

1001ITeye

2019年03月01日11时02分43秒 | 作者: 鸿文 | 标签: 小数点,个数,位数 | 浏览: 2511

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.

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. 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. Dont print the decimal point if the result is an integer.
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
大致题意: 输入一个包括6位的数R,0.0 R 99.999 ,求它的n次方,0 n =25 输出时前面的0要省去,结尾没有意义的0也要省去 ; 解决方案一:G++
#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-i1)
 cout "." result[i];
 else
 cout result[i];
 i;
 cout endl;
 我写了一些注释,能够调用pow函数,这个核算式自己写的: 1.首要,用一个char字符数组,也就是字符串存储R 2.遍历R,去掉结尾剩余的0和小数点,把所稀有存在result数组中,用base记载这个大数; 3.经过result中的数与base相乘,各自取出个位,十位,百位,千位,等等 4.核算完成后,接下来就只剩小数点了,用after记载需求的小数点个数,假如result里边的元素个数 小于result,则证明这个数是小数,还要添0,反之,则移位就行。 解决方案二: GCC
#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-i1)
 printf("%c%d",.,result[i]) ;
 else
 printf("%d",result[i]) ;
 i;
 printf("\n");
 scanf("%s%d", ch, power)!=EOF  这句话必定要写,否则会报Output Limit Exceeded,无法停止程序。  解决方法三:Java
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 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 i++)
 System.out.print(out.charAt(i)) ;
 System.out.println();
 或许也能够这样:
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表超凡娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章

阅读排行

  • 1

    puttyITeye

    保存,用户名,暗码
  • 2

    1001ITeye

    小数点,个数,位数
  • 3
  • 4
  • 5

    vim装备ITeye

    文件,设置,状况
  • 6

    vim装备ITeye

    文件,设置,状况
  • 7

    WebService JaxITeye

    功用,客户端,运用
  • 8

    easymock快速入门ITeye

    运用,断语,机制
  • 9
  • 10