The idea for this question is quite simple, mainly to make a high-precision ride, and then consider some details.
The code is quite short, but it has been wrong for a long time...
Let's talk about the ideas:
This is to remove the decimal point when reading the data. Mark the decimal point location.
Remove the decimal point and then perform high-precision multiplication. Remove the leading zero.
Then calculate the decimal point
Remove unnecessary zeros after the decimal point.
In addition, the answer is output directly when K is set to 1.
The details are mainly simulated,
I wa several times:
1. When there is no number after the decimal point, of course the decimal point does not need to be output... this is not taken into consideration.
2. There is no limit on k = 1, because the answer is in the C array, so when k = 1, there is no answer
3. The array is small ....
Code attached:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;char s[201];int n,m;int l,mark;bool f;int a[100050],b[101],c[100001];void into(){l=strlen(s);int i=1;f=true;while(i<=l){if(s[i]==‘.‘ && f){ mark=i; f=false;} else{a[i]=s[l-i]-‘0‘;i++; }}mark=l-mark;l=i-1;for(int j=mark;j<l;j++) a[j]=a[j+1];for(int j=1;j<=l;j++){b[j]=a[j];}}int main(){freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);//freopen("data.txt","r",stdin);while(cin>>s>>n){if(n==1){cout<<s<<"\n";continue;}memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(c,0,sizeof(c));mark=0;into();l-=1;int bl=l;int sum=mark-1;for(int k=2;k<=n;k++){memset(c,0,sizeof(c));for(int i=1;i<=l;i++) for(int j=1;j<=bl;j++){ c[i+j-1]+=(a[i]*b[j]); c[i+j]+=(c[i+j-1]/10); c[i+j-1]%=10; }l+=bl;l++;mark=sum*k;while(l>1 && !c[l] && l>mark) l--;for(int i=1;i<=l;i++) a[i]=c[i];} for(int i=l;i>mark;i--){ printf("%d",c[i]); } int t=1; for(int i=1;i<=mark;i++){ if(c[i]==0){ t++; } else break; } if(t-1==mark){ cout<<"\n"; continue; } else{ cout<<"."; for(int i=mark;i>=t;i--){ printf("%d",c[i]); } cout<<"\n";} }fclose(stdin);fclose(stdout);return 0;}
Vj1010: high-precision multiplication + careful Simulation