zoukankan      html  css  js  c++  java
  • Problem F: Exponentiation

    Problem F: Exponentiation
    Time Limit: 1 Sec Memory Limit: 128 MB
    Submit: 4 Solved: 2
    [Submit][Status][Web Board] [Edit] [TestData]
    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 le 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 Rn. Leading zeros and insignificant trailing zeros should be suppressed in the output.

    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
    主要思路是:几的几次方。实际上也就是大数相乘,先把小数点去掉,然后相乘,最后再把小数点加上。比如说的 a 的 n 次方,那先把字符数组a里面的东西,赋值给字符数组b,然后 a 和 b 进行运算,乘的结果放到sum数组里,然后整型的 sum 数组里的每个元素转换成字符型的,赋值给字符数组b,然后再和 a数组运算,运算的次数和你输入的几次方有关系。。然后再进行细节方面的问题……

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    int main()
    {
        char a[1000];
        char b[1000];//和字符数组a一样的
        int n,i,j,k,lena,lenb,g,g1,h,ji1,ji2;
        int sum[1001];
        char s[1002];
        while(scanf("%s%d",&a,&n)!=EOF)
        {
            if(n==1)
                 cout<<a<<endl;
           else
           {
               int count=0;
            memset(sum,0,sizeof(sum));
            lena=strlen(a);
            for(i=0;i<lena;i++)
            {
               if(a[i]=='.')
               {
                   count=lena-i-1;//小数点后面几位
                for(j=i;j<lena-1;j++)
                {
                    a[j]=a[j+1];
                }
                a[j]='';
                lena--;          //变成没有小数点的数
                break;
               }
            }
            count=count*n;//经过处理小数点最终有count位
            for(i=0;i<lena;i++)
            {
                b[i]=a[i];
            }
            b[i]='';
            lenb=strlen(b);
             for(i=1;i<=n-1;i++)
               {
                    memset(sum,0,sizeof(sum));
                   g=1000;g1=1000;
              for(j=lenb-1;j>=0;j--)//下面的乘数
              {
                  for(k=lena-1;k>=0;k--)//上面的乘数
                  {
                      sum[g--]+=(a[k]-'0')*(b[j]-'0');
                  }
                  g1--;
                  g=g1;
              }
              for(k=1000;k>=1;k--)
              {
                  sum[k-1]+=sum[k]/10;
                  sum[k]=sum[k]-sum[k]/10*10;
              }
             int start=0;  
             int l=0;
             while(!sum[start] &&start<=999)
                 start++;
               for(h=start;h<=1000;h++)
               {
                     b[l++]=sum[h]+'0';
               }
               b[l]='';
              lenb=strlen(b);
          }
        if(count!=0)
        {
            for(g=0;g<=1000;g++)
                s[g]=sum[g]+'0';
            s[1002]='';
            for(i=1000;i>1000-count;i--)
                s[i+1]=s[i];
            s[i+1]='.';
          for(i=0;i<=1002;i++)
          {
              if(s[i]!='0')
              {
                  ji1=i;
                  break;
              }
          }
          for(i=1001;i>=0;i--)
          {
              if(s[i]!='0')
              {
                  ji2=i;
                  break;
              }
          }
          for(i=ji1;i<=ji2;i++)
              cout<<s[i];
          cout<<endl;
        }
        else
            cout<<b<<endl;
        }
       }
        return 0;
    }
  • 相关阅读:
    快速排序就这么简单
    Shiro入门这篇就够了【Shiro的基础知识、回顾URL拦截】
    SpringDataJPA入门就这么简单
    递归就这么简单
    SpringBoot就是这么简单
    Activiti就是这么简单
    Lucene就是这么简单
    过来人告诉你,去工作前最好还是学学Git
    给女朋友讲解什么是Git
    我终于看懂了HBase,太不容易了...
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4236896.html
Copyright © 2011-2022 走看看