題目相關
題目鏈接
AtCoder Beginner Contest 169 C題,https://atcoder.jp/contests/abc169/tasks/abc169_c。
Problem Statement
Compute A×B, truncate its fractional part, and print the result as an integer.
Input
Input is given from Standard Input in the following format:
A B
Output
Print the answer as an integer.
Samples1
Sample Input 1
198 1.10
Sample Output 1
217
Explaination
We have 198×1.10=217.8. After truncating the fractional part, we have the answer: 217.
Samples2
Sample Input 2
1 0.01
Sample Output 2
0
Samples3
Sample Input 3
1000000000000000 9.99
Sample Output 3
9990000000000000
Constraints
- 0 ≤ A ≤ 10^15
- 0 ≤ B < 10
- A is an integer.
- B is a number with two digits after the decimal point.
題解報告
慚愧,本題在比賽的時候竟然兩次出現 WA。哎,真的是菜死了。
之所以記錄本題,不是本題有多少難,其實本題是一個水題。本題考查的知識點竟然又是浮點數的精度問題。這個問題和測試服務器有關,畢竟我們是在 AtCoder 的服務器上比賽。我真服了,這個知識點好像國內的 OJ 中比較難得考到,也可能是我進到的題目不夠多。
WA 過程
第一次 WA 代碼
#include <bits/stdc++.h>
using namespace std;
int main(){
long long a;
double b;
scanf("%lld%lf", &a, &b);
b = a*b;
printf("%lld\n", (long long)b);
return 0;
}
第二次 WA 代碼
#include <bits/stdc++.h>
using namespace std;
int main(){
long long a;
double b;
scanf("%lld%lf", &a, &b);
b = a*(b*100)/100;
printf("%lld\n", (long long)b);
return 0;
}
AC 參考代碼
連續兩次 WA 後,我認真的閱讀了一下題目,該題目沒有什麼難度啊。不可能出現 WA。唯一的可能就是傳說中的浮點數精度不夠導致。我真服氣,AtCoder 好像特別喜歡考察這個知識點。
怎麼辦呢,那就提升精度唄。根據題目的意思,浮點數是一個兩位的浮點數,那麼我們將這個浮點數擴大一百倍就可以了。直接用 double 讀如,擴大 100 倍是不行的。可以考慮用字符串讀入。
#include <bits/stdc++.h>
int main() {
unsigned long long a, b;
char s[6];
scanf("%lld%s", &a, s);
b = (s[0]-'0')*100 + (s[2]-'0')*10 + (s[3]-'0');
printf("%llu\n", a*b/100);
return 0;
}
等 AtCoder 將測試用例放出,我再進一步研究一下數據。再次觸及知識的盲點。