【一天一道编程题】之Count Divisors

[复制链接]
653|2
手机看帖
扫描二维码
随时随地手机跟帖
michael_llh|  楼主 | 2017-1-8 23:23 | 显示全部楼层 |阅读模式
本帖最后由 michael_llh 于 2017-1-8 23:25 编辑

Count Divisors
You have been given 33 integers l, r and k. Find how many numbers between l and r (both inclusive) are divisible by k. You do not need to print these numbers, you just have to find their count.
Input Format
The first and only line of input contains 3 space separated integers l, r and k.
Output Format
Print the required answer on a single line.
Constraints
1≤l≤r≤10001≤l≤r≤1000
1≤k≤1000
SAMPLE INPUT

1 10 1
SAMPLE OUTPUT

10
题意分析:
其实这道题非常简单,就是给定三个数值,前两个是一个范围,这两个端点都是包含的,然后最后一个数字是一个除数,于是说在这两个数值之间有多少个被第三个数整除的,输出这个计数的值就可以了。

参考代码:

#include <iostream>
using namespace std;

int main()
{
    int l,r,k;
    cin >> l >> r >> k;
    int count = 0;
    for(int i=l; i<=r; i++){
        if(i%k==0)
            count ++;
    }
    cout << count << endl;
    return 0;
}



相关帖子

michael_llh|  楼主 | 2017-1-9 22:00 | 显示全部楼层
yyy71cj 发表于 2017-1-9 21:18
题目意思不是很清楚的样子

是哪个地方不清楚呢?
给定三个数,第一个和第二个是一个范围的整数集合,两端都包含,然后这中间的所有数字每一个除以第三个数,能除尽就统计个数

使用特权

评论回复
michael_llh|  楼主 | 2017-1-10 11:00 | 显示全部楼层
yyy71cj 发表于 2017-1-10 08:05
哦哦,我还以为是某个应用场景中的什么算法,看来是个数学问题了……

恩,是的。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

22

主题

381

帖子

8

粉丝