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

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

原题链接:https://www.luogu.com.cn/problem/P5719


题目描述

给定 \(n(n\le10000)\)\(k(k\le 100)\),将从 1 到 \(n\) 之间的所有正整数可以分为两类:A 类数可以被 \(k\) 整除(也就是说是 \(k\) 的倍数),而 B 类数不能。请输出这两类数的平均数,精确到小数点后 1 位,用空格隔开。

数据保证两类数的个数都不会是 0。

输入格式

输出格式

输入输出样例

输入 #1

100 16

输出 #1

56.0 50.1

C++代码

#include <cstdio>
using namespace std;

int main() {
    int n, k, sumA, sumB, countA, countB;
    scanf("%d%d", &n, &k);
    sumA = sumB = countA = countB = 0;
    for (int i=1; i<=n; ++i)
        if (i % k) {
            sumB += i;
            ++countB;
        } else {
            sumA += i;
            ++countA;
        }
    printf("%.1f %.1f\n", sumA * 1.0 / countA, sumB * 1.0 / countB);
    return 0;
}
posted @ 2020-07-18 08:48  yuzec  阅读(1154)  评论(0编辑  收藏  举报