今天的题很有意思,有dfs
一、斐波那契数列
#include<iostream>
long long a[100000000] = { 0 };
int main()
{
long long n = 0;
std::cin >> n;
a[0] = 1;
a[1] = 1;
int i = 2;
while (a[i-1] < n)
{
a[i] = a[i - 1] + a[i - 2];
i++;
}
long long b = a[i - 1];
long long c = a[i - 2];
long long count = b - n > n - c ? n - c : b - n;
std::cout << count;
}
二、单词搜索
链接:https://www.nowcoder.com/share/jump/1122916481713496084189
这是一道典型的dfs,博主还没学过,算法比较初级,调试了好多遍╮(╯﹏╰)╭
#include<stdio.h>
#include<malloc.h>
#include<stdbool.h>
#include<time.h>
typedef struct StackNode
{
int x;
int y;
int di;
int change;
struct StackNode* next;
}StackNode;
typedef struct Stack
{
StackNode* head;
StackNode* rear;
int size;
}Stack;
void push(Stack* s, int x1, int y1, int dir)
{
StackNode* n = (StackNode*)malloc(sizeof(StackNode));
n->di = dir;
n->next = NULL;
n->x = x1;
n->y = y1;
n->change = 0;
if (s->head == NULL)
{
s->head = n;
}
else
{
s->rear->next = n;
}
s->rear = n;
(s->size)++;
}
void pop(Stack* s)
{
StackNode* p = s->head;
if (s->size)
{
if (s->head == s->rear)
{
free(s->head);
s->head = NULL;
s->rear = NULL;
(s->size)--;
}
else
{
while (p->next != s->rear)
{
p = p->next;
}
free(s->rear);
s->rear = p;
p->next = NULL;
(s->size)--;
}
}
}
int is_fun(int curx, int cury, Stack* s)
{
StackNode* phead = s->head;
while (phead )
{
if (curx == phead->x && cury == phead->y)
return 0;
phead = phead->next;
}
return 1;
}
bool exist(char** board, int boardLen, char* word) {
Stack s;
s.head = NULL;
s.rear = NULL;
s.size = 0;
int curx = 0;
int cury = 0;
int i = 0;
int j = 0;
int dirt = 1;
int key = 0;
while (i<boardLen)
{
key = 0;
if (board[i][j] == 0)
{
i ++;
j=0;
}
if (i == boardLen)
return false;
if(board[i][j]==word[0])
{
dirt = 1;
curx = i;
cury = j;
push(&s, curx, cury, dirt);
key++;
while (s.size)
{
if (dirt == 1)
{
cury++;
}
else if (dirt == 2)
{
curx++;
}
else if (dirt == 3)
{
cury--;
}
else if(dirt == 4)
{
curx--;
}
while (curx>=0&&cury>=0&&curx<boardLen&&board[curx][cury]!=0&&word[key] && board[curx][cury] == word[key]&&is_fun(curx,cury,&s))
{
push(&s, curx, cury, dirt);
if (dirt == 1)
{
cury++;
}
else if (dirt == 2)
{
curx++;
}
else if (dirt == 3)
{
cury--;
}
else if(dirt==4)
{
curx--;
}
key++;
}
if (word[key] == 0)
return true;
while (s.size>0 &&(s.rear->di == 5||s.rear->change>=4) )
{
pop(&s);
key--;
}
if (s.size == 0)
break;
curx = s.rear->x;
cury = s.rear->y;
dirt = s.rear->di;
if (dirt < 5)
{
if (dirt!=1&&curx >= 0 && cury + 1 >= 0 && curx < boardLen && board[curx][cury + 1] == word[key]&&is_fun(curx,cury+1,&s))
dirt = 1;
else if (dirt != 3&&curx >= 0 && cury - 1 >= 0 && curx < boardLen && board[curx][cury - 1] == word[key]&&is_fun(curx, cury-1, &s))
dirt = 3;
else if (dirt != 2 && curx + 1 >= 0 && cury >= 0 && curx + 1 < boardLen && board[curx + 1][cury] == word[key]&&is_fun(curx+1, cury, &s))
dirt = 2;
else if (dirt != 4 && curx - 1 >= 0 && cury >= 0 && curx < boardLen && board[curx - 1][cury] == word[key] && is_fun(curx-1, cury, &s))
dirt = 4;
else
dirt = 5;
s.rear->di = dirt;
(s.rear->change)++;
}
}
}
j++;
}
return false;
}
三、杨辉三角
格式输出我用的是C的语法
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
long long pre[10000000] = { 0 };
long long cur[10000000] = { 0 };
int main()
{
int n;
cin >> n;
int i = 0;
pre[i] = 1;
printf("%5lld\n", pre[i]);
for (i; i < n - 1; i++)
{
int x = i;
i = 0;
int count = 2;
while (pre[i] != 0)
{
if (i == 0)
{
cur[i] = 1;
}
else
{
cur[i] = pre[i] + pre[i - 1];
count++;
}
printf("%5lld", cur[i]);
i++;
}
cur[i] = 1;
printf("%5lld", cur[i]);
int k = 0;
int j = 0;
for (j; j < count; j++)
{
pre[j] = cur[j];
}
cout << endl;
i = x;
}
}