「LYHOI R4」于辰的数学题解
ZycNotFound · · 个人记录
Normal
解题思路
我们看到 「LYHOI R4」于辰的数学。
我们知道,导弹拦截率为
由于各枚拦截弹相互独立,
🕳
精度问题
std::cout 默认保留
我赌你的枪里没有子弹
看到 Cjy277131的提交 和 kaikaikaihuaya的提交:
Cjy277131:
#include <bits/stdc++.h>
using namespace std;
double k;int n;
int main(){
cin>>k>>n;
k=1-k;
double v=k;
for(int i=1; i<n; i++){
k=k*v;
}
printf("%.15lf",1-k);
return 0;
}
kaikaikaihuaya:
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline void solve(){
double k,k1;
int n;
scanf("%lf%lld",&k,&n);
k=1.0-k,k1=k;
for(int i=1;i<n;i++){
k1=k1*k;
}
printf("%.15lf\n",1.0-k1);
}
signed main(){
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
int T=1;
// cin>>T;
while(T--){
solve();
}
}
他们 WA 了 #6 和 #8,我们来看看数据:
6: 0.7 0
8: 1 0
相信大家都看出来了,此处
这期神了,kaikaikaihuaya 还跟我说他是 DeepSeek 做的。
这个故事告诉我们什么道理?(2分)
:::info[参考答案]
永远不要相信AI的代码
答:目中无数据,AC两行泪。
:::
AC代码:
使用 pow() 函数的代码:
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
double k;
int n;
cin >> k >> n;
cout << fixed << setprecision(15) << 1 - pow(1 - k, n) << '\n';
return 0;
}
使用循环的代码:
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
double k;
int n;
cin >> k >> n;
double s = 1;
for (int i = 1; i <= n; ++i) s *= (1 - k);
cout << fixed << setprecision(15) << 1 - s << '\n';
return 0;
}
Extra
解题思路
每发会在上一发失败的
AC代码
因此可以得到如下代码:
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
double k;
int n;
cin >> k >> n;
double ans = 0;
for (int i = 0; i < n; ++i) {
ans += pow(1 - k, i) * k;
}
cout << fixed << setprecision(15) << ans << '\n';
return 0;
}
错误示范
看到 DFyyds的提交:
#include<bits/stdc++.h>
using namespace std;
double k;
double ans;
int n;
int main(){
cin>>k>>n;
ans=k;
for(int i=1;i<n;i++){
ans+=pow((1-k),i)*k;
}
cout<<ans;
return 0;
}
此处的错误与 🕳 中的 精度问题 和 我赌你的枪里没有子弹 错误相同,不再赘述。