[编程题] 找整除

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


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

输入例子:
0 14 5

输出例子:
3
/*
分情况讨论:
    1. 区间包括0的时候,要对0进行判断
        都是0
        都不是0
        一个是0
    2. 不包括0的时候,在同一边
            两个数相等
            两个数不等
                在0的左边的时候
                    判断大的数是否是给定数的倍数,如果是,结果加1,
                    例如下面两组数据
                    -15 -5 5
                    -15 -4 5
                在0的右边的时候
                    同上,看下面两组数据
                    5 15 5
                    4 15 5

*/
#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#define ll long long

using namespace std;

int n, m, k;
int main()
{
    while (cin >> n >> m >> k)
    {
        if (n > m)
        {
            int t = n;
            n = m;
            m = t;
        }
        if (n == m)
        {
            if (n % k == 0)
                cout << 1 << endl;
            else
                cout << 0 << endl;
            continue;
        }
        if (n < 0 && m > 0)
        {
            int num = 1;
            num += abs((int)(n / k));
            num += abs((int)(m / k));
            cout << num << endl;
            continue;
        }
        if (n == 0)
        {
            int num = 1;
            num += abs((int)(m / k));
            cout << num << endl;
            continue;
        }
        if (m == 0)
        {
            int num = 1;
            num += abs((int)(n / k));
            cout << num << endl;
            continue;
        }
        if (n < 0 && m < 0)
        {
            int num = 0;
            num += abs((int)(n / k)) - abs((int)(m / k));
            if (m % k == 0)
                num += 1;
            cout << num << endl;
            continue;
        }
        if (n > 0 && m > 0)
        {
            int num = 0;
            num += abs((int)(m / k)) - abs((int)(n / k));
            if (n % k == 0)
                num += 1;
            cout << num << endl;
            continue;
        }
    }
    return 0;
}
/*
5 15 5
-15 -5 5
0 0 5
-15 15 1
5 5 5
-5 -5 5
0 15 5
-15 0 5
4 15 5
*/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值