题意
求小数点后第n位数,即可得知无需考虑整数位。
方法0
看到题目笔者第一时间想到的是将小数点后的部分转换为整数(例如40.357转换为357),再转换为字符串类型(即将357转换为“357”)。
但在十几分钟的操作后,代码如下:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
string int_to_str(int k);
int t[50];
int main()
{
double x,a,b,n;
cin>>a>>b>>n;
x=a/b;
x=x-floor(x);//去除小数点前无用的整数部分;
while( int(x) != x )
x*=10;//将 x 小数点后的部分转换为整数;
string ans=int_to_str(x);//将小数点后的部分转化为string类型;
cout<<ans[n-1];
// 因为 string 类型是从 0 开始的(数组等也是);
return 0;//所以输出 ans[n-1];
}
string int_to_str(int k)// int 转 string 函数;
{
int b=k,i=0;
char c[50];
string s="";
while( b != 0 )
{
c[i]=b%10+48;
b/=10;
i++;
}
for(int j = i-1;j >=