【问题描述】
新学期开始了,小哈是小哼的新同桌。小哼向小哈询问 QQ号,小哈当然不会直接告诉小哼啦,原因嘛你懂的。所以小哈给了小哼一串加密过的数字,同时小哈也告诉了小哼解密规则。规则是这样的:首先将第 1个数删除,紧接着将第2个数放到这串数的末尾,再将第3个数删除并将第4个数放到这串数的末尾,再将第5个数删除……直到剩下后一个数,将后一个数也删除。按照刚才删除的顺序,把这些删除的数连在一起就是小哈的QQ啦。现在你来帮帮小哼吧。小哈给小哼加密过的一串数是“6 3 1 7 5 8 9 2 4”。
【算法代码一】
#include<bits/stdc++.h>
using namespace std;
int a[100];
int main() {
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int head=1;
int tail=n+1;
while(head<tail) {
cout<<a[head]<<" ";
head++;
a[tail]=a[head];
tail++;
head++;
}
return 0;
}
/*
input:
9
6 3 1 7 5 8 9 2 4
output:
6 1 5 9 4 7 2 8 3
*/
【算法代码二:利用结构体】
#include <bits/stdc++.h>
using namespace std;
struct duilie{
int data[100];
int head;
int tail;
};
int main() {
int n;
cin>>n;
duilie q;
q.head=1;
q.tail=1;
for(int i=1; i<=n; i++) {
cin>>q.data[q.tail];
q.tail++;
}
while(q.head<q.tail) {
cout<<q.data[q.head]<<" ";
q.head++;
q.data[q.tail]=q.data[q.head];
q.tail++;
q.head++;
}
return 0;
}
/*
input:
9
6 3 1 7 5 8 9 2 4
output:
6 1 5 9 4 7 2 8 3
*/
【算法代码三:利用STL】
#include<bits/stdc++.h>
using namespace std;
int main() {
int a;
int n;
cin>>n;
queue<int> s;
for(int i=0; i<n; i++) {
cin>>a;
s.push(a);
}
while(s.empty()!=1) {
cout<<s.front()<<" ";
s.pop();
s.push(s.front());
s.pop();
}
return 0;
}
/*
input:
9
6 3 1 7 5 8 9 2 4
output:
6 1 5 9 4 7 2 8 3
*/