竖式除法模拟

写这篇博客的目的是为了更好利用数学方法来处理数据,之前遇到了一道题给你a,b,c三个数问a/b小数点后几位是c,按照我的想法肯定是现将a/b的结果使用函数转换成字符串,然后直接在字符串中查找,很遗憾对于数据量较大的数根本就不能通过,还好这道水题是我队友AC通过了,这道题到我手里肯定是通不过的,那我们就来模拟一下竖式除法的过程。

看到这个式子是不是回忆起小学学习除法的经历了吧,这应该是四则运算中最困难的了。

我们来看一下运算过程:

被除数a对除数b整除,得到一个结果,再对余数乘10(十进制),组成新余数,新余数变为新被除数,除数不变,再次进行整除,不断循环。

也就是a=a%b*10

例题1:HDU 2117  http://acm.hdu.edu.cn/showproblem.php?pid=2117

Just a Numble

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
 
题目意思:问1/n后的第m位数是多少。
 1 #include<iostream>
 2 #include<stdio.h>
 3 using namespace std;
 4 int main()
 5 {
 6     int m,n,result,i;
 7     int temp;
 8     while(scanf("%d%d",&n,&m)!=EOF)
 9     {
10         temp=1;
11         for(i=1;i<=m;i++)
12         {
13             temp*=10;
14             result=temp/n;
15             temp=temp%n;
16         }
17         result=result%10;
18         printf("%d
",result);
19     }
20     return 0;
21 }
例2

CF 900B - Position in Fraction

Description

You have a fraction . You need to find the first occurrence of digit c into decimal notation of the fraction after decimal point.

Input

The first contains three single positive integers abc (1 ≤ a < b ≤ 1050 ≤ c ≤ 9).

Output

Print position of the first occurrence of digit c into the fraction. Positions are numbered from 1 after decimal point. It there is no such position, print -1.

Sample Input

Input
1 2 0
Output
2
Input
2 3 7
Output
-1

Hint

The fraction in the first example has the following decimal notation: . The first zero stands on second position.

The fraction in the second example has the following decimal notation: . There is no digit 7 in decimal notation of the fraction.

题目意思:问a/b得到的小数中c在小数点后多少位上。

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 const int MAX=1e6+10;
 5 using namespace std;
 6 int main()
 7 {
 8     int a,b,c,i,ans;
 9     scanf("%d%d%d",&a,&b,&c);
10     ans=-1;///若没有找到,即为初始化的-1
11     for(i=1; i<MAX; i++)
12     {
13         a=a*10;
14         if(a/b==c)
15         {
16             ans=i;
17             break;
18         }
19         a=a%b;
20     }
21     printf("%d
",ans);
22     return 0;
23 }
原文地址:https://www.cnblogs.com/wkfvawl/p/9484044.html