我可不是为了被全人类喜欢才活着的,只要对于某一个人来说我是必|

王陸

园龄:6年11个月粉丝:2052关注:178

竖式除法模拟

写这篇博客的目的是为了更好利用数学方法来处理数据,之前遇到了一道题给你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\n",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\n",ans);
22     return 0;
23 }
复制代码

 

 

 

本文作者:王陸

本文链接:https://www.cnblogs.com/wkfvawl/p/9484044.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   王陸  阅读(1879)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起