poj1001&大数乘法

原创 2016年08月30日 11:57:16
Exponentiation
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 159884   Accepted: 38963

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

</pre><br /><pre name="code" class="html">#include<string>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <fstream>
using namespace std;
const int MAX_LEN = 1000;
int ia[MAX_LEN];
string subF(string str1, string str2)
{    
     int i,j;
     string result;
     memset(ia,0,sizeof(ia));     // 暂存乘法运算的结果

     reverse(str1.begin(), str1.end());     // 倒置
     reverse(str2.begin(), str2.end());

     int point1, point2,point;               //     小数位数
     point1 = str1.find('.');    
     point2 = str2.find('.');
     if(point1 != string::npos)               // 如果为小数,则将小数点删除
          str1.erase(str1.begin()+point1);
     else
          point1 = 0;
     if(point2 != string::npos)
          str2.erase(str2.begin()+point2);
     else
          point2 = 0;
     point1 > point2 ? point = point1 : point = point2;     // point保存最大的小数位数

     int len1 = str1.size();
     int len2 = str2.size();
    
     for(i = 0; i < len1; i++)               // 乘法运算
          for(j = 0; j < len2; j++)
               ia[i+j] += (str1[i]-'0')*(str2[j]-'0');

     int len;     // 乘积的长度
     for(i = 0; i < len1+len2; i++)          // 进位处理
     {
          if(ia[i] >= 10)
          {
               ia[i+1] += (ia[i]/10);
               ia[i] %= 10;
          }
     }
     if(ia[len1+len2-1] > 0)
          len = len1+len2;
     else
          len = len1+len2-1;

     for(i = len-1; i >= 0; i--)
          result += char(ia[i]+'0');
     if(point > 0)          // 插入小数点
     {
          result.insert(result.end()-point1-point2,'.');
          for(i = result.size()-1; i >= 0; i--)     // 去除小数点后的后置0
          {
               if(result[i] == '0')
                    result.erase(result.begin()+i);
               else if(result[i] == '.')
			   {
				   result.erase(result.begin()+i);
				   break;
			   }
			   else
                    break;
          }
		  while( (*result.begin()) == '0' )		// 去除小数点前的前置0
			  result.erase(result.begin());
     }
     return result;
}

int main()
{
     string str1;
	 int n;
     while(cin >> str1 >> n)
     {
		 string str2("1");
		 while(n--)
		 {
           str2 = subF(str1, str2);
		 }
          cout << str2 << endl;
     }
     return 0;
}


相关文章推荐

POJ 1001 大数阶乘

  • 2011年11月29日 12:38
  • 3KB
  • 下载

poj1001高精度乘法

  • 2013年10月19日 10:23
  • 1KB
  • 下载

Exponentiation(POJ-1001) Java大数例题

Description Problems involving the computation of exact values of very large magnitude and precisio...

poj 1001 Exponentiation(大数)

Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 154884   Accepted: ...

大数相关poj1001

// go.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include #include #include #include #...

POJ1001幂次方的运算,大数据

代码: package com.jzy.poj1001; /*  * 本代码可以解决poj1001题目。  * 只要计算的结果在string字符串空间大小范围内,R,n可以随便输入。  * ...

POJ 1001 Exponentiation(大数)

Description Problems involving the computation of exact values of very large magnitude and precisio...

JAVA 处理 大数 POJ1001

由于种种原因最近一段时间一直情绪不佳,写几个水题娱乐一下。 话说去年寒假学了点JAVA,写了些推箱子之类的东东,但还从来没有用JAVA A过题,很早就听说JAVA直接用大数类做大数和高精度手段很是淫...

HDU 1063/POJ 1001-Exponentiation(大数类)

Exponentiation Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

poj 1001 java大数类

今天在地大邀请赛中,A题是大数题,java大数类不会用,c++大数模板写的不熟练,就懒得写,结果,只得了三等奖。。。 今天恶补下: import java.math.BigDecimal; i...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj1001&大数乘法
举报原因:
原因补充:

(最多只允许输入30个字)