lyciam
发表于 2016-11-15 09:27:58
sum()
我的答案是:233168
〃陌殇、怀素
发表于 2016-11-18 19:51:24
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
int main()
{
int i;
int sum=0;
for(i=0; i<MAX; i++)
{
if((i%3==0) ||( i%5)==0 )
{
sum=sum+i;
}
}
printf("%d 以下的自然数中,属于 3 或 5 的倍数的数字之和:%d \n",MAX,sum);
return 0;
}
Kan丶
发表于 2016-12-4 20:10:59
value = 0
num =
for each in num:
value = value+each
print(value)
yretsym
发表于 2016-12-29 09:37:18
summation = 0 #定义和变量
i = 1 #定义自然数变量
while i < 1000: #循环,让i从1到1000依次代入
if i % 3 ==0 or i % 5 ==0: #先检查i 是不是3的倍数,不是3的倍数再看是不是5的倍数
summation= summation + i#和加上i
i += 1
else:
i += 1
print(summation)
芒果加黄桃
发表于 2017-1-5 22:51:55
from time import time
def method1(n):
start = time()
sum = 0
for i in range(1,n+1):
if i%3==0 or i%5==0:
sum +=i
#print(i)
print('sum=',sum)
end = time()
print('cost %.3f \'s' % (end - start))
def method2(n):
start = time()
sum1 = (3+n//3*3)*(n//3)/2
sum2 = (5+n//5*5)*(n//5)/2
sum3 = (15+n//15*15)*(n//15)/2
sum = sum1 + sum2 - sum3
print('sum=',sum)
end = time()
print('cost %.3f \'s' % (end - start))
method1(1000)
method2(1000)
sum= 234168
cost 0.006 's
sum= 234168.0
cost 0.000 's
渡风
发表于 2017-1-11 18:21:49
%Matlab
%% Problem1
% 题目1:找出1000以下自然数中3和5的倍数之和
function Output=Problem1(Input)
tic
if nargin==0
Input=1000;
end
Output=0;
for ii=1:1:Input-1
if mod(ii,3)==0||mod(ii,5)==0
Output=Output+ii;
end
end
toc
disp(['Problem1所用时间为',num2str(toc)])
disp(['Problem1的答案为',num2str(Output)])
hq333
发表于 2017-1-17 21:16:30
看了大家的帖子,综合起来,写了下面的(也是233168):
from time import time
def method1(n):
start = time()
sum = 0
for i in range(1,n):
if i%3==0 or i%5==0:
sum +=i
#print(i)
print('sum=%d'%sum)
end = time()
print('cost %.3f \'s' % (end - start))
def method2(n):
start = time()
n =n-1
sum1 = (3+n//3*3)*(n//3)/2
sum2 = (5+n//5*5)*(n//5)/2
sum3 = (15+n//15*15)*(n//15)/2
sum = sum1 + sum2 - sum3
print('sum=%d'%sum)
end = time()
print('cost %.3f \'s' % (end - start))
def method3(n):
start = time()
print sum(i for i in range(0,n) if i%3==0 or i%5==0)
end = time()
print('cost %.3f \'s' % (end - start))
def method4(n):
start = time()
sum = 0
for i in range(1,n):
if i*3<n:
sum +=i*3
else:
break
if i*5<n:
sum +=i*5
if i*15<n:
sum -=i*15
print('sum=%d'%sum)
end = time()
print('cost %.3f \'s' % (end - start))
num=1000
method1(num)
method2(num)
method3(num)
method4(num)
运行结果:
sum=233168
cost 0.000 's
sum=233168
cost 0.000 's
233168
cost 0.001 's
sum=233168
cost 0.001 's
hq333
发表于 2017-1-17 21:28:20
可见1000之内运行时间难比较,如果num=100000,运行结果如下(时间就有差别了):
sum=2333316668
cost 0.016 's
sum=2333316668
cost 0.000 's
2333316668
cost 0.017 's
sum=2333316668
cost 0.010 's
结论:method2直接计算无异是最快的,
method4虽然繁复些,循环次数少了很多,如果数据量大还是省时些,
method3比method1慢一丁点。
kalao
发表于 2017-1-20 16:51:01
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#include<algorithm>
int main()
{
int i;
int sum=0;
sum+=(333*3)+333*(332/2)*3;
sum+=(199*5)+199*(198/2)*5;
sum-=(66*15)+(66/2)*(65)*15;
cout<<sum;
return 0;
}
shinningpika
发表于 2017-1-22 13:24:46
a=[]
for i in range(1,1000):
if i%3==0 or i%5==0:
a.append(i)
b=set(a)
c=0
for i in range(1,1000):
if i in b:
c+=i
print(c)
笨蛋方法,最后答案233168
Hughues
发表于 2017-1-23 21:51:02
# Python 3代码
n = 0
for num in range(1,1000) :
if num % 3 == 0 or num % 5 == 0 :
n = num + n
num = num + 1
else :
num = num + 1
print (n)
# 解题思路:首先定义几个变量,通过循环找出从1—1000的每一个该找的值,然后把它们相加即可
# 下面附函数版本:
def summation(x=1,i=1,j=1) :
num = 0
n = 1
if x <= 1 or i > x or j > x :
print ('You had make a mistick,please chick it')
else :
for n in range(1,x) :
if n % i == 0 or n % j == 0 :
num = n + num
n = n + 1
else :
n = n + 1
print(num)
# 以上
{:10_266:}看看C语言的一大长串,Python还真是方便初学者理解
FlySelf
发表于 2017-2-2 01:26:42
import time
def multi_1(num):
'使用while循环'
count = 0
while num:
num -= 1
if num % 3 == 0 or num % 5 == 0:
count += num
return count
def multi_2(num):
'使用for循环'
count = 0
for i in range(0, num):
if i % 3 == 0 or i % 5 == 0:
count += i
return count
start_1 = time.clock()
print(multi_1(1000))
end_1 = time.clock()
print('程序按while循环执行了%fs。' %(end_1 - start_1))
start_2 = time.clock()
print(multi_2(1000))
end_2 = time.clock()
print('程序按for循环执行了%fs。' %(end_2 - start_2))
执行结果:
233168
程序按while循环执行了0.004329s。
233168
程序按for循环执行了0.002159s。
算法相同,for循环的速度竟然更快2333
0mrli0
发表于 2017-2-19 16:23:53
/*找出1000以下自然数中3和5的倍数之和*/
#include <stdio.h>
int main()
{
int sum = 0;
int i;
for(i=1; i<1000; i++)
{
if(i%3==0 || i%5==0)
sum += i;
}
printf("sum = %d", sum);
return 0;
}
marmot
发表于 2017-2-20 21:13:45
print("""问题: 10 以下的自然数中,属于 3 或 5 的倍数的有 3, 5, 6 和 9,它们之和是 23,
找出 1000 以下的自然数中,属于 3 或 5 的倍数的数字之和。
----------------------------------------------------------------""")
num = sum()
print('答案是: '+str(num))
6leensky
发表于 2017-2-24 07:38:54
s = 0
for i in range(1000):
if i%3 == 0 or i%5 == 0:
s += i
print(s)
naplq
发表于 2017-2-27 16:20:40
a = []
for i in range(1,1000):
if i%3 ==0 or i%5 == 0:
a.append(i)
print(sum(a))
夜魔时生
发表于 2017-3-6 10:54:42
sum=0
for i in range(1,1000):
if i%3==0 or i%5==0:
sum+=i
print(sum)
testabc123
发表于 2017-3-9 15:37:53
#!/usr/bin/env python
#coding:utf-8
def Sum(n=10):
num1=0
for i in list(range(n)):
if i%3 == 0 or i%5==0:
num1+=i
print(num1)
if __name__ == '__main__':
Sum(1000)
guochangyu
发表于 2017-3-11 16:45:49
233168
99592938
发表于 2017-3-14 10:11:33
number=999
s=[]
while number:
if number%3==0 or number%5==0:
s.append(number)
number-=1
print(sum(s))
print(s)
页:
1
2
3
[4]
5
6
7
8
9
10
11
12
13