程序设计实验二

//丑数
#include <iostream>
#include <string.h>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#define ll long long//是ll在前面

using namespace std;

ll a[120000]={0};//至少100000个数据

void choushu()
{
    ll l2=1,l3=1,l5=1;
    a[1]=1;
    for(int i=2;i<120000;i++)
    {
        a[i]=min(a[l2]*2,min(a[l3]*3,a[l5]*5));//注意这里是min不是max
        if(a[i]==a[l2]*2) l2++;
        if(a[i]==a[l3]*3) l3++;
        if(a[i]==a[l5]*5) l5++;
    }
}

int main()
{
    int t;
    cin>>t;
    choushu();
    while(t--)
    {
        int n;
        cin>>n;
        cout<<a[n]<<endl;
    }
    return 0;
}
//走迷宫
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>

using namespace std;

int n,m;
char map[11][11];//这里是char不是int
int dp[11][11];

int process(int x,int y,int ex,int ey)
{
    if(x==n)
    x=0;
    else if(x==-1)
    x=n-1;
    if(y==m)
    y=0;
    else if(y==-1)
    y=m-1;

    if(map[x][y]=='1')
    return 10000;

    if(x==ex&&y==ey)
    return 0;

    map[x][y]='1';

    if(dp[x][y]!=-1)
    return dp[x][y];

    dp[x][y]=min(min(process(x+1,y,ex,ey),process(x-1,y,ex,ey)),
                 min(process(x,y+1,ex,ey),process(x,y-1,ex,ey)))+1;

    return dp[x][y];
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;

        memset(dp,-1,sizeof(dp));//sizeof要加括号和dp

        for(int i=0;i<n;i++)
        {
            string s;
            cin>>s;
            strcpy(map[i],s.c_str());//少了一个括号。。。
        }
        int sx,sy,ex,ey;

        cin>>sx>>sy>>ex>>ey;

        int result=process(sx,sy,ex,ey);

        if(result!=10001)
        cout<<result<<endl;
        else
        cout<<"die"<<endl;

    }
    return 0;
}
//走迷宫2
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define ll long long
bool vis[105][105];
char map1[105][105];
int n,m;
int ans[105][105];
int xx[4]= {1,0,0,-1};
int yy[4]= {0,1,-1,0};

typedef struct
{
    int endx,endy;
} NODE;

NODE map2[105][105];

void get_map()
{
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            cin>>map1[i][j];
}

void init()
{
    for(int i=0; i<105; i++)
        for(int j=0; j<105; j++)
            vis[i][j]=0;
    for(int i=0;i<105;i++)
        for(int j=0;j<105;j++)
            ans[i][j]=3e7+10;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int step=0;
        cin>>n>>m;
        get_map();
        init();

        int t;
        cin>>t;

        for(int i=0; i<t; i++)
        {
            int x,y,x1,y1;
            cin>>x>>y>>x1>>y1;
            map2[x][y].endx=x1;
            map2[x][y].endy=y1;
            map1[x][y]='2';
        }
        int sx,sy,ex,ey;
        cin>>sx>>sy>>ex>>ey;
        queue<int>x;
        queue<int>y;
        x.push(sx);
        y.push(sy);
        ans[sx][sy]=0;
        while(!x.empty()||!y.empty())//只有有一个不为空
        {
            if(vis[x.front()][y.front()])//如果已经访问过,弹出
            {
                x.pop();
                y.pop();
                continue;
            }
            if(x.front()==ex&&y.front()==ey)//如果是终点
            {
                break;
            }
            if(map1[x.front()][y.front()]=='2')
            //如果当前格子是传送门入口,传送到出口。
            {
                vis[x.front()][y.front()]=1;
                x.push(map2[x.front()][y.front()].endx);
                y.push(map2[x.front()][y.front()].endy);
                ans[map2[x.front()][y.front()].endx][map2[x.front()][y.front()].endy]=min(ans[map2[x.front()][y.front()].endx][map2[x.front()][y.front()].endy],ans[x.front()][y.front()]+1);
            }
            else
            {
                for(int i=0; i<4; i++)
                {
                    int dx,dy;
                    dx=x.front()+xx[i];
                    dy=y.front()+yy[i];
                    if(!vis[dx][dy]&&dx>=0&&dx<n&&dy>=0&&dy<m&&map1[dx][dy]!='1')
                    //尝试四个方向的移动,如果合法且未访问,入队。
                    {
                        vis[x.front()][y.front()]=1;
                        x.push(dx);
                        y.push(dy);
                        ans[dx][dy]=min(ans[x.front()][y.front()]+1,ans[dx][dy]);
                    }
                }
            }
            x.pop();
            y.pop();

        }
        if(ans[ex][ey]==3e7+10)
            cout<<"die"<<endl;
        else
            cout<<ans[ex][ey]<<endl;
    }
}
//银行的叫号顺序
#include<iostream>
#include<string>
#include<queue>
#include<algorithm>

using namespace std;
typedef pair<int,string> PIS;
//定义一个类型别名 PIS,表示一个包含整数和字符串的 pair,用于存储事件的优先级和名称。


int main() {
	priority_queue<PIS>q;
	int n,realtime=0,time,lev,ret=99999;
	//n 表示事件数量,realtime 表示当前处理时间,time 表示事件发生时间,
	//lev 表示事件优先级,ret 用于确保相同优先级的事件按照输入顺序处理
	string name;
	cin>>n;
	while(n--)
	{
		cin>>time>>lev>>name;
		while(1)
		{
            //将time<=realtime的人全部入队
			if(time<=realtime)
			{
				q.push({lev*100000+ret,name});//体会lev设置为100000和ret为99999的妙处
				ret--;
				break;
			}
			if(!q.empty())//如果每五分钟内没有人则输出上五分钟的人
			{
				cout<<q.top().second<<endl;
				//注意top后面是括号 而不是second后面
				//second 表示这个 pair 对象的第二个元素
				q.pop();
			}
			realtime+=5;
		}
	}
	//在主循环结束后,继续从优先队列中弹出并打印剩余的事件,直到优先队列为空
	while(!q.empty())
	{
		cout<<q.top().second<<endl;
		q.pop();
	}
	return 0;
}
//银行服务
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long i64;

int main()
{
    int n,off_work;
    cin>>n>>off_work;
    priority_queue<pair<int, string> >q;//注意这里
    int who_is_priority=99999;
    int realtime=0;
    while(n--)
    {
        int time,level;
        string name;
        cin>>time>>level>>name;

        next://注意这里
        if(time<=realtime)
        {
            q.push({level*100000+who_is_priority,name});
            who_is_priority--;
            continue;//注意这里
        }
        if(!q.empty())
        {
            cout<<q.top().second<<endl;
            q.pop();
        }
        realtime+=5;
        goto next;//注意这里
    }
    while(!q.empty()&&realtime<off_work)//注意这里
    {
        cout<<q.top().second<<endl;
        q.pop();
        realtime+=5;
    }
    return 0;
}

 

//勇者斗恶龙
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int n,m;
    cin>>n>>m;
    int a[n],b[m];


    while(n!=0||m!=0)
    {
        int i=0,j=0;
        int sum=0;
        
        for(int i=0;i<n;i++)
        cin>>a[i];
        for(int i=0;i<m;i++)
        cin>>b[i];

        sort(a,a+n);
        sort(b,b+m);

        while(i<n&&j<m)
        {
            if(a[i]<=b[j])
            {
                sum+=b[j];//因为前面排序了
                i++;
                j++;
            }
            else
            j++;
        }
        
        if(j==m&&i<n)
        cout<<"Loowater is doomed!"<<endl;
        else
        cout<<sum<<endl;
        cin>>n>>m;
    }
    return 0;
}
//校赛排名
#include <iostream>
#include <algorithm>

using namespace std;

struct Student
{
    int cnt;
    long long time;//注意longlong
    char name[21];
};//注意分号

bool cmp(Student a, Student b)
{
    if(a.cnt != b.cnt)
        return a.cnt > b.cnt;
    else if(a.time != b.time)
        return a.time < b.time;
    else
        return false;//注意false
}

Student student[500000];

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i<n; i++)
        scanf("%d %d %s", &student[i].cnt, &student[i].time, student[i].name);
    stable_sort(student,student+n,cmp);//注意
    for(int i=0; i<n; i++)
        printf("%s\n", student[i].name);
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zero_019

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值