CF 900B

CF 900B
    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
 

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

Hint

第一组样例 : 1 / 2 = 0.5000(0) 出现在第二个位置

第二组样例 : 2 / 3 = 0.6666(6) 7 没有出现,输出 -1

本题主要有两个易错点,一是不要认为只寻找小数点后六位,我是开到了小数点后1000位。二是double的精度问题,因为double存储格式的问题,存储的小数是不精确的,所以避免小数的运算,转为整数的运算。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<iostream>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=200;
int main()
{
    int i,p,j;
    int a,b,c,flag=0;
    scanf("%d%d%d",&a,&b,&c);
    for(i=0; i<=10000; i++)
    {
        if(a<b&&flag==0)
            flag=i+1;
        if(a<b)
        {
            a*=10;
            p=a/b;
            if(p==c&&flag!=0)
                break;
            a=a%b;
        }
        else
        {
            a=a%b;
        }

    }
    if(i<=1000)
        printf("%d
",i+2-flag);
    else
        printf("-1
");
    return 0;
}
原文地址:https://www.cnblogs.com/daybreaking/p/9323246.html