题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12839&courseid=269
水题 debug半天 每一次只考虑当前的数字及前面的那个数字。
代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define INF 1000000100 #define MAX 1000000200 using namespace std; char a[15]; int b[1005]; int main() { int N; while(scanf("%d", &N), N) { int num = 0; int judge = 0; //1 唯一存在 2 不存在 3 很多种 int l = -INF, r = INF; b[0] = 0; for(int i = 1; i <= N; i++) { //把对应的字符串转化成数字 int s; scanf("%s", a); if(a[0] == ‘x‘) b[i] = INF; else { s = 0; int j; if(a[0] == ‘-‘) j = 1; else j = 0; for(j = j; a[j]; j++) { s *= 10; s += a[j] - ‘0‘; } if(a[0] == ‘-‘) b[i] = -s; else b[i] = s; } } b[0] = MAX; for(int i = 1; i <= N && judge != 2; i++) { if(b[i] == INF && b[i-1] == INF) judge = 2; else if(b[i] == INF && b[i-1] != INF) { if(i%2) r = min(r, b[i-1]-1); //当前是奇数位 x比前面一个数小 那么x的上限应该为l与前面一个数中较小的 else l = max(l, b[i-1]+1); //当前是偶数位 x比前面的一个数大 那么x的下限应该是x和前面一个数较大的 } else if(b[i] != INF && b[i-1] == INF) { if(i%2) l = max(l, b[i]+1); else r = min(r, b[i]-1); } else { if(i%2 && b[i] >= b[i-1] || !(i%2) && b[i] <= b[i-1]) judge = 2; } } if(judge == 2 || l > r) printf("none\n"); else if(l == r) printf("%d\n", l); else if(l < r) printf("ambiguous\n"); } return 0; }
时间: 2024-11-12 22:04:11