1. 程式人生 > >沒錯,這都是套路,

沒錯,這都是套路,

整除個數

時間限制:3000 ms  |  記憶體限制:65535 KB

難度:1

輸入

輸入包含多組資料
每組資料佔一行,每行給出兩個正整數n、b。

輸出

輸出每組資料相應的結果。

樣例輸入

2 1
5 3
10 4

樣例輸出

2
1
2

來源

自編

上傳者

mix_math

描述

1、2、3… …n這n(0<n<=1000000000)個數中有多少個數可以被正整數b整除。

 

一開始我們的慣性思維便是取餘為零計數,因為他是longlong型別,你會發現他給你的測試資料沒次都很完美的通過,可一直顯示的超時。找細節找吖找,最後還是超時(附上圖),沒錯N次超時後發現自己臉燙燙的,滿滿的套路!再附上自己的程式碼跟正確程式碼。

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    

        long n;
        while(scanf("%ld",&n)!=0){
    
long b;
    scanf("%ld",&b);
        int s=1,i;
        for(i=b;i<n;i++)
        if(i%b==0)
        s++;
    printf("%d\n",s);
    }    
    return 0;
}

#include <stdio.h>
int main(){
long n,b;
while (scanf("%ld%ld",&n,&b)) {
printf("%ld\n",n/b);

}
return 0;
}