编程小贝壳 @ 2020-10-29 11:37:59
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,k,suma,sumb,numa,numb;
cin>>n>>k;
for(int i=1;i<=n;i++){
if(i%k==0){
suma += i;
numa++;
}
else{
sumb += i;
numb++;
}
}
cout<<setprecision(1)<<suma/numa<<' ';
cout<<setprecision(1)<<sumb/numb;
}
求助
by Waaifu_D @ 2020-10-29 11:45:24
你把定义变量放到主函数外边再试试
by Waaifu_D @ 2020-10-29 11:45:46
在主函数内定义会有奇怪的现象(
by Waaifu_D @ 2020-10-29 11:46:15
#include<bits/stdc++.h>
using namespace std;
int n,k,suma,sumb,numa,numb;
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++){
if(i%k==0){
suma += i;
numa++;
}
else{
sumb += i;
numb++;
}
}
cout<<suma/numa<<' ';
cout<<sumb/numb;
}
by ⚡zhangjingcan⚡ @ 2020-10-29 12:18:41
@编程小贝壳 主函数内定义变量不会赋值为0
by 年华天地 @ 2020-10-29 12:19:16
变量定义在函数里初值不为0
by Light_snow @ 2020-10-29 12:20:16
在主函数中定义变量要赋初值
不然初值出现问题
by 蛋蛋工作室 @ 2020-12-29 22:23:33
要设定初始值为0
by 蛋蛋工作室 @ 2020-12-29 22:27:31
#include<bits/stdc++.h>
using namespace std;
int main()
{ int i,n,k,c=0,d=0,e=0,f=0;
cin>>n>>k;
for (i=1;i<=n;i++)
{ if (i%k==0)
{ c=c+i;
d=d+1;
}
else
{ e=e+i;
f=f+1;
}
}
printf ("%.1lf",(double)c/d);
cout<<" ";
printf ("%.1lf",(double)e/f);
return 0;
}
by 蛋蛋工作室 @ 2020-12-29 22:27:49
这样就可以了
by cinderellawlg @ 2021-01-19 21:59:08
@蛋蛋工作室 为什么?我的改成Double型就由40变100了?谢谢