project euler problem 28
2013-10-11 13:03
309 查看
Number spiral diagonals
Problem 28
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
It can be verified that the sum of the numbers on the diagonals is 101.
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
|
#include <iostream> #include <cmath> #include<algorithm> using namespace std; int main() { int k=1,j=0,a=1,b=2,sum=1;//k初始化为第一环,刚开始a等于最里面那个数就是1,b为第个环间的差值,sum等于最里面的那个数就是1,因为等下开始a就变了,故…… while(k++<=2000) //每环有4个数,而要执行500行,因为第一行是1就一个数,本来是501行,但是里面那行已经处理,故执行500就得了,有:4*500=2000 { j++; a+=b; sum+=a; if(j%4==0) b+=2; //因为每一环有四个数,所以当正好四个数完的时候b就增2到下一环 //cout<<sum<<endl; } cout<<sum<<endl; return 0; }
相关文章推荐
- Project Euler Problem 28
- http://projecteuler.net/problem=28 [Answer:669171001]
- Project Euler problem 28
- Project Euler Problem 32
- Project Euler problem 26
- Project Euler problem 9 勾股数
- Project Euler problem 53
- Project Euler Problem 10: Summation of primes
- Project Euler Problem 11: Largest product in a grid
- Project Euler Problem 3 - Largest prime factor
- project euler Problem 53
- Project Euler Solutions(Problem 1~7)
- project euler Problem 36
- http://projecteuler.net/problem=17 [Answer:21124]
- http://projecteuler.net/problem=39 [Answer:840]
- Project Euler -> problem 3
- Project Euler problem 61
- Project Euler problem 67
- Project Euler Problem 60
- project euler problem 22对文件的处理