You have a fraction . You need to find the first occurrence of digit c into decimal notation of the fraction after decimal point.
Input
The first contains three single positive integers a, b, c (1 ≤ a < b ≤ 105, 0 ≤ c ≤ 9).
OutputPrint position of the first occurrence of digit c into the fraction. Positions are numbered from 1 after decimal point. It there is no such position, print -1.
Examples1 2 0
2
2 3 7
-1
The fraction in the first example has the following decimal notation: . The first zero stands on second position.
The fraction in the second example has the following decimal notation: . There is no digit 7 in decimal notation of the fraction.
题意:求a/b的小数部分中第几位是c思路:模拟除法,现将a,b约分,减少运算次数
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
int gcd(int a,int b){
while(b){
int c=a%b;
a=b;
b=c;
}
return a;
}
int main(){
int n,m,c;
cin>>n>>m>>c;
int temp=gcd(n,m);
n/=temp;
m/=temp;
int ans=-1;
for(int i=1;i<=N;i++){
n*=10;
if(n/m==c){
ans=i;
break;
}
n%=m;
}
cout<<ans<<endl;
return 0;
}