poshahaha @ 2021-04-09 22:43:16
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n, k;
int sum1 = 0, sum2 = 0;
int n1 = 0, n2 = 0;
int i;
cin >> n >> k;
for (i = 1; i <= n; i++)
{
if (i % k == 0)
{
n1++;
sum1 += i;
}
else
{
n2++;
sum2 += i;
}
}
cout << setiosflags(ios::fixed) << setprecision(1) << double(sum1 / n1) << ' ' << double(sum2 / n2);
return 0;
}```
by 渡鸦2007 @ 2021-04-09 22:50:54
@poshahaha double(sum1 / n1)
改为double(sum1) / n1
by 渡鸦2007 @ 2021-04-09 22:51:34
@fqyz_wyc 你的写法是先算出答案再类型转换,结果会给你整除
by HanPi @ 2021-04-09 22:56:03
c/c++ 中整型除以整型默认结果为整型,转成double类型再除就行了
double((double)sum1 / (double)n1) << ' ' << double((double)sum2 / (double)n2)
by Qzong @ 2021-04-09 23:28:58
其实
(double)a/b就够了
by poshahaha @ 2021-04-10 13:38:18
@fqyz_wyc 感谢
by poshahaha @ 2021-04-10 13:38:36
@HanPi 感谢
by poshahaha @ 2021-04-10 13:38:53
@Qzong 感谢