sweet夏
发表于 2018-3-22 17:30:55
public static void main(String args[]){
//找出1000以下,3和5的倍数
int n = 1000;
int s = 0;
HashSet<Integer> set = new HashSet<Integer>();
for( int i = 1; i <= (n-1)/3; i++ ){
set.add(i*3);
}
for( int j = 1; j <= (n-1)/5; j++ ){
set.add(j*5);
}
//遍历求和
Iterator it = set.iterator();
while(it.hasNext()){
s = s + Integer.valueOf(it.next().toString());
}
System.out.print(s);
}
思路: 与其循环匹配1000次,不如在小于1000的范围内,算出3及5的倍数,利用HashSet 自动剔除重复数。得到结果
但是最后要求求和。所以又多了一个循环,实际计算量也并没有减少,反倒代码更长了。
阿bang
发表于 2018-3-26 14:22:01
def multi3and5(maxnum):
sum = 0
for num in range(3, maxnum):
if num % 3 ==0 or num % 5 == 0:
sum += num
returnsum
print multi3and5(1000)
python
独我雅想
发表于 2018-4-1 17:17:34
public class SumOfMutipes3And5 {
privatestatic long sum = 0;
public long of3And5(long s){
for (int i = 0;i<s;i++)
{
if(i%3 == 0) sum += i;
}
for (int j = 0;j<=s;j++)
{
if (j % 5==0) sum += j;
}
for (int k = 0;k<=s;k++)
{
if (k%15==0) sum -= k;
}
returnsum;
}
public static void main(String[] args) {
SumOfMutipes3And5 sum1 = new SumOfMutipes3And5();
System.out.println(sum1.of3And5(1000));
}
}
soulwyb
发表于 2018-4-11 19:46:59
def sun3_5(n, con1, con2):
sun = 0
for i in range(n):
if i % con1 == 0:
sun += i
elif i % con2 == 0:
sun += i
return sun
sun = sun3_5(1000, 3, 5)
print sun
233168
soulwyb
发表于 2018-4-11 19:54:48
看完各位大佬的 我在改进:
print sum([ i for i in range(1000) if (i % 3 == 0) or (i % 5 == 0)])
py_break
发表于 2018-5-2 15:16:53
sum = 0
for i in range(1000):
if i % 3 == 0 or i % 5 == 0:
sum = sum + i
print(sum)
豆芽小包
发表于 2018-5-24 18:12:08
本帖最后由 豆芽小包 于 2018-5-24 18:20 编辑
def pl(num):
s = []
sum = 0
for i in range(int(num)):
if (i%3==0) or (i%5==0):
s.append(i)
for x in s:
sum = sum + x
print(sum)
num = input('请输入一个正整数:')
pl(num)
答案:233168
野花菜子
发表于 2018-6-20 16:15:56
sum = 0
for n in range(1, 1000):
if n % 3 == 0or n % 5 ==0:
sum += n
print(sum)
1314xxxxxx
发表于 2018-7-10 20:38:23
num_of_3Multiples = 1000 // 3 + 1
num_of_5Multiples = 1000 // 5 + 1
num_of_3and5Multiples = 1000 // (3*5) + 1
sum_of_both_them = 3 * sum(range(1, num_of_3Multiples))+5 * sum(range(1, num_of_5Multiples)) - 15 * sum(range(1, num_of_3and5Multiples))
print(sum_of_both_them)
答案是234168?
如有不对,多请指教
shotgun
发表于 2018-7-16 16:35:04
#include <stdio.h>
int main(void)
{
int sum=0,i;
for(i=1;i<1000;i++)
{
if(i%3==0 || i%5==0)
{
sum+=i;
}
}
printf("%d\n",sum);
return 0;
} {:10_256:}
ゆりお姉さん
发表于 2018-7-19 17:20:11
sum(x for x in range(1000)if x%3==0 or x%5==0)
ZKD
发表于 2018-8-10 22:35:44
def multiple3and5():
sum_res = 0
s3 = 0
s5 = 0
mul3 = 1
mul5 = 1
m3 = (1000 - 0.1)/ 3
m5 = (1000 - 0.1)/ 5
while mul3 <= m3:
if mul3 % 5 != 0:
s3 += mul3
mul3 += 1
s5 = ((1 + int(m5)) * int(m5)) / 2
sum_res = int(s3 * 3 + s5 *5)
return sum_res
a = multiple3and5()
print(a)
分为两个等差数列计算,其中一个采用循环,因为要剔除那些能同时被3和5整除的数字,另外一个直接用等差数列得到答案,这样就可以只用很少的时间开销去解答本题了。
wk1009519920
发表于 2018-8-14 11:09:14
list1=[]
for i in range(1,1000):
if i % 3 ==0 or i % 5 ==0:
list1.append(i)
print(sum(list1))
塔利班
发表于 2018-8-23 09:57:35
sum()
茶侘酒寂
发表于 2018-8-25 17:08:22
本帖最后由 茶侘酒寂 于 2018-8-25 17:10 编辑
pyhton:233168
def mult_3(): #找出3的倍数
t = 0
i = 3
for k in range(1000):
if (i*k > 1000):
return t
# print(k)
t = t + i * k
def mult_5():
t = 0
j = 5
for k in range(1000):
if (j*k >= 1000): #找出5的倍数并排除3倍数
return t
if (k%3 == 0):
#print(k)
continue
#print(k)
t = t + j * k
t = mult_3() + mult_5() #相加,这样运算为535次
print(t)
shirleytse
发表于 2018-9-13 16:36:02
i = 1
sum = 0
while i <= 1000:
if i%3 == 0 or i%5 == 0:
sum += i
i+=1
print(sum)
StephenShi
发表于 2018-9-15 22:57:08
从没考虑过算法速度的我。
ls = []
for i in range(1,1001):
if i % 3 == 0 or i % 5 == 0:
ls.append(i)
print(sum(ls))
>>>234168
StephenShi
发表于 2018-9-15 22:59:41
StephenShi 发表于 2018-9-15 22:57
从没考虑过算法速度的我。
>>>234168
好像还做错了。1000以下{:10_278:}
yuc_mi
发表于 2018-9-17 15:20:32
print("找出 1000 以下的自然数中,属于 3 或 5 的倍数的数字之和")
def getsum(n):
sum = 0
for i in range(0,n):
if i % 3 and i % 5:
continue
else:
sum += i
return sum
n = int(input('请输入一个自然数:'))
result = getsum(n)
print(result)
山有扶苏啊
发表于 2018-9-30 12:25:24
答案233168
mul = []
t = 0
for i in range(1000):
if i % 5 == 0:
mul.append(i)
for j in range(1000):
if j % 3 == 0 and j not in mul:
mul.append(j)
for k in range(0, len(mul)):
t += mul
print(t)
页:
1
2
3
4
5
6
[7]
8
9
10
11
12
13
14