【算法题】找整除

来源:互联网 发布:课堂游戏 知乎 编辑:程序博客网 时间:2024/06/14 06:58

牛牛想在[a, b]区间内找到一些数满足可以被一个整数c整除,现在你需要帮助牛牛统计区间内一共有多少个这样的数满足条件? 输入描述:
首先输入两个整数a,b,(-5*10^8 ≤ a ≤ b ≤ 5*10^8) 接着是一个正整数c(1 <= c <= 1000)

输出描述: 输出一个整数表示个数。

输入例子: 0 14 5

输出例子: 3


除法可以用减法实现


#include <iostream>#include <string>#include <algorithm>using namespace std;//#define debug_int func(int a,int b,int c){    int curr = b;    while (curr%c)    {        curr--;    }    int count(0);    while (curr>=a)    {        curr -= c;        count++;    }    return count;}int main(){    int a, b;    int c;#ifdef debug_    a = 0;    b = 14;    c = 5;#else    cin >> a;    cin >> b;    cin >> c;#endif    cout << func(a,b,c);    return 0;}