1. 程式人生 > >【演算法題】找整除

【演算法題】找整除

牛牛想在[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; }