"High precision" poj1001-exponentiation

Source: Internet
Author: User
Tags mul

The second problem of the whole question bank, originally did not have the chip to do, the whim holds the mentality of the second kill to write the code, but the hard and sang ate 4 wa.

"Thinking" first remove the decimal point, the most basic high-precision multiplication, and then the result of the operation to add the decimal point output.

"Front shop" Let's take a look at how big the array needs to be set up first. The data range is a maximum of 99.999, then approximately 100.000, when n=25, there are at most 125 zeros, that is, up to 125 bits.

"Easy Error Point" * Data 1:10 01, if directly from the back to 0, the data output will become 1. So delete the last zero of the range is the lowest, until after the decimal point.

* Data 2:000010 01, there is no decimal point in this data! At first I mistook the 6-bit for a decimal point, so the array of num is labeled 0. 5, but this case takes up 0 of the space. 6. My solution is that if there is a decimal point in the current number, then the last one, that is, the reverse-placed Num[0] is set to 0, integers and decimals can be processed uniformly.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5 using namespacestd;6 Const intmaxn= the+5;7 CharS[MAXN];8 intNUM[MAXN];9 intANS[MAXN];Ten intn,ed,pos=0; One  A voidSwitchnum ()//converts a string to a number and records the position of the next decimal point - { -     BOOLHave=false; the      for(intI=0;i<7; i++) -     { -         if(s[i]=='.')  -         { +Pos= (6-I.) *n;have=true; -         } +         Else  A         { at             if(have) num[5-(I-1)]=s[i]-'0'; -                 Elsenum[5-i]=s[i]-'0'; -         } -     } -     if(have) num[0]=0;  -      for(intI=0;i<6; i++) inans[i]=Num[i]; - }  to  + voidMul () - { the     intTEMP[MAXN]; *memset (temp,0,sizeof(temp)); $      for(intI=0; i<=ed;i++)Panax Notoginseng          for(intj=0; j<=5; j + +) -         { thetemp[i+j]+=ans[i]*Num[j]; +             if(i+j>0&& temp[i+j-1]>9) A             { thetemp[i+j]+=temp[i+j-1]/Ten; +temp[i+j-1]%=Ten; -             } $         } $Ed=ed+5; -     if(temp[ed]>9)//don't write >10 . -     { theed++; -temp[ed]=temp[ed-1]/Ten;Wuyitemp[ed-1]%=Ten; the     } -      for(intI=0; i<=ed;i++) ans[i]=Temp[i]; Wu } -  About voidoutput () $ { -     BOOLf=false;  -     intop=0; -      for(intI=0; i<pos;i++)//0 of the bottom of the integer part cannot be deleted . A     { +         if(ans[i]>0) Break; theop++; -     } $      for(inti=ed;i>=op;i--) the     { the         if(i==pos-1)//cannot be written here (i=pos-1) Otherwise assignment, do not write (i==pos+1) the         { thecout<<'.'; -f=true; in         } the         if(ans[i]>0) f=true; the         if(f) cout<<Ans[i]; About     } thecout<<Endl; the } the  + intMain () - { the      while(SCANF ("%s%d", s,&n)! =EOF)Bayi     { theEd=5;//Remove the initial 5 from the decimal point the switchnum (); -          for(intI=1; i<n;i++) Mul (); - output (); the     } the}

"High precision" poj1001-exponentiation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.