D 远神的高精度
传送门 https://ac.nowcoder.com/acm/contest/318/D
这道题就是个高精度的裸题
写这篇博客的目的不在于针对这道题,
而是给大家提供一个我做高精度的做法
以后碰到也许可以直接套我的板子!
代码如下:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int a[1105]; 5 int cnt=0; 6 int cal(int x,int y,int n){ 7 double z=(double)x/y; 8 int res=0; 9 while(z>=1){ 10 res++; 11 z/=10; 12 } 13 if(x<y){ 14 res=1; 15 a[++cnt]=0; 16 } 17 while(cnt<=n+res){ 18 int flag=0; 19 while(x<y&&x!=0){ 20 x*=10; 21 if(flag)a[++cnt]=0; 22 flag++; 23 } 24 int r=x/y; 25 int b[10]; 26 int cnt1=0; 27 while(r>0){ 28 b[++cnt1]=r%10; 29 r/=10; 30 } 31 for(int i=cnt1;i>=1;i--){ 32 a[++cnt]=b[i]; 33 } 34 x%=y; 35 if(x==0){ 36 int s=cnt; 37 for(int i=s;i<=n+res;i++){ 38 a[++cnt]=0; 39 } 40 break; 41 } 42 } 43 return res; 44 } 45 int main(){ 46 int x,y,n; 47 cal(100,1,5); 48 while(~scanf("%d%d%d",&x,&y,&n)){ 49 a[0]=0; 50 cnt=0; 51 int k=cal(x,y,n); 52 char s[100]; 53 scanf("%s",s); 54 if(s[0]=='Y'){ 55 if(a[n+k+1]>=5){ 56 a[n+k]++; 57 } 58 for(int i=n+k;i>=1;i--){ 59 if(a[i]>=10){ 60 a[i]=0; 61 a[i-1]++; 62 } 63 else break; 64 } 65 if(a[0]>0)printf("%d",a[0]); 66 for(int i=1;i<=k;i++){ 67 printf("%d",a[i]); 68 } 69 if(n>0)printf("."); 70 for(int i=k+1;i<=k+n;i++){ 71 printf("%d",a[i]); 72 } 73 printf("\n"); 74 } 75 else{ 76 for(int i=1;i<=k;i++){ 77 printf("%d",a[i]); 78 } 79 if(n>0)printf("."); 80 for(int i=k+1;i<=k+n;i++){ 81 printf("%d",a[i]); 82 } 83 printf("\n"); 84 } 85 } 86 }
E 爱摸鱼的Dillonh
传送门 https://ac.nowcoder.com/acm/contest/318/E
题意:很好看懂不再解释
思路:枚举n开i次方,最大只要开60方。这题的坑点在于开方时要用powl(),
还要把数强转为long double,光是这些还不够,我们一般性的以为开多次方,
就会有多少个因子,事实上这只是我们想当然,必须还要判断一下因子数是
否和i相等。这道题足足wa了我20多发。。。好吧也算是学到了,不亏。
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef long double ld; 5 int T; 6 ll n; 7 vector< vector<ll> >ans; 8 int main(){ 9 cin>>T; 10 int kase=0; 11 while(T--){ 12 scanf("%lld",&n); 13 ll x=n; 14 while(x%2==0)x/=2; 15 printf("Case #%d:\n",++kase); 16 if(x==1){ 17 printf("-1\n"); 18 continue; 19 } 20 else{ 21 ans.clear(); 22 for(int i=1;i<=60;i++){ 23 ll t=powl((ld)n,(ld)(1.0/i)); 24 if(t==1)continue; 25 x=n; 26 vector<ll>v; 27 while(x%t==0){ 28 x/=t; 29 v.push_back(t); 30 } 31 while(x%(t+1)==0){ 32 x/=(t+1); 33 v.push_back(t+1); 34 } 35 if(v.size()==i&&x==1)ans.push_back(v); 36 } 37 printf("%d\n",ans.size()); 38 for(int i=0;i<ans.size();i++){ 39 printf("%d",ans[i].size()); 40 for(int j=0;j<ans[i].size();j++){ 41 printf(" %lld",ans[i][j]); 42 } 43 printf("\n"); 44 } 45 } 46 } 47 }