ACM-简单题之Just a Numble——hdu2117

来源:互联网 发布:知乎 人力资源部门前景 编辑:程序博客网 时间:2024/05/22 10:31
Just a Numble
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2106 Accepted Submission(s): 986

Problem Description
Now give you two integers n m, you just tell me the m-th number after radix point in 1/n,for example n=4,the first numble after point is 2,the second is 5,and all 0 followed

Input
Each line of input will contain a pair of integers for n and m(1<=n<=10^7,1<=m<=10^5)

Output
For each line of input, your program should print a numble on a line,according to the above rules

Sample Input
4 2
5 7
123 123

Sample Output
5
0

8


这道题,怎么说呢,第一次看,有点吓到啊,

被m,n范围,

又是一道大数题了么。。o(╯□╰)o

好讨厌的说。。。

算一算怎么解呢?后来发现,就模拟除法运算,很简单就算出来了。

比如当n=4, 首先判断1%4==0 不相等,

按照除法规则就需要 将余数乘以10 再判断,而余数1乘以10  再除以4得到的便是小数点后第一个数,

再判断余数1乘以10以后的数与4取余是否等于0,不等就再次循环。

啊,还要注意碰到无限循环小数,你就悲剧了,所以,要利用m,

题目求第m个位置,我们就算到第m个位置结束,break就OK了,

嘿嘿,又AC一道题啦O(∩_∩)O~


//just a numble#include <iostream>#include <string.h>using namespace std;int arr[100001];void set_arr(int n,int m){memset(arr,0,sizeof(arr));int i,j,k;i=k=1;while((k=k%n)!=0){k*=10;arr[i]=k/n;++i;// 如果i大于m,不管是有限小数还是无限的,全跳出来if(i>m)break;}return;}int main(){int n,m;int i,j,len;while(cin>>n>>m){set_arr(n,m);cout<<arr[m]<<endl;}return 0;}



0 0
原创粉丝点击