本题套用题中所给公式即可(注意只有j=0时才可使用)
分子分母约分要使用辗转相除得最大公约数的方法,不然会TLE,不然会TLE,不然会TLE!!!重要的事情说三遍QwQ
代码如下:
#include <iostream>
using namespace std;
int Q(int a,int b) //使用辗转相除法求最大公约数
{
return a%b?Q(b,a%b):b;
}
int main()
{
int j,temp,number=0,i,a,b,c;
char x[15];
while((cin>>j)&&j!=-1)
{
a=0;
b=temp=1;
number++;
cin>>x;
cout<<"Case "<<number<<": ";
for(i=2; x[i]; ++i)
{
a*=10;
a+=(x[i]-'0');
b*=10; //计算10的(k+j)次幂
if(i<=j+1)
temp*=10; //计算10的j次幂
}
if(j) //使用题中所给公式
{
a-=(a/temp);
b-=(b/temp);
}
c=Q(a,b);
a/=c;
b/=c;
cout<<a<<"/"<<b<<endl;
}
return 0;
}