Project Euler:Problem 28 Number spiral diagonals
2024-10-01 23:13:41
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?
找规律。
右上角(2n+1)^2
左上角(2n+1)^2-(2n+1)+1
左下角(2n+1)^2-4*n
右下角(2n+1)^2-6*n
2n+1<=1001 n<=500
#include <iostream>
#include <string>
using namespace std; int main()
{
int n = 500;
long long res = 1;
for (int i = 1; i <= 500; i++)
{ int tmp = (2 * i + 1)*(2 * i + 1);
res += tmp*4 - (2 * i + 1) + 1 - 4 * i - 6 * i;
}
cout << res << endl;
system("pause");
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
最新文章
- json数据转换失败json_last_error int(4)
- nohup输入密码后继续后台运行
- Javascript倒计时组件new TimeSpan(hours, minutes, minutes)
- Using Amazon API Gateway with microservices deployed on Amazon ECS
- 一种JavaScript 类的设计模式
- TOP/ORDER BY 顺序(转)
- 虚拟主机、VPS、云主机以及独立服务器的关系
- Umbraco(3) - CSS &; Javascript(翻译文档)
- android136 360 拖拽
- codeforces 553D . Nudist Beach 二分
- 搜狐畅游:每月给员工直系长辈2000元爱孝薪_企业新闻_265G产业频道
- 剑指offer 连续子序列和
- UVa 532 - Dungeon Master
- 【Alpha】第六次Daily Scrum Meeting
- JAVAWEB开发环境搭建,附JDK开发环境一键配置批处理bat
- fiddler 抓取 安卓模拟器 https包
- 怎样在win7 IIS中部署网站
- ecshop jquery 冲突
- 两次内存断点法寻找OEP
- Hadoop vs Spark性能对比
热门文章
- iOS_01_什么是ios
- 安装orabbix
- java韩顺平老师视频有需要可以留言
- PHP URL参数获取方式的四种例子
- u-boot分析1:Nandflash、Norflash启动
- 【例题5-1 UVA 10474 】Where is the Marble?
- php实现合并两个排序的链表(很多情况下新建数组装东西比连东西逻辑快很多)($cur=$cur->;next;的理解)
- Sphinx+MySQL5.1x+SphinxSE+mmseg中文分词
- mycat server.xml 配置文件详解
- Android 如何Android中自定义Navigationbar