28 - number spiral diagonals

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

number_spiral_diagonals

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?

link

cevap : 669171001

Derleme ortami Visual studio community c++

// project euler problem 28
// yasin tasan --> may 2018

#include "stdafx.h"
#include "stdlib.h"
#include <iostream>

using namespace std;

#define row 1001
#define col 1001
#define limit ((col - 1) / 2) + 1 

int main()
{
    unsigned int arr_right_top[limit] = {};
    unsigned int arr_left_bot[limit] = {};
    unsigned int arr_right_bot[limit] = {};
    unsigned int arr_left_top[limit] = {};
    unsigned int sum = 0;

    for (size_t i = 1; i < limit; i++)
    {
        arr_right_top[i] = (i * 2 + 1) * (i * 2 + 1);
        cout << "arr_right_top = [" << i << "] = " << arr_right_top[i] << endl;
    }

    for (size_t i = 1; i < limit; i++)
    {
        arr_left_bot[i] = 1 + (2 * i)*(2 * i);
        cout << "arr_left_bot = [" << i << "] = " << arr_left_bot[i] << endl;
    }

    for (size_t i = 1; i < limit; i++)
    {
        arr_right_bot[i] = 1 + (i*2)*(i*2-1);
        cout << "arr_rigth_bot = [" << i << "] = " << arr_right_bot[i] << endl;
    }

    for (size_t i = 1; i < limit; i++)
    {
        arr_left_top[i] = 1 + (i*2)*(i*2+1);
        cout << "arr_left_top = [" << i << "] = " << arr_left_top[i] << endl;
    }

    for (size_t i = 1; i < limit; i++)
        sum = sum + arr_right_top[i];    

    for (size_t i = 1; i < limit; i++)
        sum = sum + arr_left_bot[i];

    for (size_t i = 1; i < limit; i++)
        sum = sum + arr_right_bot[i];

    for (size_t i = 1; i < limit; i++)
        sum = sum + arr_left_top[i];

    cout << "sum = " << sum + 1 << endl;

    system("pause");
}