SBBSBSBSBSB @ 2023-12-03 17:29:56
#include<bits/stdc++.h>
using namespace std;
int n,k;
float ansA,ansB,A1,B1,A2,B2;
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
{
if(i%k==0)
{
ansA+=i;
A1++;
}
else
if(i%k!=0)
{
ansB+=i;
B1++;
}
}
A2=ansA/A1;
B2=ansB/B1;
printf("%.1f %.1f",A2,B2);
return 0;
}
by lechenkai @ 2023-12-03 17:39:15
最好用double,精度高、不宜出错。
by CleanIce @ 2023-12-03 17:50:05
@SBBSBSBSBSB
我给你重写吧,把代码写工整一点:
#include <iostream>
#include <iomanip>
int main() {
int n, k;
std::cin >> n >> k;
int count_a = 0, count_b = 0;
int sum_a = 0, sum_b = 0;
for (int i = 1; i <= n; i++) {
if (i % k == 0) {
count_a++;
sum_a += i;
} else {
count_b++;
sum_b += i;
}
}
double res_a = 1.0 * sum_a / count_a;
double res_b = 1.0 * sum_b / count_b;
std::cout << std::fixed << std::setprecision(1) << res_a << " " << res_b << std::endl;
}
by GPUawa @ 2023-12-03 18:00:12
@CleanIce 你这像AI写的()
by GPUawa @ 2023-12-03 18:01:06
@SBBSBSBSBSB 用double
用float一般都会遇到精度问题
by CleanIce @ 2023-12-03 18:08:16
@GPUawa
那是因为 oier 都不按照标准格式规范写。这就是标准格式规范化的代码。
随便找一个做程序开发的都会发现他的代码都是按照标准格式规范写的。
by GPUawa @ 2023-12-03 18:10:47
@CleanIce 不是,
using namespace std;
不符合规范?
by GPUawa @ 2023-12-03 18:15:28
@CleanIce 这个马蜂我叹为观止(
by CleanIce @ 2023-12-03 19:14:48
@GPUawa
C++ 不建议使用预编译命名空间导入指令,很容易就导致撞名或者是重复定义之类的错误。
要用也是编译时命名空间导入指令(区分于预编译):
using std::cin;
using std::cout;
using std::fixed;
using std::setprecision;
using std::endl;
by GPUawa @ 2023-12-03 19:30:00
@CleanIce 刚在小破站看到(