Skip to content

Instantly share code, notes, and snippets.

@ninehills
Created September 29, 2011 07:17
Show Gist options
  • Save ninehills/1250166 to your computer and use it in GitHub Desktop.
Save ninehills/1250166 to your computer and use it in GitHub Desktop.
二主楼找座问题
// see : http://acm.nankai.edu.cn/p1010.html
#include <stdio.h>
#define MAX_SEAT 51
// 计算n个座的情况下第x号座和过道的距离。(x: 0~ n-1)
int distance(int x, int n) {
if(x < n/2)
return x;
else
return n - x -1;
}
int main(void){
int n;
char seat[MAX_SEAT];
scanf("%d", &n);
scanf("%s", seat);
int i, best = -1, flag = 0;
for (i = 0; seat[i] != '\0' && i < MAX_SEAT; ++i) {
if(seat[i] == 'E'){
if(flag == 1){
//连续两个空位,将两个空位分别和best相比较
if(distance(i - 1, n) > distance(best, n))
best = i - 1;
if(distance(i, n) > distance(best, n))
best = i;
} else {
flag = 1;
}
} else {
flag = 0;
}
}
printf("%d\n", best);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment