队列
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> using namespace std; const int n=10; int a[n],b,c; int head,tail;//创建队列 int fangwen(int b) { for(int i=1;i<n;i++) if(a[i]==b) cout<<i<<endl; cout<<"-1"<<endl; }//理论上队列不能访问非对首元素 若用数组模拟可用数组方式访问 int push(int c) { a[tail+1]=c; for(int i=1;i<n+1;i++) cout<<a[i]<<" "; cout<<endl; }//在队尾加入一个元素 void front() { if(head>=tail) cout<<"-1"; else cout<<a[head]<<endl; }//获得对首元素的值 void pop() { if(head<=tail) { a[head]=0; head++; } for(int i=1;i<n+1;i++) cout<<a[i]<<" "; cout<<endl; }//依次弹出对首元素 int main() { for(int i=1;i<n;i++) { scanf("%d",&a[i]); tail++; } scanf("%d",&b); scanf("%d",&c); fangwen(b); push(c); front(); pop(); return 0; }
队列练习 题目:给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请输出最终的队头元素。 操作解释:1表示入队,2表示出队
输入 N(操作个数)
N个操作(如果是入队则后面还会有一个入队元素)
具体见样例(输入保证队空时不会出队)
输出 最终队头元素,若最终队空,输出”impossible!”(不含引号)
样例输入 Sample Input 输出
3 9
1 2
1 9
2
参考程序
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<queue> using namespace std; /*int n,b,tail,head,a; int m[1001]; void push(int q) { tail++; m[tail]=q; } void pop() { if(head<=tail) { m[head]=0; ++head; } } void sds() { if(head>tail) cout<<"impossible!"; else printf("%d",m[head]); } int main() { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a); if(a==1) { scanf("%d",&b); push(b); } if(a==2) pop(); } sds(); return 0; }*/ queue<int>q; int n,a,b; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a); if(a==1) { scanf("%d",&b); q.push(b); } if(a==2) q.pop(); } if(q.front()!=0) printf("%d",q.front()); else cout<<"impossible!"; return 0; }
(此题与队列练习1相比改了2处:1加强了数据 2不保证队空时不会出队)
给定一个队列(初始为空),只有两种操作入队和出队,现给出这些操作请
输出最终的队头元素。 操作解释:1表示入队,2表示出队
输入描述 Input Description
N(操作个数)
N个操作(如果是入队则后面还会有一个入队元素)
具体见样例(输入保证队空时不会出队)
输出描述 Output Description
最终队头元素,若最终队空,或队空时有出队操作,输出”impossible!”(不含引号)
样例输入 Sample Input
3
1 2
2
2
样例输出 Sample Output
impossible! #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<queue> using namespace std; int n,a,b,tail,head; queue<int>q; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a); if(a==1) { scanf("%d",&b); q.push(b);//push对尾插入一个元素 } if(a==2) { if(q.empty()) { cout<<"impossible!"; return 0; } else q.pop();//empty判断是否队列为空 pop删除队首元素 //本函数用来测试变量是否已经配置。若变量已存在、非空字符串或者非零,则返回 false 值;反之返回 true。 } } if(q.empty()) n++; else cout<<q.front();//队首元素为0,及队列为空 return 0; }
比起第一题,本题加了另外一个操作,访问队头元素(编号3,保证访问队头元素时或出队时队不为空),现在给出这N此操作,输出结果
输入 N
N次操作(1入队 2出队 3访问队头)
输出 K行(K为输入中询问的个数)每次的结果
输入 输出
6 7
1 7 9
3
2
1 9
1 7
3
参考程序
#include<iostream> #include<cstdio> #include<queue> using namespace std; queue<int>q; int main() { int a,m,b; cin>>a; for(int i=1;i<=a;i++) { cin>>m; if(m==1) { cin>>b; q.push(b); } if(m==2) { if(q.empty()) { cout<<"impossible!"; return 0; } q.pop(); } if(m==3) { cout<<q.front()<<endl; } } if(q.empty()) { a++; } else return 0; }
大数学家高斯小时候偶然间发现一种有趣的自然数集合Blah,对于以a为基的集合Ba定义如下:
(1) a是集合Ba的基,且a是Ba的第一个元素;
(2)如果x在集合Ba中,则2x+1和3x+1也都在集合Ba中;
(3)没有其他元素在集合Ba中了。
现在小高斯想知道如果将集合Ba中元素按照升序排列,第N个元素会是多少?
输入
输入包括很多行,每行输入包括两个数字,集合的基a(1<=a<=50))以及所求元素序号n(1<=n<=1000000)
输出
对于每个输入,输出集合Ba的第n个元素值
样例输入样例输出
1 100 418
28 5437 900585
参考程序
#include<iostream> #include<cstdio> using namespace std; long long stack[1000010]; long long a,n,p2,p3,ntop; int main() { while(scanf("%d%d",&a,&n)!=EOF) { p2=1; p3=1; ntop=1; stack[1]=a; while(true) { if(ntop==n) { printf("%d\n",stack[ntop]); break; } if(2*stack[p2]+1<3*stack[p3]+1) { ntop++; stack[ntop]=2*stack[p2]+1; p2++; } else { if(2*stack[p2]+1>3*stack[p3]+1) { ntop++; stack[ntop]=3*stack[p3]+1; p3++; } else { ntop++; stack[ntop]=3*stack[p3]+1; p3++; p2++; } } } } return 0; }