算法强训day4

今天的题很有意思,有dfs

一、斐波那契数列

链接:Fibonacci数列_牛客题霸_牛客网

#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;
	}
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
朴素贝叶斯算法原理: 朴素贝叶斯算法是一种基于贝叶斯定理和特征条件独立假设的分类算法。其基本思想是对于给定的待分类项,求解在此项出现的条件下各个类别出现的概率,哪个类别的概率最大,就认为此待分类项属于哪个类别。具体来说,朴素贝叶斯算法假设每个特征与其他特征之间相互独立,即每个特征都独立地对分类结果产生影响。在实际应用中,朴素贝叶斯算法常用于文本分类、垃圾邮件过滤等领域。 Python实现: Python中有多个库可以实现朴素贝叶斯算法,其中比较常用的是scikit-learn库。下面是一个简单的示例代码,用于展示如何使用scikit-learn库实现朴素贝叶斯算法进行文本分类: ```python from sklearn.naive_bayes import MultinomialNB from sklearn.feature_extraction.text import CountVectorizer # 构建训练数据 train_data = ['this is a good book', 'this is a bad book', 'good day', 'bad day'] train_labels = ['positive', 'negative', 'positive', 'negative'] # 构建特征提取器 vectorizer = CountVectorizer() # 将文本转换为特征向量 train_features = vectorizer.fit_transform(train_data) # 构建朴素贝叶斯分类器 clf = MultinomialNB() # 训练分类器 clf.fit(train_features, train_labels) # 构建测试数据 test_data = ['good book', 'bad book', 'happy day'] test_features = vectorizer.transform(test_data) # 进行预测 predicted_labels = clf.predict(test_features) # 输出预测结果 print(predicted_labels) # 输出:['positive' 'negative' 'positive'] ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值