whytree

导航

 

分数a/b化为小数后,小数点后第n位的数字是多少?
【输入】
三个正整数a,b,n
相邻两个数之间用单个空格隔开。0<a<b<100,1<=n<=10000
【输出】
一个数字

错解

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	double a,b,n;
	cin>>a>>b>>n;
	double x=a/b;
	int y=x*10*n;
	y=y%10;
	cout<<y;
   return 0;

错误原因在于n过大,超出了double类型的储存范围

正解

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int a=0,b=0;
	int n,ans=0;
	cin>>a>>b>>n;
	
	for(int i=1;i<=n;i++)
	{
		int r=a%b;
		ans=r*10/b;
		a=r*10;
	}
	cout<<ans;
    return 0;    
}

回归小数计算本质——竖式除法

posted on 2026-01-18 20:46  why_tree  阅读(0)  评论(0)    收藏  举报