首页 > 代码库 > HDU 5059 Help him(细节)

HDU 5059 Help him(细节)

HDU 5059 Help him

题目链接

直接用字符串去比较即可,先判断原数字正确不正确,然后写一个判断函数,注意细节,然后注意判掉空串情况

代码:

#include <cstdio>
#include <cstring>

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;
}


HDU 5059 Help him(细节)