标签:
I want to be a good teacher, so at least I need to remember all the student names. However, there are
too many students, so I failed. It is a shame, so I don’t want my students to know this. Whenever I
need to call someone, I call his CLOSEST student instead. For example, there are 10 students:
A ? ? D ? ? ? H ? ?
Then, to call each student, I use this table:
Pos Reference
1 A
2 right of A
3 left of D
4 D
5 right of D
6 middle of D and H
7 left of H
8 H
9 right of H
10 right of right of H
Input
There is only one test case. The first line contains n, the number of students (1 ≤ n ≤ 100). The next
line contains n space-separated names. Each name is either ‘?’ or a string of no more than 3 English
letters. There will be at least one name not equal to ‘?’. The next line contains q, the number of
queries (1 ≤ q ≤ 100). Then each of the next q lines contains the position p (1 ≤ p ≤ n) of a student
(counting from left).
Output
Print q lines, each for a student. Note that ‘middle of X and Y ’ is only used when X and Y are
both closest of the student, and X is always to his left.
Sample Input
10
A ? ? D ? ? ? H ? ?
4
3
8
6
10
Sample Output
left of D
H
middle of D and H
right of right of H
模拟一下,就可以了!
就是看right of或者left of的时候要注意,另一边没有人才行,否则就是middle of了
#include<cstdio>
#include<iostream>
using namespace std;
#include<cstring>
char a[105][4];
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;++i){
cin>>a[i];
}
int m;
cin>>m;
while(m--){
int s;
cin>>s;
int i=s-1;
if(strcmp(a[i],"?")){
cout<<a[i]<<endl;
}
else if(i-1>=0&&strcmp(a[i-1],"?")){
if(i+1<n&&strcmp(a[i+1],"?"))
cout<<"middle of "<<a[i-1]<<" and "<<a[i+1]<<endl;
else
cout<<"right of "<<a[i-1]<<endl;
}else if(i+1<n&&strcmp(a[i+1],"?")){
if(i-1>=0&&strcmp(a[i-1],"?"))
cout<<"middle of "<<a[i-1]<<" and "<<a[i+1]<<endl;
else
cout<<"left of "<<a[i+1]<<endl;
}else {
for(int j=2;;++j){
if(i+j<n&&i-j>=0&&strcmp(a[i+j],"?")&&strcmp(a[i-j],"?")){
cout<<"middle of "<<a[i-j]<<" and "<<a[i+j]<<endl;break;
}else if(i+j<n&&strcmp(a[i+j],"?")){
for(int k=0;k<j;++k){
cout<<"left of ";
}
cout<<a[i+j]<<endl;break;
}else if(i-j>=0&&strcmp(a[i-j],"?")){
for(int k=0;k<j;++k){
cout<<"right of ";
}
cout<<a[i-j]<<endl;break;
}
}
}
}
// while(1);
return 0;
}
标签:
原文地址:http://blog.csdn.net/mymilkbottles/article/details/51345341