牛牛想在[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
*/