首页 > 其他分享 >复试机试学习

复试机试学习

时间:2023-06-04 09:02:05浏览次数:37  
标签:pr 学习 head struct int 复试 flag SHE 机试

想要见识一下,如何将数据结构中学习到的数据结构应用到实际中,转化为编程

  • 猫狗收容所
    image
  1. 两个队列分别收容猫和狗,不仅收容他们的信息还要增加进入次序的信息
  2. 此时队列的元素已经不是简单的整型了
#include<queue>
#include<cstdio>
using namespace std;
struct Animal {
    int num;
    int seq;
};

int main()
{
    queue<Animal> catQueue;
    queue<Animal> dogQueue;
    int seq=0;
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int method,para;
        scanf("%d%d",&method,&para);
        if(method==1){
            //非收养
            if(para>0){
                //来的是狗
                Animal dog;
                dog.num=para;
                dog.seq=seq;
                ++seq;
                dogQueue.push(dog);
            }else{
                //来的是猫
                Animal cat;
                cat.num=para;
                cat.seq=seq;
                ++seq;
                catQueue.push(cat);
            }
        }else{
            //收养,得考虑队列空不空
            if(para==0){
                //采用第一种收养方式
                if(catQueue.empty()&&dogQueue.empty()){
                    //猫狗队列都是空
                    continue;
                }else if(catQueue.empty() || !dogQueue.empty() && dogQueue.front().seq<catQueue.front().seq){
                    //狗队列非空,收养狗
                    printf("%d ",dogQueue.front().num);
                    dogQueue.pop();
                }else{
                    //猫队列非空
                    printf("%d ",catQueue.front().num);
                    catQueue.pop();
                }
            }else if(para==1){
                //只选择狗
                if(dogQueue.empty()){
                    continue;
                }else{
                    printf("%d ",dogQueue.front().num);
                    dogQueue.pop();
                }
            }else{
                //只选择猫
                if(catQueue.empty()){
                    continue;
                }else{
                    printf("%d ",catQueue.front().num);
                    catQueue.pop();
                }
            }

        }

    }
    printf("\n");
    return 0;
}

牛逼!贪吃蛇小游戏

/*贪吃蛇*/
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#define HENG 80
#define SHU 25  //HENG是宽度,SHU是高度
#define TIME 400 //初始速度

void Gotoxy(int x, int y);
void Food(struct SHE *head);
int Move(struct SHE *head);
int Panduan(struct SHE *head, int x, int y);
struct SHE *Link(struct SHE *head);
void FreeShe(struct SHE *head);
void Gameover(void);
void Huitu(struct SHE *head);


int Fx, Fy;  //食物的位置
int fen;   //得分
int fang; //方向参数
int Tx, Ty;

//构造蛇身
struct SHE
{
    int x;
    int y;
    struct SHE *next;
};

int main(void)
{
    struct SHE *head = NULL;
    int temp;
    int times = TIME;
    char m;

    do
    {
        fen = 0;
        fang = 2;
        temp = fen;
        times = TIME;
        head = NULL;

        head = (struct SHE *)malloc(sizeof(struct SHE));
        head -> x = 40;
        head -> y = 13;
        head -> next = NULL;

        Food(head);

        while (1)
        {
            Huitu(head);
            Sleep(times);
            if (0 == Move(head))
            {
                FreeShe(head);
                Gameover();
                break;
            }
            if (temp < fen)
            {
                Food(head);
                temp = fen;
                if (times > 50)
                {
                    times -=10;
                }
                else if (times > 20)
                {
                    times -=1;
                }
                else
                    ;
            }
        }
        do
        {
            Gotoxy(19,21);
            printf("是否继续?Y/N:");
            scanf(" %c", &m);
            system("cls");
        } while (m != 'Y' && m != 'y' && m != 'n' && m != 'N');
    } while (m == 'Y' || m == 'y');

    return 0;
}

//绘图
void Huitu(struct SHE *head)
{
    int flag = 1;

    while (head != NULL)
    {
        Gotoxy(head -> x, head -> y);
        if (flag == 1)
        {
            printf("□");
        }
        else if (flag == 2)
        {
            printf("■");
        }
        else
            ;
        Gotoxy(Tx, Ty);
        printf(" ");
        flag++;
        head = head -> next;
    }
}

//用于光标的移动
void Gotoxy(int x, int y)
{ 
  COORD coord; 
  coord.X = x; 
  coord.Y = y; 
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

//食物位置构造
void Food(struct SHE *head)
{
    int flag = 0;
    struct SHE *pr = head;

    do
    {
        srand(GetTickCount()); //基于毫秒级的随机数产生办法,需头文件winbase.h, windows中设计程序可以用windows.h
        Fx = (rand()%40) * 2;
        Fy = (rand()%12) * 2;
        flag = 0;
        pr = head;
        while (pr != NULL) //防止食物位置与蛇形重合
        {
            if (pr -> x == Fx && pr -> y == Fy)
            {
                flag = 1;
                break;
            }
            pr = pr -> next;
        }
    } while (flag == 1);
    Gotoxy(Fx, Fy);
    printf("■");
}

//用于蛇的移动
int Move(struct SHE *head)
{
    char op;
    int tox, toy, flag, temp;

    if (kbhit())  //kbhit函数用于检查当前是否有键盘输入,若有则返回一个非0值,否则返回0,需头文件conio.h
    {
        op = getch();
        switch (op)
        {
            case 'w':
                if (fang != 3)
                fang = 1;
                break;
            case 'd':
                if (fang != 4)
                fang = 2;
                break;
            case 's':
                if (fang != 1)
                fang = 3;
                break;
            case 'a':
                if (fang != 2)
                fang = 4;
                break;
            default:
                break;
        }
    }

    switch (fang)
    {
        case 1:
            toy = (head -> y) - 1;
            tox = (head -> x);
            break;
        case 2:
            tox = (head -> x) + 2;
            toy = (head -> y);
            break;
        case 3:
            toy = (head -> y) + 1;
            tox = (head -> x);
            break;
        case 4:
            tox = (head -> x) - 2;
            toy = (head -> y);
            break;
        default:
            break;
    }
    flag = Panduan(head, tox, toy);
    if (flag == 0)
    {
        return 0; //game over
    }
    else if (flag == 1)
    {
        head = Link(head);
        fen++;
    }
    else
    {
        ;
    }
    while (head != NULL)
    {
        temp = head -> x;
        head -> x = tox;
        tox = temp;

        temp = head -> y;
        head -> y = toy;
        toy = temp;

        head = head -> next;
    }
    return 1;
}

//用于食物与失败条件的判断
int Panduan(struct SHE *head, int x, int y)
{

    int flag = 1;

    if (x < 0 || x > HENG - 1 || y < 0 || y > SHU - 1)
    {
        return 0;  //撞墙
    }
    while (head != NULL)
    {
        if (head -> x == x && head -> y == y && flag != 1)
        {
            return 0; //咬到自己
        }
        flag++;
        if (head -> next == NULL)
        {
            Tx = head -> x;
            Ty = head -> y;
        }
        head = head -> next;
    }
    if (x == Fx && y == Fy)
    {
        return 1;//有食物
    }
    return 2;//无异常
}

//蛇身增长
struct SHE *Link(struct SHE *head)
{
    struct SHE *p, *pr;

    pr = head;
    p = (struct SHE *)malloc(sizeof(struct SHE));
    if (p == NULL)
    {
        printf("发生错误!!!");
        exit(0);
    }
    if (head == NULL)
    {
        head = p;
    }
    else
    {
        while (pr -> next != NULL)
        {
            pr = pr -> next;
        }
        pr -> next = p;
    }
    p -> x = Tx;
    p -> y = Ty;
    p -> next = NULL;
    return head;
}

//释放内存
void FreeShe(struct SHE *head)
{
    struct SHE *pr = NULL;

    while (head != NULL)
    {
        pr = head;
        head = head -> next;
        free(pr);
    }
}

//游戏失败画面,如果窗口大小不是80*25导致画面不协调可舍去
void Gameover(void)
{
    int i,j;

    int a[SHU][HENG]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,1,1,1,0,0,1,1,1,1,1,0,0,0,0,1,1,1,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,0,1,1,0,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,1,1,1,1,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,1,1,1,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

    Gotoxy(0, 0);
    for (i = 0; i < SHU; i++)
    {
        for (j = 0; j< HENG; j++)
        {
            if (a[i][j] == 0)
            {
                printf(" ");
            }
            else
            {
                printf("0");
            }
        }
    }

    Gotoxy(19, 20);
    printf("游戏最终得分是 %d 分!!!\n", fen);
}

标签:pr,学习,head,struct,int,复试,flag,SHE,机试
From: https://www.cnblogs.com/barry-first/p/17454164.html

相关文章

  • 跟着思兼学习Klipper(24): 红外遥控3D打印机
    原创文章,转载引用请务必注明链接,水平有限,如有疏漏,欢迎指正交流。文章如有更新请访问DFRobot社区或者cnblogs博客园。书接上回,这次补完红外遥控的部分。6、番外:板载USER_LED的使用上面3.1pwr_status.sh说到启动后会控制板载用户可控LED间隔0.5s闪烁。但是在BTT......
  • 【ROS学习】初学ROS的小项目
    ROS学习:turtle控制turtle旋转首先查看turtlesim里面的节点,使用rqt_graph查看节点流图/**@Description:这是一个让turtle旋转的rospublisher*@Version:*@Autor:copy*@Date:2022-03-1217:28:55*@LastEditors:copy*@LastEditTime:2022-03-1217:42:00......
  • 【ROS学习】配置rotors-simulator
    本文按照https://github.com/ethz-asl/rotors_simulator一步一步搭建并且踩坑记录。雷第一个坑:尽量不要在虚拟机上跑,因为你在catkinmake的时候就会卡住。在原文中也提到了,第一次加载gazebo的时候会出现超时error,不要慌,关闭当前gazebo窗口再次输入用法的1命令。好像用不......
  • 「学习笔记」概率与期望
    样本点与样本空间\(A=\left\{1,2,3\right\}\)\(1,2,3\)为样本点,\(A\)为样本空间。\[A=\left\lbrace1,2,3\right\rbrace\\B=\left\lbrace2,3,4\right\rbrace\\A\capB=\left\lbrace2,3\right\rbrace=A\cdotB\\A......
  • 05web安全学习---PHP正则表达式
    一、初识SQL注入<metacharset='GBK'/><form><center> 用户登录<br/>账号:<inputtype='text'name='uname'/><br/>密码:<inputtype='password'name='pwd'/><br/>&......
  • 强化学习:连续控制问题中Actor-Critic算法的linear baseline
    最近在看连续控制问题,看到了一个Actor-Critic算法中手动扩展features和设置linearbaseline的方法,这些方法源自论文:《BenchmarkingDeepReinforcementLearningforContinuousControl》。  对于低维的features我们可以手动扩展:  代码实现:returntorch.cat([observations,ob......
  • 强化学习基础篇[3]:DQN、Actor-Critic详细讲解
    强化学习基础篇[3]:DQN、Actor-Critic详细讲解1.DQN详解1.1DQN网络概述及其创新点在之前的内容中,我们讲解了Q-learning和Sarsa算法。在这两个算法中,需要用一个Q表格来记录不同状态动作对应的价值,即一个大小为$[状态个数,动作个数]$的二维数组。在一些简单的强化学习环境中,比如......
  • 【QoS预测】用于冷启动QoS预测的基于图对比学习的双子图网络
    论文题目:ZhuJ,LiB,WangJ,etal.BGCL:Bi-subgraphnetworkbasedongraphcontrastivelearningforcold-startQoSprediction[J].Knowledge-BasedSystems,2023,263:110296.问题:通过利用用户和服务之间的历史交互记录,协同过滤(Collaborativefiltering)成为了一种......
  • 基于深度学习的图像分类:使用卷积神经网络实现猫狗分类器
    摘要:深度学习在计算机视觉领域中具有广泛的应用。本文将介绍如何使用卷积神经网络(CNN)实现一个猫狗分类器。我们将使用Python和TensorFlow框架搭建一个简单的卷积神经网络模型,并利用猫狗图像数据集进行训练和测试。通过本文,读者将了解到深度学习在图像分类任务中的基本原理和实践应......
  • 用chartGPT学习一门语言--python(提问示范)
    1. 2. 3. 4 5. 6. ......