資源限制
時間限制:1.0s 內存限制:256.0MB
問題描述
小於10的自然數中有四個數字能除盡3或5(3, 5, 6, 9),它們的和爲23。
請計算所有小於1000的自然數中能除盡3或5的數字的合。然後使用標準輸出cout,輸出你的結果。
輸入格式
無。
輸出格式
一行一個整數,表示你的結果。
解題思路:
for循環或者while循環,直接就能得出結果,沒有任何技巧,就是在循環中判斷是否能被3整除或者能否被5整除,代碼如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int count = 0;
for(int i = 1; i < 1000; i ++){
if(i % 3 == 0 || i % 5 == 0){
count += i;
}
}
cout << count;
return 0;
}