藍橋杯試題 歷屆試題 小數第n位
題目描述:
資源限制
時間限制:1.0s 內存限制:256.0MB
問題描述
我們知道,整數做除法時,有時得到有限小數,有時得到無限循環小數。
如果我們把有限小數的末尾加上無限多個0,它們就有了統一的形式。
本題的任務是:在上面的約定下,求整數除法小數點後的第n位開始的3位數。
輸入格式
一行三個整數:a b n,用空格分開。a是被除數,b是除數,n是所求的小數後位置(0<a,b,n<1000000000)
輸出格式
一行3位數字,表示:a除以b,小數後第n位開始的3位數字。
樣例輸入
1 8 1
樣例輸出
125
樣例輸入
1 8 3
樣例輸出
500
樣例輸入
282866 999000 6
樣例輸出
914
AC代碼:
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <math.h>
#include <map>
using namespace std;
int ans[3];
int main()
{
int a,b,n,i;
cin>>a>>b>>n;
a=a%b;
int tt;
int k=0;
for(i=1;;i++)
{
a=a*10;//a爲不斷更新的被除數
tt=a/b;//tt爲不斷更新的得數
a=a%b;
if(i==n||i==n+1||i==n+2)//存儲在ans數組中便於輸出
ans[k++]=tt;
if(k==3)
break;
}
for(i=0;i<3;i++)
cout<<ans[i];
cout<<endl;
return 0;
}