-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem 28.cpp
62 lines (51 loc) · 1.11 KB
/
Problem 28.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* 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>
using namespace std;
int diagsum(int size)
{
int num_now = 1;
int half, last_half = 0;
int sum = 1;
// input ought to be odd
if ( size % 2 == 0 || size < 0) {
cerr << "Size input should be positive odd number!" << endl;
return -1;
}
if ( size == 1 )
return sum;
for (int i = 3; i <= size; i = i + 2) {
half = i / 2;
// go right for 1 step
num_now++;
// go to southeast
num_now += last_half + half;
sum += num_now;
// go to southwest
num_now += half * 2;
sum += num_now;
// go to northwest
num_now += half * 2;
sum += num_now;
// go to northeast
num_now += half * 2;
sum += num_now;
last_half = half;
}
return sum;
}
int main()
{
cout << diagsum(1001) << endl;
return 0;
}