Little_Elephant @ 2021-06-02 19:18:53
样例都对,但就是不知道哪错了。故求助大佬!
#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;
int main()
{
int i,n,k;
int ans = 0,total = 0;
int a,b;
cin >> n >> k;
for(i = 1; i <= n ; i++)
{
if(i % k != 0)
{
b += i;
total++;
}
if(i % k == 0)
{
a += i;
ans++;
}
}
cout << fixed << setprecision(1) << a/ans << ".0" << " " <<(double)b / total;
return 0;
}
by Nt_Tsumiki @ 2021-06-02 19:22:55
你用一下printf
by Nt_Tsumiki @ 2021-06-02 19:23:39
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n,k;double ai=0,arr=0,bi=0,brr=0;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
if(i%k==0)
{
ai+=i;
arr++;
}
else
{
bi+=i;
brr++;
}
}
printf("%.1lf %.1lf",ai/arr,bi/brr);
}
by Little_Elephant @ 2021-06-03 13:38:50
谢谢!
by Lucas_2021 @ 2021-07-14 09:35:33
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n,k;
cin >> n >> k;
int a = 0;
int b = 0;
int a2 = 0;
int b2 = 0;
for (int i = 1; i <= n; i++)
{
if (i % k == 0)
{
a = a + i;
a2 = a2 + 1;
}
else
{
b = b + i;
b2 = b2 + 1;
}
}
cout<<fixed<<setprecision(1) << a * 1.0 / a2 << " " << b * 1.0 / b2;
return 0;
}