队列简单运用举例

1,输入若干数,求对他们进行从大到小的排序,输出排名。

#include <bits/stdc++.h>
using namespace std;
#define n 10
int main(){
    int a[n];
    queue<int>q;
    for(int i=0;i<n;i++){
        cin>>a[i];
        q.push(a[i]);
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<n-1;j++)
            if(a[j]<a[j+1])
            {
                int temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
    for(int i=0;i<n;i++)
    {
        int temp=q.front();
        q.pop();
        for(int j=n-1;j>=0;j--)
            if(a[j]==temp)cout<<j+1<<endl;
    }
    return 0;
}

 2,加减算式

样例输入

99+88-77

样例输出

110
#include <bits/stdc++.h>
using namespace std;
int transform(char *p){
    int temp=0,i=0,res=0;
    while(*p!='\0'){
        temp+=(*p-'0')*pow(10,i);
        p++;
        i++;
    }
    while(i--){
        res+=(temp%10)*pow(10,i);
        temp/=10;
    }
    return res;
}
void solve(char a[])
{
    int ans=0;
    queue<char>q1;
    queue<int>q2;
    char *p1=a,*p2=a;
    while(*p1!='\0'){
        p1++;
        if(*p1=='+'||*p1=='-'){
            char temp=*p1;
            q1.push(temp);
            *p1='\0';
            q2.push(transform(p2));
            *p1=temp;
            while(p2!=p1)p2++;
            p2++;
        }
        if(*p1=='\0')q2.push(transform(p2));
                
    }
    ans+=q2.front();q2.pop();
    do{
        if(q1.front()=='+')ans+=q2.front();
        else if(q1.front()=='-')ans-=q2.front();
        q1.pop();
        q2.pop();
    }while(q2.size()>0);
    cout<<ans;
}
int main(){
    char a[10000];
    cin>>a;
    solve(a);
    return 0;
}

3,找出最早出现的最短单词

#include <bits/stdc++.h>
using namespace std;
int main(){
    char a[200];
    cin.get(a,200);
    queue<string>q;
    int mi=200;
    char *p1=a,*p2=a;
    while(*p1++==' '&&p2++);
    while(*p1!='\0'){
        p1++;
        if(*p1==' '){
            *p1='\0';
            q.push(p2);
            *p1=' ';
            mi=min(mi,(int)(p1-p2));
            while(*++p1==' ');
            while(p2!=p1)p2++;
        }
        if(*p1=='.'){
            *p1='\0';
            if(*(p1-1)==' '){
                while(*p2++==' ');
                if(p1-p2>0)mi=min(mi,(int)(p1-p2));
            }
            else{
                mi=min(mi,(int)(p1-p2));
            }
            if(p1-p2>0)q.push(p2);
        }
    }
    /*while(q.size()){
        cout<<q.front()<<endl;
        q.pop();
    }*/
    while(q.size()){
        if(q.front().length()==mi){
            cout<<q.front();
            break;
        }
        q.pop();
    }
    return 0;
}

未完待续。。

持续施工!收藏点起来!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值