一. 题目
Exponentiation
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 156373   Accepted: 38086

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

  1. 95.123 12
  2. 0.4321 20
  3. 5.1234 15
  4. 6.7592 9
  5. 98.999 10
  6. 1.0100 12

Sample Output

  1. 548815620517731830194541.899025343415715973535967221869852721
  2. .00000005148554641076956121994511276767154838481760200726351203835429763013462401
  3. 43992025569.928573701266488041146654993318703707511666295476720493953024
  4. 29448126.764121021618164430206909037173276672
  5. 90429072743629540498.107596019456651774561044010001
  6. 1.126825030131969720661201

Hint

If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer

  1. C++

  2. while(cin>>s>>n)

  3. {

  4. ...

  5. }

  6. c

  7. while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want

  8. /*while(scanf(%s%d",s,&n)!=EOF) //this also work */

  9. {

  10. ...

  11. }

Source

 
二. 题意
  • 给出字符串表示的浮点数 和 n次方数
  • 输出其n次方运算后的值
三. 分析
  • 算法核心:

    • 大数乘法:利用程序模拟算数运算过程,算出乘积
  • 实现细节:
    • 将输入数字从低位到高位反转存储,计算时方便进位
    • 模拟计算时,去除小数点,统一按照大整数相乘
    • 输出时逆序输出,同时计算小数点位置,相应位输出小数点
 
三. 题解
  1. #include <stdio.h>
  2. #include <memory.h>
  3.  
  4. #define MAXN 100
  5. int a[MAXN], b[MAXN], len_a, len_b;
  6.  
  7. /* 模拟算数运算过程,算出乘积 */
  8. void multiply(int *a, int *b)
  9. {
  10. int i, j, len, c[MAXN];
  11.  
  12. memset(c, , sizeof(c));
  13.  
  14. for (i = ; a[i] != -; i++)
  15. for (j = ; b[j] != -; j++)
  16. c[i + j] += a[i] * b[j];
  17.  
  18. len = i + j - ;
  19.  
  20. for (i = ; i < len; i++)
  21. if (c[i] >= ) { c[i + ] += c[i] / ; c[i] = c[i] % ;}
  22.  
  23. if (c[len] != ) len_b = len + ;
  24. else len_b = len;
  25.  
  26. for (i = ; i < len_b; i++) b[i] = c[i];
  27. }
  28.  
  29. void main()
  30. {
  31. char str[MAXN];
  32. int i, j, exp;
  33. int st, pt, ed;
  34.  
  35. freopen( "input.txt", "r" , stdin);
  36.  
  37. while (scanf("%s %d", str, &exp) != EOF) {
  38. memset(a, -, sizeof(a));
  39. memset(b, -, sizeof(b));
  40. len_a = ;
  41. st = pt = ed = -;
  42.    
  43.    /*
  44.     1.st为输入数字的启始位置
  45.     2.pt为输入数字小数点的位置
  46.     3.ed为输入数字的结束位置
  47.     4.此三个中间变量的作用:
  48.       4.1 去掉开头和结尾的 0
  49.       4.2 对于小数点的特殊处理
  50.       4.3 用于计算出乘积后小数点的正确位置
  51. */
  52. for (i = ; str[i] != '\0'; i++) {
  53. len_a++;
  54.  
  55. if (str[i] != '' && str[i] != '.' && st == -) st = i;
  56. if (str[i] != '' && str[i] != '.' || (str[i] != '.' && pt == -)) ed = i;
  57. if (str[i] == '.') pt = i;
  58. }
  59.  
  60. for (i = ed, j = ; i >= st; i--) {
  61. if (str[i] == '.') continue;
  62.  
  63. b[j] = a[j] = str[i] - '';
  64. j++;
  65. }
  66.  
  67. len_b = len_a = j;
  68. for(i = ; i < exp; i++) multiply(a, b);
  69.  
  70. if (pt < st) {
  71. printf(".");
  72. for (i = ; i < (ed - pt) * exp - len_b; i++) printf("");
  73. }
  74.  
  75. for (i = len_b - ; i >= ; i--) {
  76. if (pt > st && pt < ed && i == (ed - pt) * exp - ) printf(".");
  77. printf("%d", b[i]);
  78. }
  79.  
  80. printf("\n");
  81. }
  82. }

[POJ] #1001# Exponentiation : 大数乘法的更多相关文章

  1. POJ 1001 Exponentiation(大数运算)

    POJ 1001 Exponentiation 时限:500 ms   内存限制:10000 K 提交材料共计: 179923   接受: 43369 描述:求得数R( 0.0 < R < ...

  2. [POJ 1001] Exponentiation C++解题报告 JAVA解题报告

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

  3. POJ 1001 Exponentiation 无限大数的指数乘法 题解

    POJ做的非常好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. ...

  4. POJ 1001 Exponentiation

    题意:求c的n次幂……要求保留所有小数…… 解法:一开始只知道有BigInteger……java大数+模拟.第一次写java大数……各种报错各种exception……ORZ 没有前导0和小数后面的补位 ...

  5. poj 1001 Exponentiation 第一题 高精度 乘方 难度:1(非java)

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

  6. POJ 1001 Exponentiation(JAVA,BigDecimal->String)

    题目 计算实数a的n次方,具体输出格式看案例 import java.util.*; import java.math.*; public class Main { public static voi ...

  7. POJ 1001 Exponentiation 模拟小数幂

    模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...

  8. Project Euler 16 Power digit sum( 大数乘法 )

    题意: 215 = 32768,而32768的各位数字之和是 3 + 2 + 7 + 6 + 8 = 26. 21000的各位数字之和是多少? 思路:大数乘法,计算 210 × 100 可加速计算,每 ...

  9. 51nod 1027大数乘法

    题目链接:51nod 1027大数乘法 直接模板了. #include<cstdio> #include<cstring> using namespace std; ; ; ; ...

随机推荐

  1. java工具类–自动将数据库表生成javabean

    最近和数据库的表打交道挺多的,因为暂时做的是接口活. 在这过程中发现要把表转换成对应的javabean类型,字段少的表还行,如果不小心碰到几十个字段的他妈的写起来就有点麻烦了,万一碰到几百个的呢,那不 ...

  2. C# 使用AutoResetEvent进行线程同步

    AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...

  3. linux 命令学习大全

    转载  http://blog.csdn.net/xiaoguaihai/article/details/8705992/

  4. 23.allegro中自动布线[原创]

    1. --- 方法①:选择网络自动布线 -- --- 已经步好: --- 方法②: ---- ---- 布线: --- 方法③: -- ----

  5. Android开发之onActivityResult()中的resultCode为0,intent为null的解决办法

    BUG:昨天在使用activity之间传值的时候,遇到了一个bug,该bug为:Activity A启动Activity B,然后在Activity B中取到一个值,并通过back键返回到Activi ...

  6. 1934. Black Spot(spfa)

    1934 水题 RE了N久 后来发现是无向图 #include <iostream> #include<cstring> #include<algorithm> # ...

  7. Android系统服务-WindowManager

      WindowManager是Android中一个重要的服务 (Service ).WindowManager Service 是全局的,是唯一的.它将用户的操作,翻译成为指令,发送给呈现在界面上的 ...

  8. HDU 4948

    题目大义: 给一张图,任意两点间有单向边,找出一种方案,使得每个新入队的点与队中的点距离<=2. 题解: 贪心,从最后入队点开始反向插入,每次找出最大入度的点入队. 只需证明最大入度点A与所有未 ...

  9. HTTPS通信机制

    概述 使用HTTP协议进行通信时,由于传输的是明文所以很容易遭到窃听,就算是加密过的信息也容易在传输中遭受到篡改,因此需要在HTTP协议基础上添加加密处理,认证处理等,有了这些处理机制的HTTP成为H ...

  10. UVa 536 Tree Recovery

    题意:给出一颗二叉树的前序遍历和中序遍历,输出其后序遍历 用杭电1710的代码改一点,就可以了. #include<iostream> #include<cstdio> #in ...