題目鏈接
直接用字符串去比較即可,先判斷原數字正確不正確,然後寫一個判斷函數,注意細節,然後注意判掉空串情況
代碼:
#include#include const int N = 105; char n[N], a[N], b[N]; bool judge(char *str) { int len = strlen(str); if (len == 0) return false; int s = 0; if (str[s] == '-') { if (len == 1) return false; s++; } if (len != 1 && str[s] == '0') return false; for (int i = s; i < len; i++) { if (str[i] < '0' || str[i] > '9') return false; } return true; } bool big(char *a, char *b) { if (a[0] == '-' && b[0] != '-') return false; if (a[0] != '-' && b[0] == '-') return true; int i = 0, j = 0; int lena = strlen(a), lenb = strlen(b); bool flag = true; if (a[i] == '-' && b[j] == '-') { i++; j++; flag = false; } if (lena > lenb) return flag; if (lena < lenb) return !flag; while (i < lena && j < lenb) { if (a[i] > b[j]) return flag; else if (a[i] < b[j]) return !flag; i++; j++; } return true; } bool solve() { if (!judge(n)) return false; if (big(n, a) && big(b, n)) return true; return false; } int main() { while (gets(n) != NULL) { scanf("%s%s%*c", a, b); while (!judge(a) || !judge(b)); printf("%s\n", solve() ? "YES" : "NO"); } return 0; }