任学锋后台,嘉盛钰佳国际,wwe2013年4月8日
Description
现有一式子 a / b. 你需要找出数字 c 在小数点后第一次出现的位置
Input
输入包含三个整数 a, b, c (1 ≤ a < b ≤ 105, 0 ≤ c ≤ 9).
Output
输出数字 c 第一次在小数点后出现的位置,如果 c 不在小数点后出现输出 -1
Sample Input
1 2 0
2
2 3 7
-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\n",i+2-flag); else printf("-1\n"); return 0; }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论