标签:
简单贪心。注意输出格式,每到80个字符就换一行。
#include<cstdio> #include<cstring> #include<cmath> #include<stack> #include<algorithm> using namespace std; const int maxn = 2000 + 10; int a[maxn], ans[maxn]; int n; void read() { for (int i = 1; i <= n; i++) { char s[5]; scanf("%s", s); a[i] = s[0] - ‘A‘ + 1; } } void work() { int first = 1, last = n; for (int i = 1; i <= n; i++) { int tmp = last - first + 1; bool f = 0; for (int j = 0; j<tmp / 2; j++) { if (a[first + j]>a[last - j]) { ans[i] = a[last], last--, f = 1; break; } else if (a[first + j]<a[last - j]) { ans[i] = a[first], first++, f = 1; break; } } if (f == 0) ans[i] = a[first], first++; } int cnt = 0; for (int i = 1; i <= n; i++) { printf("%c", ans[i] - 1 + ‘A‘); cnt++; if (cnt == 80) { printf("\n"); cnt = 0; } } printf("\n"); } int main() { while (~scanf("%d", &n)) { read(); work(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zufezzt/p/5263137.html