求助,为何数据精度上出现错误

P5719 【深基4.例3】分类平均

Do1phln @ 2020-07-17 10:30:25

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
    int n,k;
    cin>>n>>k;
    int a[10001],b[10001],ta = 0,tb = 0;
    int suma,sumb;
    double avea,aveb;
    for(int i = 1; i <= n;i++)
    {
        if(i%k == 0)
        {
            a[ta] = i;
            ta++;
        }
        else
        {
            b[tb] = i;
            tb++;
        }
    }
    for(int i = 0;i < ta; i++)
    {
        suma+=a[i];
    }
    for(int i = 0;i < tb; i++)
    {
        sumb+=b[i];
    }
    avea = suma / ta;
    aveb = sumb / tb;
    printf("%.1lf, %.1lf", suma / (double)ta, sumb / (double)tb);
    return 0;
}

求助各位大佬,为何跑样例时第二个数据结果为50.2QAQ


by 东方_east @ 2020-07-17 11:14:44

@OERROR 比如

double a;
a=3/2;//这个得1
a=3.0/2.0;//这个得1.5

by Do1phln @ 2020-07-29 21:35:30

@东方_east 哦哦,好的,谢谢大佬!


上一页 |