2017年第八届蓝桥杯C/C++ C组国赛 —— 第四题:小数第n位
標(biāo)題: 小數(shù)第n位
我們知道,整數(shù)做除法時,有時得到有限小數(shù),有時得到無限循環(huán)小數(shù)。
如果我們把有限小數(shù)的末尾加上無限多個0,它們就有了統(tǒng)一的形式。
本題的任務(wù)是:在上面的約定下,求整數(shù)除法小數(shù)點(diǎn)后的第n位開始的3位數(shù)。
輸入:
一行三個整數(shù):a b n,用空格分開。a是被除數(shù),b是除數(shù),n是所求的小數(shù)后位置(0<a,b,n<1000000000)
輸出:
一行3位數(shù)字,表示:a除以b,小數(shù)后第n位開始的3位數(shù)字。
比如:
輸入:
1 8 1
程序應(yīng)該輸出:
125
再比如:
輸入:
1 8 3
程序應(yīng)該輸出:
500
再比如:
輸入:
282866 999000 6
程序應(yīng)該輸出:
914
資源約定:
峰值內(nèi)存消耗 < 256M
CPU消耗 < 1000ms
請嚴(yán)格按要求輸出,不要畫蛇添足地打印類似:“請您輸入…” 的多余內(nèi)容。
所有代碼放在同一個源文件中,調(diào)試通過后,拷貝提交該源碼。
注意: main函數(shù)需要返回0
注意: 只使用ANSI C/ANSI C++ 標(biāo)準(zhǔn),不要調(diào)用依賴于編譯環(huán)境或操作系統(tǒng)的特殊函數(shù)。
注意: 所有依賴的函數(shù)必須明確地在源文件中 #include , 不能通過工程設(shè)置而省略常用頭文件。
提交時,注意選擇所期望的編譯器類型。
笨笨有話說:
這個除法小學(xué)就會算啊,模擬手算除法的過程就可以了吧。
只是數(shù)有點(diǎn)大啊…
管它呢,能算多遠(yuǎn)算多遠(yuǎn)…
歪歪有話說:
如果我能確定循環(huán)節(jié)從哪里開始到哪里結(jié)束,再大的數(shù)不過就是與它取模的余數(shù)等價啊
Code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^0. ^ 0^.0 1 .^.1 ^0 .........001^.1 1. .111100....01^00 11^ ^1. .1^1.^ ^0 0^.^ ^0..1.1 1..^1 .0 ^ ^00. ^^0.^^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成整數(shù)再%1000就OK了, 其實(shí)也不一定非要全部變成整數(shù),并且對于循環(huán)小數(shù)也是不可能變成整數(shù)的, 只需要把n+2之前變成整數(shù),%1000就OK了。(a/b)*pow(10,n+2)%1000由公式:x/d%m=x%(d*m)/da*pow(10,n+2)%(b*1000)/b Functions required by the program:Determination algorithm:Determining data structure:*/ /* My dear Max said: "I like you, So the first bunch of sunshine I saw in the morning is you, The first gentle breeze that passed through my ear is you, The first star I see is also you. The world I see is all your shadow."FIGHTING FOR OUR FUTURE!!! */ #include <cmath> #include <cstdio> #include <iostream>using namespace std;long long a,b,n;long long Q_pow(long long a,long long b,long long mod){long long res=1;while(b){if(b&1) res=res*a%mod;a=a*a%mod;b>>=1;}return res; }int main(){cin>>a>>b>>n;long long mod=b*1000;long long res=Q_pow(10,n+2,mod);long long temp=(a%mod*res%mod)%mod;printf("%03d\n",temp/b);return 0; }總結(jié)
以上是生活随笔為你收集整理的2017年第八届蓝桥杯C/C++ C组国赛 —— 第四题:小数第n位的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017年第八届蓝桥杯C/C++ C组国
- 下一篇: ACM-数论 —— 一.整除的性质