P2399 non hates math
P2399 non hates math
将分数化成小数的模拟题,把循环减掉就可以了。
1.1(234)
*10^4==11234.234
*10^1==11.234
9999*(1.1(234))==11223
所以
(1.1(234))==11223/9999
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<cstring> 8 #define inf 2147483647 9 #define For(i,a,b) for(register long long i=a;i<=b;i++) 10 #define p(a) putchar(a) 11 #define g() getchar() 12 //by war 13 //2017.10.23 14 using namespace std; 15 char a[10100]; 16 long long len; 17 long long fenzi,fenmu,zi,mu; 18 long long g,s; 19 bool flag; 20 21 void in(long long &x) 22 { 23 long long y=1; 24 char c=g();x=0; 25 while(c<'0'||c>'9') 26 { 27 if(c=='-') 28 y=-1; 29 c=g(); 30 } 31 while(c<='9'&&c>='0')x=x*10+c-'0',c=g(); 32 x*=y; 33 } 34 void o(long long x) 35 { 36 if(x<0) 37 { 38 p('-'); 39 x=-x; 40 } 41 if(x>9)o(x/10); 42 p(x%10+'0'); 43 } 44 45 long long gcd(long long a,long long b) 46 { 47 return (b==0?a:gcd(b,a%b)); 48 } 49 50 long long ksm(long long a,long long b) 51 { 52 if(b==0) 53 return 1; 54 while(b%2==0) 55 { 56 b>>=1; 57 a=a*a; 58 } 59 long long r=1; 60 while(b>0) 61 { 62 if(b%2==1) 63 r=r*a; 64 b>>=1; 65 a=a*a; 66 } 67 return r; 68 } 69 70 int main() 71 { 72 cin>>a; 73 len=strlen(a); 74 For(i,0,len-1) 75 if(a[i]!='.') 76 fenzi=fenzi*10+a[i]-'0'; 77 else 78 { 79 s=i+1; 80 break; 81 } 82 fenmu=ksm(10,len-s); 83 For(i,s,len-1) 84 { 85 if(a[i]=='(') 86 { 87 flag=true; 88 mu=ksm(10,i-s); 89 zi=fenzi; 90 } 91 if(a[i]!='('&&a[i]!=')') 92 { 93 fenzi=fenzi*10+a[i]-'0'; 94 if(!flag) 95 zi=zi*10+a[i]-'0'; 96 } 97 } 98 if(!flag) 99 { 100 zi=0; 101 mu=0; 102 } 103 else 104 { 105 fenmu/=100; 106 } 107 fenzi-=zi; 108 fenmu-=mu; 109 g=gcd(fenzi,fenmu); 110 fenzi/=g; 111 fenmu/=g; 112 o(fenzi),p('/'),o(fenmu); 113 return 0; 114 }