Brian Warehouse

Some birds aren`t meant to be caged, their feathers are just too bright... ...
posts - 40, comments - 16, trackbacks - 0, articles - 1

POJ 1001 Exponentiation

Posted on 2010-08-17 14:02 Brian 阅读(275) 评论(0)  编辑 收藏 引用 所属分类: POJ

分类开篇语: 第一个程序搞了好几天,发现了很多问题。POJ不保证按顺序做且更新速度肯定不会很快。有些题自己做不出来借鉴别人的会注明出处。很多算法都需要从网上找,第一题的大浮点数相乘的核心算法就是这样找来的。我心里明白,虽然AC了,但是边缘数据处理的很粗糙,我自己都发现几个bug了,但是依然AC了。

本题主要注意将字符串转化为实际的数字然后借鉴数制的思想来进行大数相乘。

Description

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 Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input

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.

Output

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.

Sample Input

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
编译器C++ 源码:
#include <iostream>
#include <string>
using namespace std;
#define MAX 255
 
int getnum(string s,int *c) // get real number of R
{
    int i=0,j=0,t[MAX];
    memset(t,0,sizeof(int)*MAX); // a stores 0
   
 while (i < 6) // R value 1 through 6
 {
  if (s[i] != '.')
  {
   t[j]=s[i]-'0';
   j++;
  }   
  i++;
 }     // a`s length = 5
   
 for (j=0; j<5; j++)
  c[j]=t[4-j]; // c stores in order from a
  
 for (i=0; s[i] != '.'; i++); // find decimal point
 return (5-i); // the position of . point
}
 
void multi(int *a,int *b) // big-multiplication
{
    int i=0,j,r=0,t[MAX];
    memset(t,0,sizeof(int)*MAX); // t stores 0
   
 for (; i<5; i++)
  for (j=0; j<255; j++)
   t[i+j] += a[i]*b[j]; // core algorithms!
 
 for (i=0; i<255; i++)
 {
  b[i]=(r+t[i])%10; // r always stores remainder
  r=(r+t[i])/10;   // b stores the result
 }    
}    // basic algorithms of b-m
int main() 
{
    int i,j,d_pos,n,a[MAX],b[MAX];
    string s;
    while (cin>>s>>n)
 {
  
  memset(b,0,sizeof(int)*MAX);
  memset(a,0,sizeof(int)*MAX);
  d_pos=getnum(s,a);
  getnum(s,b);
  
  for (i=0; i<n-1; i++)
   multi(a,b);  // a is a loop invariant
  
  for (i=254; !b[i]; i--); //find last non-zero  
  for (j=0; !b[j]; j++); // find first non-zero
  
  for (; i >= n*d_pos; i--) // loop n times
   cout<<b[i];
  if (n*d_pos >= j+1) cout<<"."; //pay attention
  for (i=n*d_pos-1; i>=j; i--)
   cout<<b[i];  //from back formating output
  
  cout<<endl;
 }
 return 0;
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理