Problem K: qwb与小数
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 403 Solved: 78
[Submit][Status][Web Board]
Description
qwb遇到了一个问题:将分数a/b化为小数后,小数点后第n位的数字是多少?
做了那么多题,我已经不指望你能够帮上他了。。。
Input
多组测试数据,处理到文件结束。(测试数据<=100000组)
每组测试例包含三个整数a,b,n,相邻两个数之间用单个空格隔开,其中0 <= a <1e9,0 < b < 1e9,1 <= n < 1e9。
Output
对于每组数据,输出a/b的第n位数,占一行。
Sample Input
1 2 1
1 2 2
Sample Output
5
0
思路:这道题思路很简单,
核心思想是商的第n位为第n-1位的余数*10/ 除数,
只是需要用到快速幂
的思想,求出第n-1位的余数。
#include<stdio.h>
long long mod(long long a,long long b,long long c,long long n)//求出n-1位的余数
{long long ans=a,base=c;while(n){if(n&1)ans=ans*base%b;//ans记录余数 base=base*base%b;//根据同余定理ans与base都取余 n>>=1;}return ans;
}
int main()
{long long a,b,n;while(~scanf("%lld %lld %lld",&a,&b,&n)){long long t;t=mod(a%b,b,10,n-1)%b*10/b;printf("%lld\n",t);//第n位为第n-1位的余数*10/ 除数 }return 0;
}