songsammy @ 2024-04-19 08:47:51
#include <stdio.h>
int main(){
int n,k;
scanf("%d %d",&n,&k);
int c = n/k;
long int ttl = n*(n+1)/2;
double s1 = k*(c+1)/2;
double s2 = (ttl-c*s1)/(n-c);
printf("%.1f %.1f",s1,s2);
}
double s1 = k*(c+1)/2;
这一行,如果改为
double s1 = (c+1)/2*k;
则错误。为啥?
by 编码落寞 @ 2024-04-19 08:57:37
@songsammy
因为你这里都是整数处理,除法直接取整。
double s1 = k*(c+1)/2;
double s1 = (c+1)/2*k;
已k=2、c=2为例: 上面就为3,下面为2,可以前面加个1.0*
by songsammy @ 2024-04-19 09:00:40
十分感谢。