《啊哈!算法》学习2

今天去拔了一颗智齿,真可怕,早上拔的,现在还疼,忧伤,,,

qq号解密问题(队列)

  • 问题描述:加密规则:奇数位的删除,偶数位的放到这列数的后面。求依次被删除的数的顺序。 初始序列:631758923
  • 思考:队列,只可以在首尾进行操作的特质。在队首进行删除操作,队尾进行插入操作,利用数组进行位移。
    选自书本原图
#include <iostream>

using namespace std;

int main()
{
    int q[101]={6,3,1,7,5,8,9,2,4};
    int head=0,tail=9;      //tail记录队尾的下一个元素

    //边删除边打印
    while(head!=tail)			//not NULL
    {
        cout<<q[head];
        head++;
        q[tail]=q[head];
        head++;
        tail++;
    }

    return 0;
}

  • 通过结构体实现队列操作:(懒得写了)
struct queue
{
    int data[100];
    int head;
    int tail;  
};

解密回文(栈)

  • 问题描述:判断是否是回文;
  • 思考:栈,进出的顺序是否一致?可以判断是否是回文。(猜想)书上是说——取中间点,先是中间之前的全部入栈,然后出栈,判断是否与后半段相同。两个差不多意思,,
#include <iostream>
#include<stack>
#include<cstdio>
using namespace std;

int main()
{
    stack<int> S;
    int a[81],b[81];
    int flag=1;         //表示是回文数

    int n ;//数有几位
    cin>>n ;

    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        S.push(a[i]);
    }

    for(int i=0;i<n;i++)
    {
        b[i]=S.top();
        S.pop();
        if(b[i]!=a[i])
            flag=0;
    }
    if(flag==0)
        cout<<"it is not a huiwenshu"<<endl;
    else
        cout<<"yes"<<endl;

    getchar();
    return 0;
}

纸牌游戏(综合运用)

  • 问题描述:AB两者已知的固定序列的数字串,AB交替出牌,前面有相同数字的可以收牌,且收回的牌按照顺序放在自己数字串的后面,问:谁先出完?
  • 思考:AB两个队列,游戏进行为一个栈,在加一个辅助栈,,,
  • 【游戏规则有两个地方不明确,牙疼,懒得想了,书上原代码如下】
#include <iostream>
#include<cstdio>

using namespace std;

struct queue
{
    int data[1000];
    int head;
    int tail;
};

struct stack
{
    int data[10];
    int top;
};

int main()
{
    struct queue q1,q2;
    struct stack s;
    int book[10];
    int i,t;
    
    //初始化队列&&栈
    q1.head=1;
    q1.tail=1;
    q2.head=1;
    q2.tail=1;
    s.top=0;
    
    //book数组用于标记桌面上有哪些数
    for(i=1;i<=9;i++)
        book[i]=0;
    
    for(i=1;i<=6;i++)
    {
        scanf("%d".&q1.data[q1.tail]);
        q1.tail++;
    }
    for(i=1;i<=6;i++)
    {
        scanf("%d".&q2.data[q1.tail]);
        q2.tail++;
    }
    
    while(q1.head<q1.tail && q2.head<q2.tail)
    {
        t=q1.data[q1.head];
        if(book[t]==0)
        {
            q1.head++;
            s.top++;
            s.data[s.top]=t;
            book[t]=1;
        }
        else
        {
            q1.head++;
            q1.data[q1.tail]=t;
            q1.tail++;
            while(s,data[s.top]!=t)
            {
                book[s.data[s.top]=0;
                q1.tail++;
                s.top--;
            }
            book[s.data[s.top]]=0;
            q1.data[q1.tail]=s.data[s.top];
            q1.tail++;
            s.top--;
        }
        if(q1.head==q1.tail)    break;
        
        t=q2.data[q2.head];
        if(book[t]==0)
        {
            q2.head++;
            s.top++;
            s.data[s.top]=t;
            book[t]=1;
            
        }
        else
        {
           //差不多的来一遍
        }
        
    }
    if(q2.head==q2.tail)
    {
        cout<<"A win"<<endl;
        cout<<"its card is:"<<endl;
        
        for(i=q2.head;i<q2.tail;i++)
            cout<<q2.data[i];
        
        if(s.top>0)
        {
            cout<<"card in des:"<<endl;
            for(i=1;i<=s.top;i++)
                cout<<s.data[i];
        }
        else
            cout<<"there is nothing in des..."<<endl;
    }
    else
    {
        //,,,差不多的来一遍
    }
    return 0;
    
}

链表方式

  • 问题描述:通过链表方式完成对数据的存储。

模拟链表(数组)

  • 问题描述:通过数组方式模拟链表。
  • 设两个数组来表示,一组存数据,另外的来表示下一个是什么(他的位置关系)
    在这里插入图片描述
    在这里插入图片描述

枚举算法

【在别想多的同时,也要适当想一下怎么优化】

  1. 奥数: 三位数相加
  2. 炸弹人
  3. 火柴棍等式
  4. 数的全排列
    【对以上几个问题作出依次描述】

猜你喜欢

转载自blog.csdn.net/qq_39789226/article/details/86618393
今日推荐