Description
现有一式子 a / b. 你需要找出数字 c 在小数点后第一次出现的位置
Input
输入包含三个整数 a, b, c (1 ≤ a < b ≤ 105, 0 ≤ c ≤ 9).
Output
输出数字 c 第一次在小数点后出现的位置,如果 c 不在小数点后出现输出 -1
Sample Input
Input
1 2 0
Output
2
Input
2 3 7
Output
-1
思路:因为有的小数为无线不循环小数,不能用double型变量把数据算出来进行查找,所以需要模拟除法的计算过程,进行一遍遍历。
代码实现如下:
#include<stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a,b,c,i=1;
scanf("%d%d%d",&a,&b,&c);
if(a>b)
a=a-((a/b)*b);
for(i=1;i<99999;i++)
{
int t;
t=(a*10)/b;
if(t==c)
break;
a=a*10-(t*b);
}
if(i<99999)
printf("%d\n",i);
else
printf("-1\n");
return 0;
}
浙公网安备 33010602011771号