`

1001

阅读更多
总时间限制:
500ms
内存限制:
65536kB
描述

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. Don't 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-i==1)
         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-i==1)
         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<=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() ;
		}
	}

}
 
   java准确的计算精度BigInteger/BigDecimal常用方法:
   add()加,substract()减,multiply()乘,devide()除
   abs()绝对值,max()最大值,min()最小值,compare to()比较大小,toString(),toPlainString() 转化为字符串
 
     仅BigInteger , mod()求余, gcd()求最大公约数;,and()求与,or()求或,not()求反,xor()求异或
 
BigDecimal pow(int n)
          返回其值为 (thisn)BigDecimal,准确计算该幂,使其具有无限精度。
 String toPlainString()
          返回不带指数字段的此 BigDecimal 的字符串表示形式。
 String toString()
          返回此 BigDecimal 的字符串表示形式,如果需要指数,则使用科学记数法。

 

static BigDecimal valueOf(double val)
          使用 Double.toString(double) 方法提供的 double 规范的字符串表示形式将 double 转换为 BigDecimal
static BigDecimal valueOf(long val)
          将 long 值转换为具有零标度的 BigDecimal
static BigDecimal valueOf(long unscaledVal, int scale)
          将 long 非标度值和 int 标度转换为 BigDecimal
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics