zoukankan      html  css  js  c++  java
  • 高精度算r的n次方 问题 H: 乾隆巡江南

    问题 H: 乾隆巡江南

    时间限制: 2 Sec  内存限制: 128 MB
    提交: 13  解决: 3
    [提交][状态][讨论版]

    题目描述

            话说乾隆带着他的宰相刘罗锅和你出巡江南,被杭州城府邀请去听戏,至于什么戏,那就不知了。乾隆很高兴,撒酒与君臣共享。三更欲回住处,可是乾隆这人挺怪,他首先要到西湖边散散步,而且命令不准有人跟着他。         小醉,步于西湖岸,停于断桥前,突闻琴声悠悠,歌儿婉婉。这乐曲不哀伤不愁怅,少了一分怨女的羁绊,多了一分少女的期盼。乾隆走上前去,视其背影,为一女子手抚古琴,悠悠而唱。可是这么晚了,小女怎么还不回家呢,莫非是她起早床?乾隆走上前去,小声问道:“伊为何未宿?”,小女沉默片刻,转身而来。顿时,顿时,顿时!!!!!乾隆惊呆了!!!!哇!!!!噻!!!!!!这人,这伊!!!!原来!!!!!!!不是一个美女(狂汗ing)。小女并未回答她的话,只是与乾隆侃了侃诗。乾隆兴哉,问其曰:“不知偶能助伊否?”,小女曰:“偶无所以助,且有一事相求,愿君能解之。”         乾隆一看,立刻晕到在地,片刻而起,曰:“明日必解之”,且去。         回到家中,乾隆夜召你“入寝”,曰:“如此姑娘,如此情调,如此罗曼蒂克,竟然丢一个如此煞风景之问”,一边发气,一边把这个问题交给你。你一看,顿然发现,原来是用蝌蚪文写的:         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  < =  9999.9)  and  n  is  an  integer  such  that  0  <   n  < =  200.          此时的你,已经是皇帝身边的小太监,自然有必要为皇上解决此题。

    输入

            The  input  will  consist  of  a  set  (less  than  11)  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  2
    0.4321  5
    5.1234  7
    6.7592  3
    98.999  5
    1.0100 10
    

    样例输出

    9048.385129
    .01506334182914325601
    92663.3181348508776705891407804544
    308.806114738688
    9509420210.697891990494999
    1.10462212541120451001


    题目意思就是说,算R的n次方,整数部分全是零的话不要输出0(如例2),小数最后的0也不用输出来,例如0.100只需输出.1即可,若答案是整数,不用输出小数点




    #include <iostream>
    #include <string>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int main()
    {
        char a1[15];
        int a[10005];
        int b[10005];
        int c[10005];
        int n;
        while (cin >> a1 >> n)
        {
            memset(a, 0, sizeof(a));
            memset(b, 0, sizeof(b));
            memset(c, 0, sizeof(c));
            int l = strlen(a1);
            int k=-1, j, i;//k一开始初始化为-1,为了辨别输进来的是小数还是整数
            for (i = 0; i < l; i++)
            {
                if (a1[i] == '.')
                {//是小数的话,先把"."去了
                    k = i;
                    for (j = i; j < l; j++)
                    {
                        a1[j] = a1[j + 1];//小数点后的几位都向前移一位
                    }
                    k = l - k - 1;//k用来标记第几位应该有小数
                    break;
                }
            }
            if (k != -1)
            {//输进来是小数的话
                for (i = 0; i <= l - 2; i++)
                {
                    a[l - 1 - i] = a1[i] - '0';
                    b[l - 1 - i] = a1[i] - '0';
                }
                l--;
            }
            else
            {//是整数的话
                for (i = 0; i <= l - 1; i++)
                {
                    a[l - i] = a1[i] - '0';//倒的存入a数组
                    b[l - i] = a1[i] - '0';
                }
            }
            
            int lc = l;
            int x;
            int nn = n - 1;
            while (nn--)
            {
                for (i = 1; i <= l; i++)//这里是l
                {
                    x = 0;
                    for (j = 1; j <= lc; j++)//这里是lc,别弄反了,一开始我就反了
                    {//乘法运算
                        c[i + j - 1] = a[i] * b[j] + x + c[i + j - 1];//当前位=乘机+进的位+当前位
                        x = c[i + j - 1] / 10;
                        c[i + j - 1] = c[i + j - 1] % 10;
                    }
                    c[lc + i] = x;
                }
                lc = l + lc;
                for (i = 1; i <= lc; i++)
                {
                    b[i] = c[i];
                }
                memset(c, 0, sizeof(c));
            }
            if (k == -1)
            {//整数的情况
                while (b[lc] == 0 && lc>1) lc--;//删除前导零
                for (i = lc; i >= 1; i--) cout << b[i];
                cout << endl;
            }    
            else
            {//小数的情况
    
                bool f = 0;
                for (i = lc; i >= n * k + 1; i--)//n*k这个位置要放‘.’
                {//先输出整数部分
                    if (b[i] == 0 && f == 0) continue;
                    else if (b[i] != 0) { f = 1; cout << b[i]; }
                    else cout << b[i];
                }
                f = 1;
                int pp = 1;
                for (i = 1; i <= n * k; i++)
                {
                    if (b[i] == 0 && f == 0) continue;
                    else if (b[i] != 0 && f == 1) { f = 1; pp = i; break; }
                }
                if (i != n * k + 1)//i==n*k+1时,代表小数位全是0,不需要输出‘.’
                {
                    cout << ".";
                    for (i = n * k; i >= pp; i--)  cout << b[i];
                }
                cout << endl;
            }
        }
        return 0;
    }
     
  • 相关阅读:
    2019春总结作业
    第十二周作业及总结
    第十一周作业及总结
    第十周编程及总结
    第九周作业及总结
    第八周作业及总结
    第五周作业及总结
    第四周Java作业及总结
    第三周总结
    第二周总结
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271314.html
Copyright © 2011-2022 走看看