题解分享
题解分享简介
蛇形填数(结果填空) - 题解
填空题:手搓蓝桥杯省一
填空题么,写个程序是不是小题大做了,我们可以先按照这个写几个出来
【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】
我们可以发现一个规律,针对坐标为(i,i)的数字即(1,1),(2,2)……这些数字的增加方式很有意思,我们拆分出来即:1,5,13,25,41……,每次增加的都是4的倍数,第一次增加是4的1倍……,所以我们只需要手算一下变化20次的(20,20)即可知道答案,用时2min,(这不比编写一个程序简单?)
查看全文
0
0
9
3
蛇形填数(结果填空) - 题解
模拟
```
#include <cstdio>
//int main(){
// printf("%d\n",20*20+19*19);
// return 0;
//}
int main(){
int sum =1;
for(int i=1;i<=20;i++){
sum+=(i-1)*4;
}
printf("%d\n",sum);
return 0;
}
```
本蒟看到题目,手动模拟,找规律
发现第n行第n列的数值为n^2+(n-1)^2,就得到正确答案,至于为什么,还没想到,请大佬赐教👀️
然后又发现第n行第n列的数值呈等差数列,结果答案也是对的
查看全文
0
0
5
1
蛇形填数(结果填空) - 题解
笨方法,模拟坐标的移动
```
#include <bits/stdc++.h>
using namespace std;
int posx = 1,posy = 2;
int a[300][300];
int main(){
a[1][1] = 1;
a[posx][posy] = 2;
int flag = 0;
for (int i = 2; i < 50;++i) {
if (flag == 0) {
for (int j = 1; j < i; ++j) {
posx = posx + 1;
posy = posy - 1;
a[posx][posy] = a[posx - 1][posy + 1] + 1;
}
flag = 1;
posx += 1;
a[posx][posy] = a[posx - 1][posy] + 1;
continue;
}
if (flag == 1){
for (int j = 1; j < i; ++j) {
posx = posx - 1;
posy = posy + 1;
a[posx][posy] = a[posx + 1][posy - 1] + 1;
}
flag = 0;
posy += 1;
a[posx][posy] = a[posx][posy - 1] + 1;
continue;
}
}
cout << a[20][20];
return 0;
}
```
查看全文
0
0
3
1
蛇形填数(结果填空) - 题解
```
//
// Created by 30945 on 2024/4/12.
//非常感谢up提供的教程,之前学了半天没明白这个题,看了up的解法一个小时就彻底拿下这类题了
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int m = 3, n = 3, num = 0;
int cnt = m + n - 1;//确定是哪一斜排
for (int i = 1; i < cnt; i++) {//把前i-1排有多少个数记录下来
num += i;
}
//确定那一排是从上往斜下还是从下往斜上填
if (cnt % 2 == 0) {
//偶数是从上往斜下填
int row = 1;
while (row <= m) {
num++;
row++;
}
} else {
int row = cnt;
while (row >= m) {
num++;
row--;
}
}
cout << num << endl;
return 0;
}
```
查看全文
0
0
2
0
蛇形填数(结果填空) - 题解
模拟蛇形前进
```
#include <cstdio>
#include <cmath>
#include<iostream>
using namespace std;
int a[200][200];
int main(){
int x=1,y=1;
int count=0;
for(int i=1;i<200;i++){ //上边界和左边界为0,其他均为1
for(int j=1;j<200;j++){
a[i][j]=1;
}
}
int pre1=1,pre2=-1;
int n=1;
while(1){ //x,y是坐标,蛇形前进
if(x==20&&y==20){
cout<<n;
break;
}
if(a[x][y-1]==0){ //碰到 上边界
y++;
pre1=-pre1;
pre2=-pre2;
n+=2;
}
else if (a[x-1][y]==0){ //碰到左边界
x++;
pre1=-pre1;
pre2=-pre2;
n+=2;
}
else{
x+=pre1;y+=pre2;
n++;
}
}
}
```
查看全文
0
0
1
1
蛇形填数(结果填空) - 题解
观察中间数字 ,1 5 13 25 ....不难发现每两个数字的差组成的数列 4 8 12 ...为一个等差数列。基于这点可用很快求出答案
```
import java.util.Arrays;
public class Main{
public static void main(String[] args) {
int sum = 1;
int x = 4;
int[] ans = new int[22];
ans[1] = 1;
for (int i = 1; i < 21; i++) {
sum = sum + x;
x = x + 4;
ans[i+1] = sum;
}
System.out.println(Arrays.toString(ans));
System.out.println(ans[20]);
}
}
```
即可得出答案为761
查看全文
0
0
1
1
蛇形填数(结果填空) - 题解
可以判断0,2,4和1,3,5偶数和奇数的位置从而快速看出偶数时x+1,而奇数时y+1,这样只要找出规律就很快能写出来了
include
using namespace std;
int main()
{ int n=0;
int arr[105][105]={0};
scanf("%d",&n);//输入数组的大小,arr[n][n]
```
//填充数组
int num=1;
for(int i=1;i<n;i++)
{
if(i%2==0)//i为偶数时从左下向右上填充
{
int x=i;
int y=0;
while(x+1)//每次循环填充的数字为i+1既是x+1
{
arr[x][y]=num;
x--;
y++;
}
}
else//i为奇数时,数字从右上到坐下填充
{
int x=0;
int y=i;
while(y+1)//每次循环填充的数字为i+1既是y+1
{
arr[x][y]=num;
x++;
y--;
}
}
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
printf("%-3d",arr[i][j]);
}
printf("\n");
}
return 0;
```
}
查看全文
0
0
1
1
蛇形填数(结果填空) - 题解
include
using namespace std;
int main() {
int arr[100][100];
int x = 0, y = 0;
int dir = 0;
/dir == 1 右上方向
dir == -1 左下方向
dir == 0 无方向/
for (int i = 1; i < 10000; i++) {
if (x == 100 || y == 100)break;
arr[x][y] = i;
cout << x << "," << y << " " << "dir:" << dir <<"value:" <<i<< endl;
if (dir == -1) {
if (x == 0) {
++y;
dir = 1;
}
else {
--x;
++y;
}
}
else if (dir == 1) {
if (y == 0) {
++x;
dir = -1;
}
else {
++x;
--y;
}
}
else {
++x;
dir = -1;
}
}
cout << arr[19][19];
return 0;
```
}
查看全文
0
0
0
1
蛇形填数(结果填空) - 题解
include
using namespace std;
int a[300][300];
int main() {
int total=1,pox=0,poy=0,cnt=1;
a[0][0]=1;
for(int i=1;i<=50;i++){
if(i%2==1){
while(cnt<i){
a[--pox][++poy]=++total;
cnt++;
}
a[pox][++poy]=++total;
}else{
while(cnt<i){
a[++pox][--poy]=++total;
cnt++;
}
a[++pox][poy]=++total;
}
cnt=1;
}
cout<<a[19][19]<<" ";
}
仿照蛇形填数2的讲义,使用pos指针。两个while即为方向
查看全文
0
0
0
1



