99592938
发表于 2017-3-14 10:21:13
maoguy 发表于 2015-10-29 02:06
答案233168
太麻烦了,i%3==0 or i%5==0
546397641
发表于 2017-3-15 16:10:04
sum = 0
for a in range(1,1000):
if a%3 == 0 or a%5 == 0:
sum += a
print(sum)
结果:233168
yuzhitai
发表于 2017-3-19 23:43:50
#include<stdio.h>
int main(){
int a;
int b;
int c;
int d;
for( a=1;a<=1000;a++){
b=a*3;
c=a*5;
d=b+c+d;
}
printf("%d",d);
getchar();
}
~
凌晨分子
发表于 2017-4-3 00:04:56
sum()
结果:233168。
思路:通过for循环遍历0-999,对3和5的倍数取出存入一个列表中,再通过sum函数将生成的列表元素(也就是3和5的倍数)进行相加,算出结果。
疑问:在判断3和5倍数的地方有没有更好的写法?
天之南
发表于 2017-4-30 19:19:14
#include<stdio.h>
int main()
{
const int x = 1000;
int sum = 0;
for (int i = 3; i < x; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
printf("%d\n", sum);
return 0;
}
铭记太阳
发表于 2017-5-4 12:46:19
答案正解233168
这里用数学方法来缩短程序运行时间,计算3的求和+5的求和-多余的15的求和即可。
#include<stdio.h>
#include<stdlib.h>
//为简化代码,只支持30以上的运算
int main(void)
{
int lim,Sn=0;
printf("enter the limit:\t");
scanf("%d",&lim);
if(lim<31) exit(1);
lim--;
Sn=(3+lim/3*3)*(lim/3)/2+(5+lim/5*5)*(lim/5)/2-(15+lim/15*15)*(lim/15)/2;
printf("sum = %d",Sn);
return 0;
}
Python_meng
发表于 2017-5-14 20:11:23
mul3 = 3
mul5 = 5
mul15 = 15
lim = 1000
if mul3 <= lim:
mul3 += 3
if mul5 <= lim:
mul5 += 5
if mul15 <= lim:
mul15 += 15
sum = mul3 + mul5 - mul15
请大神指教
Python_meng
发表于 2017-5-14 20:28:45
mul3 = 3
mul5 = 5
mul15 = 15
lim = 1000
sum3 = 3
sum5 = 5
sum15 = 15
while mul3 < lim:
mul3 += 3
sum3 +=mul3
while mul5 < lim:
mul5 += 5
sum5 += mul5
while mul15 < lim:
mul15 += 15
sum15 += mul15
sum = sum3 + sum5 - sum15
print(sum)
求大神给看下是哪里错了
Python_meng
发表于 2017-5-14 20:35:21
gongxiaobo2006 发表于 2015-4-24 14:26
把代码贴一下,看看对不对,哪位大婶有牛鼻的,希望不吝赐教阿!
sum=0
for i in range(0,1000):
不用写i = i+1
sum = 0
for i in range(1000):
if i%==0 or i%5==0:
sum +=i
print(sum)
这样子就可以了
进击的小蜗牛
发表于 2017-5-17 10:31:16
i = 1
m = 1
while i <= 1000:
if i % 3 == 0 or i % 5 == 0:
m = m + i
i += 1
print(m)
我认为不用再去区分是不是同时是3和5的倍数,应为3和5的倍数也符合if条件
渡漫
发表于 2017-5-18 19:35:17
sum = 0
i = 0
while i<1000:
if i%3==0 or i%5==0:
sum = sum + i
i = i + 1
print(sum)
Python新手,结果是233168
jyl4788
发表于 2017-5-31 07:51:54
用的是python,结果是233168
def natural(n):
sum = 0
for each in range(n):
if each % 3 == 0 or each % 5 == 0:
sum = sum + each
return sum
愤怒的大头菇
发表于 2017-6-4 20:35:00
本帖最后由 永恒的蓝色梦想 于 2020-7-2 18:29 编辑
count = 0
for i in range(1, 1000):
if not i % 3 or not i % 5:
count += i
print(count)
答案:233168
悠然随心
发表于 2017-6-15 16:25:56
def sum(to):
result = 0
for i in range(1,to):
if i%3 == 0 or i%5 == 0:
result += i
return result
print(sum(1000))
悠然随心
发表于 2017-6-15 16:27:52
本帖最后由 悠然随心 于 2017-6-15 16:34 编辑
def sum(to):
result = 0
i = 3
while i < to:
result += i
i += 3
i = 5
while i < to:
result += i
i += 5
i = 15
while i < to:
result -= i
i += 15
return result
print(sum(1000))这种写法代码长一些,但运行效率比上一楼要高
qq542201801
发表于 2017-6-22 17:42:04
num_list = []
for i in range (1,1000):
if i % 3 == 0 or i % 5 == 0:
num_list.append(i)
print (sum (num_list))
fatsky
发表于 2017-7-1 17:29:31
简单粗暴
#include <stdio.h>
int tot;
int main()
{
for (int i = 3; i < 1000; i++)
if (i % 3 == 0 || i % 5 == 0)
tot += i;
printf("%d", tot);
return 0;
}
fatsky
发表于 2017-7-1 17:58:05
本帖最后由 fatsky 于 2017-7-1 18:04 编辑
仿筛法:
#include <cstdio>
int tot,num;
int main()
{
for (int i = 3; i < 1000; i += 3) num == 1;
for (int i = 5; i < 1000; i += 5) num = 1;
for (int i = 3; i < 1000; i++)
if (num)tot += i;
printf("%d", tot);
return 0;
}
Plinskin
发表于 2017-7-3 17:21:56
本帖最后由 Plinskin 于 2017-7-3 17:27 编辑
print "To Find The Sum Of All The Multiples Of 3 Or 5 Below 1000"
counterNum=0
Sum = 0
i = 0
whilei <= 1000:
if i % 3 == 0 or i % 5 == 0:
counterNum = i
Sum = Sum + counterNum
i += 1
else:
i += 1
print "the Final Sum is:" + str(Sum)
===运行结果====
/usr/bin/python /Users/plinskin/PycharmProjects/untitled1/分数.py 1
To Find The Sum Of All The Multiples Of 3 Or 5 Below 1000
the Final Sum is:234168
Process finished with exit code 0
为啥我算出来是234168,我看了步进,在15这种即整除3又整除5的地方也只计算一次呀
Plinskin
发表于 2017-7-3 17:33:56
悠然随心 发表于 2017-6-15 16:27
这种写法代码长一些,但运行效率比上一楼要高
确实是,参与计算的都是符合要求的数字
页:
1
2
3
4
[5]
6
7
8
9
10
11
12
13
14