baru003のブログ

baruの雑記兼備忘録

PE 0001

・問題リンク Add all the natural numbers below one thousand that are multiples of 3 or 5.

・コメント
1000未満の自然数のうち3か5の倍数になっている数字の合計を求める問題です。
そのまま実装してacceptできました。

・ソース

import java.util.*;

public class P0001 {

	public static void main(String[] args) {
		int ans = 0;
		for (int i = 0; i < 1000; i++) {
			if (i % 3 == 0 || i % 5 == 0) {
				ans += i;
			}
		}
		System.out.println(ans);
	}

}