第 11 章 模拟

本文集展示了多个C++编程实例,包括算法实现、数据结构操作、输入输出处理等,旨在帮助读者提升C++编程技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A1008


#include<iostream>
using namespace std;

const int maxn=111;

int a[maxn],n;

int main()
{
    cin >> n;
    int next,qian=0;
    int time=0;
    for(int i=0; i<n; i++)
    {
        cin >> next;
        if(next>qian)
        {
            time+=(next-qian)*6;
        }
        else
        {
            time+=(qian-next)*4;
        }
        qian=next;
    }
    time+=n*5;
    cout << time;
    return 0;
}

A1011


#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    double res=1;
    for(int i=0; i<3; i++)
    {
        double w,t,l;
        cin >> w >> t >> l;
        double x=max(w,max(t,l));
        if(x==w)
            cout << "W ";
        else if(x==t)
            cout << "T ";
        else
            cout << "L ";
        res*=x;
    }
    printf("%.2lf",(res*0.65-1)*2);
    return 0;
}

A1014


#include<iostream>
#include<queue>
#include<unordered_map>
using namespace std;

//用队列 数组 去 模拟 每个 窗口

const int maxn=22;

queue<int> q[maxn];

int sum[maxn];     //存放 每个 窗口 当前 最晚 结束的 时间

int n,m,k,Q;

/*
    
*/

int main()
{
    cin >> n >> m >> k >> Q;
    unordered_map<int,int>hash;
    for(int i=1; i<=k; i++)
    {
        int s;          //每个人办理 业务 的时间
        cin >> s;

        //首先 选择 一个 合适窗口,分成 两种 情况:1.人数 在 没满 n*m的情况下,寻找 当前 人数 最少的 窗口,2.否则,寻找当前 最早结束 的窗口
         int t=0;     //表示 合适 窗口
             for(int j=0; j<n; j++)
             {
                 if(i<=n*m)       //在 窗口 没满 之前
                 {
                     if(q[j].size()<q[t].size())
                        t=j;
                 }
                else               
                    if(q[j].front()<q[t].front())
                        t=j;
             }

        sum[t]+=s;        //该窗口下的 所有人 结束的 时间
        if(i>n*m)
            q[t].pop();
        q[t].push(sum[t]);

        if(sum[t]-s<540)          //如果 一个人的结束 时间 减去 开始 时间 超过 540,表示 在 17.00之后了 ,就不要了
            hash[i]=sum[t];
    }

    int  temp;
    while(Q--)
    {
        cin >> temp;
        if(hash.count(temp))     //对于Q个 查询,首先 要在 哈希表中 能找到
        {
            printf("%02d:%02d\n",hash[temp]/60+8,hash[temp]%60);
        }
        else
            cout << "Sorry" << endl;
    }
    return 0;
}


A1031

在这里插入图片描述



#include<iostream>
using namespace std;

string s;

int n1,n2,n3;



int main()
{
    cin >> s;
    n1=(s.size()+2)/3;
    n3=n1;
    n2=s.size()-n1*2;

    int k=0;

    char a[s.size()][s.size()];

    for(int i=0; i<s.size(); i++)
        for(int j=0; j<s.size(); j++)
            a[i][j]=' ';


    for(int i=0; i<n1; i++)
    {
        a[i][0]=s[k++];
    }


    for(int i=1; i<=n2; i++)
    {
         a[n1-1][i]=s[k++];
    }


    for(int i=n1-1; i>=0; i--)
    {
        a[i][n2+1]=s[k++];
    }


    for(int i=0; i<n1; i++)
    {
        for(int j=0; j<=n2+1; j++)
        {
            if(a[i][j]!=' ')
                cout << a[i][j];
            else if(a[i][j]==' ')
                cout << ' ';
        }
        cout << endl;
    }

}

A1041



#include<iostream>
using namespace std;

const int maxn=100010;

int flag[maxn];
int a[maxn];

int n;

int main()
{
    cin >> n;
    for(int i=0; i<n; i++)
    {
        cin >> a[i];
        flag[a[i]]++;
    }

    for(int i=0; i<n; i++)
    {
        if(flag[a[i]]==1)
        {
            cout << a[i];
            return 0;
        }
    }

    cout << "None";
    return 0;
}

A1042

#include <iostream>
#include <cstring>

/*
    注意:1.题中的例子 就 告诉 你 洗牌 的 方式---将其中 一个 值 放到 指定 的 位置,但 题中 具体 不是 这样

*/

using namespace std;

const int N = 60;

int k;
int q[N], p[N], w[N];

void print(int x)
{
    if (x <= 13) cout << 'S' << x;
    else if (x <= 26) cout << 'H' << x - 13;
    else if (x <= 39) cout << 'C' << x - 26;
    else if (x <= 52) cout << 'D' << x - 39;
    else cout << 'J' << x - 52;
}

int main()
{
    cin >> k;
    for (int i = 1; i <= 54; i ++ ) cin >> q[i];        //数组 q 存放 那个 排列 顺序
    for (int i = 1; i <= 54; i ++ ) p[i] = i;

    while (k -- )
    {
        memcpy(w, p, sizeof w);        //将 p 中  的值 复制 w个字节的字符 到 w 中    注意 memspy 要加 cstring
        for (int i = 1; i <= 54; i ++ ) p[q[i]] = w[i];
    }

    for (int i = 1; i <= 54; i ++ )
    {
        print(p[i]);
        if (i != 54) cout << ' ';
    }

    return 0;
}

A1047


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn=40010;

vector<string> lesson[maxn];

int n,k;

int main()
{
    scanf("%d%d", &n, &k);

    char str[5];
    while (n -- )
    {
        int cnt;
        scanf("%s%d", str, &cnt);
        while (cnt -- )
        {
            int l;
            scanf("%d", &l);
            lesson[l].push_back(str);
        }
    }

    for(int T=1; T<=k; T++)
    {
        cout << T << " " << lesson[T].size() << endl;
        sort(lesson[T].begin(),lesson[T].end());
        //玄机 就在这里 对于 vector 数组 用 快捷的方式 去写,并且 使用printf
        for(auto id:lesson[T])
            printf("%s\n",id.c_str());
    }
    return 0;
}

A1054

#include<iostream>
#include<unordered_map>
using namespace std;

int n,m;

int main()
{
    cin >> m >> n;
    unordered_map<int,int> mp;
    for(int i=0; i<m*n; i++)
    {
        int x;
        cin >> x;   //输入一个x值,边输入边统计
        if(++mp[x]>n*m/2)          //这个 ++ 放到 前面 和 放到 后面,区别 就 在于 前者 是 加完 以后 在比较,后者是 比较完 再 加,以本题 为 例
        {
            cout << x;
            break;
        }
    }
    return 0;

A1062



#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=100010;

struct node
{
    int id,v,t,level;

    int total() const
    {
        return v+t;
    }
 //bool operator < (const Person &t) 表示 有 两个 结构 一个是 本身 一个 是t代表的结构 后面const表示 使用的 求和函数
//使用 函数 重载 比 cmp 不会 出现段错误
    bool operator < (const node &t) const
    {
        if(level != t.level)
            return level<t.level;
        if(total() != t.total())
            return total()>t.total();
        if(v!=t.v)
            return v>t.v;
        if(id!=t.id)
            return id<t.id;
    }

}Node[maxn];

int n,l,h;

int main()
{
    cin >> n >> l >> h;
    int idd,temp1,temp2;
    int k=0;
    while(n--)
    {
        cin >> idd >> temp1 >> temp2;
        if(temp1<l || temp2<l)
            continue;

        int level;
        if(temp1>=h && temp2>=h)
            level=1;
        else if(temp1>=h && temp2<h)
            level=2;
        else if(temp1<h && temp2<h && temp1>=temp2)
            level=3;
        else
            level=4;

        Node[k++]={idd,temp1,temp2,level};
    }

    sort(Node,Node+k);
    cout << k << endl;
    for(int i=0; i<k; i++)
        printf("%08d %d %d\n",Node[i].id,Node[i].v,Node[i].t);
    return 0;
}

A1065



#include<iostream>
using namespace std;

typedef long long LL;        //注意 long long类型的 范围是 long的取值范围为(-9223372036854774808~9223372036854774807),占用8个字节(-2的63次方到2的63次方-1)。

//所以 a,b,c必须 设置 为 long long类型

bool check(LL a,LL b,LL c)
{
    LL d=a+b;
    if(a>=0 && b>=0 && d<0)       //如果 a,b是非负,他俩之和 大的没边 就是 越界,则和 一定是 负数,也就是 他俩之和 一定大于 第3个数
        return true;
    if(a<0 && b<0 && d>=0)      //如果a,b是 负数,他俩之和 小的 没边,则他俩 之和 是一定是 非负数,也就是 一定不大于 第3个值
        return false;
    //以上 情况,需要特殊 判断
    return a+b>c;   //对于 这个 如果 a+b>c则 返回true 否则 返回 false
}

int main()
{
    int n;
    cin >> n;
    LL a,b,c,d;

    for(int T=1; T<=n; T++)
    {
        scanf("%lld%lld%lld",&a,&b,&c);        //注意:对于long long 类型 是 %lld
        if(check(a,b,c))
            printf("Case #%d: true\n",T);
        else
            printf("Case #%d: false\n",T);
    }
}

A1069

#include<iostream>
#include<algorithm>
using namespace std;
/*
    解题思路:

*/
int main()
{
    string s;
    cin >> s;
    s.insert(0,4-s.length(),'0');//如果字符串不足4位则左边补0  s.insert(p,n,t); ———— n个值为t元素,返回新添加的第一个
    while(1)
    {
        sort(s.begin(),s.end());
        string smin=s;
        int minn=stoi(smin);     //字符串排列成最小的值
        reverse(s.begin(),s.end());
        string smax=s;
        int maxn=stoi(smax);       //字符串排列成最大值
        int n= maxn-minn;
        printf("%04d - %04d = %04d\n",maxn,minn,n);
        if(n==0 || n==6174)
            break;
        s=to_string(n);
        s.insert(0,4-s.length(),'0');
    }
    return 0;
}

A1080


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;

const int maxn=40010;

int n,m,k;

int cnt[maxn];      //表示 每个 学校 打算 录取 多少人

vector<int> uty[maxn];      //表示 每个 实际 录取的 人,最后 也是 根据 这个 东西进行 输出

int wish[maxn];       //表示 每个 人 想选择  那个学校

/*
        题中的要求是:对一批学生 按照 其成绩 进行排名,然后 根据 每个人的志愿 来选择 是否 录取,对于 统一 排名的 人,如果 这些人

        报名 同样 的学校 即使 人数 已经 满了,但是 也要 都 录取
*/

struct node
{
    int ge,gi,id;
    int wish[5];

    int total() const
    {
        return ge+gi;
    }

    bool operator <(node &t) const      //重载 函数 按照 总分的 降序 和  ge 成绩 的 降序排列
    {
        if(total()!=t.total())       //总分不同,按照 总分的 降序 排列
        {
            return total()>t.total();
        }
        return ge>t.ge;
    }

    bool operator == (node &t) const
    {
        return ge==t.ge && gi==t.gi;
    }

}Node[maxn];

int main()
{
    cin >> n >> m >> k;

    for(int i=0; i<m; i++)
        cin >> cnt[i];

    for(int i=0; i<n; i++)
    {
        scanf("%d %d",&Node[i].ge,&Node[i].gi);   //输入 每个 人 的 ge成绩 和 gi成绩
        Node[i].id=i;
        for(int j=0; j<k; j++)
        {
            cin >> Node[i].wish[j];     //输入 每个人的 志愿
        }
    }

    sort(Node,Node+n);    //排序


    memset(wish,-1,sizeof wish);    //wish表示 每个人 能 选择的 学校
    for(int i=0; i<n; )
    {
        int j=i+1;          //先去 寻找 同一个 排名的 人

        while(j<n && Node[i]==Node[j])    //从i~j-1 这个 区间 内的 人的 成绩 都是 相同
                j++;

        //接下来 进行的 操作是 遍历 同一个 排名中,每个人的 志愿
        for(int t=i; t<j; t++)
            for(int u=0; u<k; u++)
            { 
                if(cnt[Node[t].wish[u]]>uty[Node[t].wish[u]].size())     //如果 该人 选择的 学校的 目标人数 大于 实际该学校  已经 录取的 人数,则先将 该人 选择的 学校 放到 wish中
                {
                    wish[t]=Node[t].wish[u];
                    break;
                }
            }

        for(int t=i; t<j; t++)    //根据 同一 排名 中的 人所 选择的学校,如果不为-1,表示已经 选择了,则将 人 放到 对应的 uty中
        {
            if(wish[t]!=-1)
                uty[wish[t]].push_back(Node[t].id);
        }

        i=j;

    }


    for(int i=0; i<m; i++)       //最后 直接 输出
    {
        if(uty[i].size())         //如果 该 学校 所录取的 人数 不为0,则 进行 排序 然后输出  后面输出 endl
        {
                    sort(uty[i].begin(),uty[i].end());
                    printf("%d",uty[i][0]);
                    for(int j=1; j<uty[i].size(); j++)
                        printf(" %d",uty[i][j]);
        }

            cout << endl;
    }
}

A1083



#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=1000;

struct node
{
    string name;
    string id;
    int grade;

    bool operator<(const node &t)
    {
        if(grade!=t.grade)
        {
            return grade>t.grade;
        }
    }
}Node[maxn];

int n;

int low,high;

int main()
{
    cin >> n;
    string temp1,temp2;
    int temp3;
    for(int i=0; i<n; i++)
    {
        cin >> temp1 >> temp2 >> temp3;
        Node[i]={temp1,temp2,temp3};
    }

    cin >> low >> high;

    sort(Node,Node+n);

    bool flag=false;
    for(int i=0; i<n; i++)
    {
        if(Node[i].grade>=low && Node[i].grade<=high)
        {
            flag=true;
            cout << Node[i].name << " " << Node[i].id << endl;
        }

    }

    if(flag==false)
        cout << "NONE" << endl;
    return 0;

}

A1092


#include<iostream>
#include<unordered_map>
using namespace std;

string temp1,temp2;

int main()
{

    unordered_map<char,int> aa;
    cin >> temp1 >> temp2;

    for(int i=0; i<temp1.size(); i++)
    {
        aa[temp1[i]]++;
    }

    for(int i=0; i<temp2.size(); i++)
    {
        aa[temp2[i]]--;
    }

    int yes=0,no=0;

    for(auto t:aa)
    {
        if(t.second>0)
            yes+=t.second;
        else
            no+=t.second;
    }

    //这个if-else可以 通过 第2个 例子 看出
    if(no<0)                //如果 是no<0,表示 店家提供的 不满足,但不代表 店家已经被 掏空了
        cout << "No" << " " << -no;
    else            //否则,店家 提供的 满足 输出 剩余的
        cout << "Yes" << " " << yes;
    
    return 0;

}

A1095



/*
    1.题目的意思是,输入n,m 其中n表示 信息个数,m表示查询 个数
    2.首先,要根据 时间,进行排序,然后 in后面 必须 有一个out,只有这样 车辆 才被 计算
    3.后面 根据 输入 一个 时间点 判断 在 该 时间点 内 停车的个数
    4.最后 输出 停车时间 最长的 车

*/


#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <vector>

using namespace std;

struct Event
{
    int tm, status;    //tm表示 进入 或者 离开 的 时间

    bool operator< (const Event &t) const
    {
        return tm < t.tm;          //按照 时间的 升序排列,不要 加 if判断,直接 排序 记住
    }
};

int get(vector<Event>& ets)            //计算 同一个 车,在 该 停车场内 所有 的停靠 时间
{
    int res = 0;
    for (int i = 0; i < ets.size(); i += 2)
        res += ets[i + 1].tm - ets[i].tm;

    return res;
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);

    unordered_map<string, vector<Event>> cars;

    char id[20], status[20];

    for (int i = 0; i < n; i ++ )
    {
        int hh, mm, ss;
        scanf("%s %d:%d:%d %s", id, &hh, &mm, &ss, status);
        int t = hh * 3600 + mm * 60 + ss;
        int s = 0;
        if (status[0] == 'o') s = 1;
        cars[id].push_back({t, s});
    }



    vector<Event> events;      //events 存放 满足 条件的 结构数组

    for (auto& item : cars)
    {
        auto& ets = item.second;         //ets现在 临时 存储 满足 条件的 值
        sort(ets.begin(), ets.end());
        int k = 0;

        for (int i = 0; i < ets.size(); i ++ )       //将 所有 合适的 值 放入到 ets中
            if (ets[i].status == 0)
            {
                if (i + 1 < ets.size() && ets[i + 1].status == 1)
                {
                    ets[k ++ ] = ets[i];
                    ets[k ++ ] = ets[i + 1];
                    i ++ ;
                }
            }
        //注意这里,因为ets是指针,所以 其实 已经 在cars中 删掉 多余的了
        ets.erase(ets.begin() + k, ets.end());   //删除掉 多余的
        for (int i = 0; i < k; i ++ ) events.push_back(ets[i]);       //现在 这个 event 存放的 是 满足条件的 拍照的车的in和out
    }

    sort(events.begin(), events.end());             //得到所有人的 满足条件的 值,这个时候 必须在 通过 时间 进行一个排序



    int k = 0, sum = 0;

    while (m -- )
    {
        int hh, mm, ss;
        scanf("%d:%d:%d", &hh, &mm, &ss);
        int t = hh * 3600 + mm * 60 + ss;        //将 所有 时间 转化为 秒

/*
    我们 已经 得到 满足 题意 的 车了,就是 events数组,下来 就根据 输入的 时间 统计 在 该 时间点 内 停放在 停车场内的 车的数量
    1.k必然是去 遍历 events数组,然后 现在 给你 一个时间,你去遍历 你 这个 数组,如果 在 给定的 时间 之前,一个in一个out,相当于该车
    在该时间内 没有停,如果 前面 有一个 in,没有 out,说明 该时间点内 汽车 在这里 停着。然后我们 再给定 下一个 时间点,然后k就着刚才的位置
    继续 往后 走,如果 在新给定的 时间点 前有 上一个 的out,那在sum中,也是 减,如果in还是 +

*/

        while (k < events.size() && events[k].tm <= t)
        {
            if (events[k].status == 0) sum ++ ;
            else sum -- ;
            k ++ ;
        }

        printf("%d\n", sum);

    }


    int maxt = 0;           //得到 的是 最大的 停车 时间
    for (auto& item : cars) maxt = max(maxt, get(item.second));      //这个 get 函数 计算 同一个汽车 在停车场内所有的 汽车 停靠时间

    vector<string> aaa;

    for(auto &item:cars)
    {
        if(get(item.second)==maxt)
            aaa.push_back(item.first);
    }

    sort(aaa.begin(),aaa.end());

    for(int i=0; i<aaa.size(); i++)
        printf("%s ",aaa[i].c_str());

    printf("%02d:%02d:%02d",maxt/3600,maxt%3600/60,maxt%60);

    return 0;
}


A1105


#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

const int maxn=10010;

int a[maxn],er[maxn][maxn];

int n;

bool cmp(int a,int b)
{
    return a>b;
}

int main()
{
    cin >> n;

    
    for(int i=0; i<n; i++)
        cin >> a[i];
        
    if(n==1)              //注意 这个 特殊 情况
    {
        cout << a[0];
        return 0;
    }

    sort(a,a+n,cmp);

    int M=ceil(sqrt(n));

    while(n%M!=0)
        M++;

    int N=n/M;

    int nai=1,zi=M,pi=1,gu=N,i=1,j=1;
    int num=0;

    while(num<n)
    {
        while(j<gu && num<n)
        {
            er[i][j]=a[num++];
            j++;
        }
        while(i<zi && num<n)
        {
            er[i][j]=a[num++];
            i++;
        }
        while(j>pi && num<n)
        {
            er[i][j]=a[num++];
            j--;
        }
        while(i>nai && num<n)
        {
            er[i][j]=a[num++];
            i--;
        }

        nai+=1;
        zi-=1;
        pi+=1;
        gu-=1;
        i++;
        j++;
        if(num==n-1)        //这种 情况 应对的 是 方形矩阵问题
        {
            er[i][j]=a[num++];
        }
    }

    for(int i=1; i<=M; i++)
    {
        for(int j=1; j<=N; j++)
        {
            if(j!=1)
                cout << " ";
            cout << er[i][j];
        }
        cout << endl;
    }
    return 0;
}

A1109



#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=10010;

int n,k;

struct node
{
    string name;
    int height;
}Node[maxn];

bool cmp(node a,node b)
{
    if(a.height!=b.height)
        return a.height>b.height;
    return a.name<b.name;
}

int main()
{
    cin >> n >> k;
    for(int i=0; i<n; i++)
    {
        cin >> Node[i].name >> Node[i].height;
    }
    sort(Node,Node+n,cmp);

    int j=0;
    for(int i=0; i<k; i++)
    {
        int renshu=n/k;
        if(i==0)
        {
            renshu+=n%k;
        }

        string temp[maxn];

        for(int r=renshu/2+1,l=r-1; r<=renshu || l>0; r++,l--)
        {
            if(r<=renshu)
                temp[r]=Node[j++].name;
            if(l>0)
                temp[l]=Node[j++].name;
        }

        for(int s=1; s<=renshu; s++)
        {
            if(s!=1)
                cout << " ";
            cout << temp[s];
        }
        cout << endl;
    }


}

A1128

在这里插入图片描述



#include<iostream>
#include<cstring>
using namespace std;

const int maxn=1010;

int n;

int main()
{
    cin >> n;
    bool row[maxn],zhu[maxn*2],fu[maxn*2];        //注意 主对角 和 副对角的 范围
    while(n--)
    {
        memset(row,false,sizeof(row));
        memset(zhu,false,sizeof(zhu));
        memset(fu,false,sizeof(fu));

        int temp1;
        cin >> temp1;

        bool success=true;
        for(int i=0; i<temp1; i++)       //i表示行
        {
            int temp2;
            cin >> temp2;         //temp2表示 列

            if(row[temp2] || zhu[i-temp2+temp1] || fu[i+temp2])     //主对角线上 差相同(横-纵) 副对角线上 和相同(横+纵)
                success=false;

            row[temp2]= zhu[i-temp2+temp1]=fu[i+temp2]=true;

        }
        if(success)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;

    }
    return 0;
}

A1129


/*
    题意:
    1.输入n表示 下面 每次 访问 商品的 编号,在输入 每次 系统 所推荐的商品 的个数
    2.对于第1个商品,肯定是 没有 推荐
    3.从 第2个 商品开始,先输出 我们 正在访问的 商品的 编号,然后 输出 系统的 推荐的商品编号
    4.注意 系统 根据 商品 出现 次数的 降序排列,如果 次数 相同,按照 商品编号升序 排列
    5.比如第3行,系统推荐 357,是因为我们 之前 已经访问了 357
    6.对于第4行,系统推荐537,因为当前访问5,也就是5访问了两次,所以5在第1个

*/
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=50010;

int ans[maxn]={0};          //统计 每个 商品的 个数

int top_k[maxn];     //表示 推荐的 前k个 商品

bool cmp(int a,int b)       //参数 a,b表示 商品的 编号
{
    if(ans[a]!=ans[b])                //肯定 是对 商品 的次数 进行 排序,不是 对top_k进行 排序,top_k中 存放的是 商品 的编号
        return ans[a]>ans[b];
    return a<b;          //如果 商品的 次数 相同 时按照 商品的编号 的 升序 排列
}


int main()
{
    int n,m;
    cin >> n >> m;
    int k=0,id;
    for(int i=0; i<n; i++)
    {
        cin >> id;
        if(i!=0)            //对于 第1个 值 不输出
        {
            cout << id << ":";
            for(int i=0; i<k; i++)
            {
                cout << " " << top_k[i];
            }
            cout << endl;
        }

        ans[id]++;      //统计每个商品的个数
        bool flag=false;   //flag去标记 新 出现的  商品 是 前k个还是 后起之秀,true表示 之前的  false表示 后起之秀
        for(int j=0; j<k; j++)
        {
            if(top_k[j]==id)   //之前 这个 id 是 在 前k个 中 出现过
            {
                flag=true;
                break;
            }
        }

        if(flag==false)   //这个 id 没有 在 前k个 商品 中出现
        {
            top_k[k++]=id;
        }
        sort(top_k,top_k+k,cmp);
        k=min(k,m);       //对于 后起之秀 而言,肯定 是 要 把 新出现的 放在 k+1位置,所以 上面 的  排序 也是 包含了 后起之秀,但是 我们 每次 只要 前k个
    }
}

A1132



#include<iostream>
using namespace std;

int n;

int main()
{
    cin >> n;
    string temp1,temp2,temp3;
    int temp4,temp5,temp6;
    while(n--)
    {
        cin >> temp1;
        temp2=temp1.substr(0,temp1.size()/2);
        temp3=temp1.substr(temp1.size()/2);
        temp4=stoi(temp2);
        temp5=stoi(temp3);
        temp6=stoi(temp1);
        if(temp4*temp5 && temp6%(temp4*temp5)==0)                         //浮点出现 错误 表示 除数为0了.所以 前 面 加上 temp4*tmep4
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }

}

A1147



#include<iostream>
using namespace std;

const int maxn=1010;

int n,m;

int h[maxn];        //h数组 存放数字



void postOrder(int root)
{
    if(root>n)
        return ;
    postOrder(2*root);
    postOrder(2*root+1);
    cout <<h[root];
    if(root!=1)         //如果不是 根节点,就输出空格,对于最后一个 值,肯定是根节点,所以就不用输出空格
        cout << " ";
}

int main()
{
    cin >> m >> n;
    while(m--)
    {
        for(int i=1; i<=n; i++)
            cin >> h[i];

        bool lt=false,gt=false;       //lt,gt分别表示有父亲节点小于孩子节点,父亲节点大于孩子节点
        for(int i=1; i<=n; i++)
        {
            for(int j=0; j<2; j++)     //j只能取 0或者1,表示 左孩子和右孩子
            {
                if(i*2+j<=n)    //表示i这个点有孩子
                {
                    int a=h[i],b=h[i*2+j];             //a表示父亲节点,b=h[i*2+0]表示左孩子,b=h[i*2+1]表示右孩子
                    if(a>b)
                        gt=true;
                    else
                        lt=true;
                }
            }
        }

        if(gt && lt)       //如果即出现大于又出现小于 则 表示 不是 堆
        {
            cout << "Not Heap" << endl;
        }
        else if(gt)
            cout << "Max Heap" << endl;
        else
            cout << "Min Heap" << endl;

        postOrder(1);
        cout << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值