竖式除法模拟
写这篇博客的目的是为了更好利用数学方法来处理数据,之前遇到了一道题给你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
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 }
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 a, b, c (1 ≤ a < b ≤ 105, 0 ≤ 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
1 2 0
2
2 3 7
-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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)