0
点赞
收藏
分享

微信扫一扫

每日一练蓝桥杯C/C++B组~蛇形填数

梅梅的时光 2022-02-19 阅读 98

01、每日一练蓝桥杯C/C++B组~既约分数
02、每日一练蓝桥杯C/C++B组~十六进制转八进制
03、每日一练蓝桥杯C/C++B组~门牌制作
04、每日一练蓝桥杯C/C++B组~数列排序

每日一练蓝桥杯C/C++B组~蛇形填数

题目描述

如下图所示,小明用从 11 开始的正整数“蛇形”填充无限大的矩阵。

1  2  6  7  15 16 28 
3  5  8  14 17 27 
4  9  13 18 26
10 12 19 25
11 20 24
21 23
22

1 5 13 25 41

第一种方法:通过观察可以推到出公式。

1
1 + 1 * 4 = 5
1 + 4 + 2 * 4 = 13
1 + 4 + 8 + 3 * 4 = 25
1 + 4 + 8 + 12 + 4 * 4 =41
    
公式:(n-1^2 + n^2

第二种方法:暴力枚举,初始化n为20,就我们要求的第20行第20列,ans为1,ans = 1 + 4 + 8 + …+ (20-1) * 4 = 761

#include <iostream>

using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int n = 20, ans = 1;
	for(int i = 0; i < n; i++){
		ans += i * 4;
	}
	cout << ans << endl;
	return 0;
}

举报

相关推荐

0 条评论