Project Euler 28 Number spiral diagonals


题意:给出一个 1001 × 1001 的矩阵,寻找每一圈四个顶点,并求出所有顶点的和

思路:只需要找到右上顶点数字的规律,然后每一圈四个顶点构成了一个等差数列,求个和即可


/*************************************************************************
    > File Name: euler028.cpp
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年05月23日 星期二 16时50分16秒
 ************************************************************************/

#include<bits/stdc++.h>
using namespace std;

#define ll long long

int main(){
	ll sum = 1;
	int n = ( 1001 + 1 ) / 2;
	for(int i = 2 ; i <= n ; i++){
		sum += (ll)4*(2*i-1)*(2*i-1) - 12*(i-1);
	}
	printf("sum = %lld\n",sum);
	return 0;
}
posted @ 2017-06-23 13:19  ojnQ  阅读(159)  评论(0编辑  收藏  举报