ICPC North Central NA Contest 2018 C . Rational Ratio(结论 模拟 gcd)

简介: ICPC North Central NA Contest 2018 C . Rational Ratio(结论 模拟 gcd)

linkkkk

题意:

给出一个无限小数和循环节,将其化成最简分式。

思路:

先有个结论是


0.456=456/999


按照这种模拟就好了。

如果说,循环节不是全部的小数位的话,先将多余的数变到小数点前,处理后再变回去。

比如0.14757575,可以先变成14.757575,最后再除以100

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef unsigned long long ull;
typedef pair<ll,ll>PLL;typedef pair<int,int>PII;typedef pair<double,double>PDD;
#define I_int ll
inline ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
#define read read()
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)
ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
const int maxn=4e5+7,maxm=1e6+7,mod=1e9+7;
int main(){
  string s;cin>>s;
  int k=read;
  ll sum9=0;
    for(int i=1;i<=k;i++) sum9=sum9*10+9;
    ll pos=-1,pre=0,las=0;
    for(int i=0;i<s.size()-k;i++){
        if(s[i]=='.'){
            pos=i;continue;
        }
        pre=pre*10+s[i]-'0';
    }
    for(int i=s.size()-k;i<s.size();i++){
        las=las*10+s[i]-'0';
    }
    ll tmp=1;
    for(int i=pos+1;i<s.size()-k;i++) tmp=tmp*10;
    ll ans=sum9*pre+las;
    sum9=sum9*tmp;
    ll res=__gcd(ans,sum9);
    printf("%lld/%lld\n",ans/res,sum9/res);
  return 0;
}


目录
相关文章
|
7月前
|
Go
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
28 0
|
人工智能 BI
CodeForces - 1485D Multiples and Power Differences (构造+lcm)
CodeForces - 1485D Multiples and Power Differences (构造+lcm)
58 0
ICPC Greater New York Region 2020 (模拟)
ICPC Greater New York Region 2020 (模拟)
73 0
ICPC North Central NA Contest 2018 G . Tima goes to Xentopia(最短路 空间优化 剪枝)
ICPC North Central NA Contest 2018 G . Tima goes to Xentopia(最短路 空间优化 剪枝)
57 0
|
人工智能 BI
AtCoder Beginner Contest 216 F - Max Sum Counting (降维 背包dp)
AtCoder Beginner Contest 216 F - Max Sum Counting (降维 背包dp)
98 0
[North Central NA Contest 2018] Rational Ratio | 规律 细节模拟
Description Every (positive) rational number can be expressed as a ratio of two (positive) integers. However, in decimal form, rational numbers often have an infifinitely repeating pattern, e.g., 1/7 = 0.142857142857142857…
93 0
[North Central NA Contest 2018] Rational Ratio | 规律 细节模拟
【1088】Rational Arithmetic (20 分)
【1088】Rational Arithmetic (20 分) 【1088】Rational Arithmetic (20 分)
82 0
2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】
Dying Light Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 513    Accepted Submission(s): ...
1152 0