HDU 2054 判断大数是否相等

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2054

题解:这个题不严谨,仅判断后向0就行,而且不用判正负。

代码:

#include<iostream>
using namespace std;
const int maxn = 100000;
void cleanlastzero(char str[]) {
    int len = strlen(str), digit_place = -1;
    for (int i = 0;i < len;i++) {
        if (str[i] == '.') {
            digit_place = i;
            break;
        }
    }
    if (digit_place == -1)
        return;
    else {
        int i;
        for (i = len - 1;str[i] == '0';i--) {
            str[i] = '\0';
        }
        if (i == digit_place)
            str[i] = '\0';
    }
}
int main() {
    char str1[maxn], str2[maxn];
    while (cin >> str1 >> str2) {
        cleanlastzero(str1);
        cleanlastzero(str2);
        if (strcmp(str1, str2)==0)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
}

全面代码:没怎么优化,就是模拟各种情况,比较好理解,贴出来,如果漏情况了请告知我,谢谢

#include<iostream>
using namespace std;
const int maxn = 100000;
int judgePositive(char str1[], char str2[]) {  //判断正负 并记住
	int flag1 = 1, flag2 = 1;
	if (str1[0] == '-')
		flag1 = 0;
	if (str2[0] == '-')
		flag2 = 0;
	if (flag1 != flag2)
		return 0;
	return 1;
}
void cleanPrezero(char str[]) {去除前向0  最后格式为包含小数点的小数点前没0 (0.0----.0)
	char temp[maxn];
	strcpy(temp, str);
	int len = strlen(str);
	int flag = 0;
	if (str[0] == '+' || str[0] == '-')
		flag = 1;                        如果有符号 跳到下一个位置
	for (;str[flag] == '0';flag++);
	if (str[flag]=='\0')               ///如果遇到结束,说明全为00000 所以回退一格 变为0  碰到小数点,就直接格式变为.0001
		flag--;
	int i;
	for (i = 0;temp[flag]!='\0';i++) {
		str[i] = temp[flag++];
	}
	str[i] = '\0';
}
void cleanlastzero(char str[]) {            //去除后向0
	int len = strlen(str), digit_place = -1;
	for (int i = 0;i < len;i++) {
		if (str[i] == '.') {
			digit_place = i;
			break;
		}
	}
	if (digit_place == -1)   如果没有小数点不处理结束
		return;
	else {
		int i;
		for (i = len - 1;str[i] == '0';i--) {
			str[i] = '\0';
		}
		if (i&&i == digit_place)  /如果遍历到小数点了并且不是字符串的第一个话就把小数点抹掉,比如23.0---23
			str[i] = '\0';           
		if (i == 0)
			str[i] = '0';         如果遍历到小数点是第一个字符,那么说明后项和前向全为0,那么整个字符串就是0了啊
	}
}
int main() {
	char str1[maxn], str2[maxn];
	while (cin >> str1 >> str2) {
		int flag = judgePositive(str1, str2);
		cleanPrezero(str1);
		//cout << str1 << endl;
		cleanPrezero(str2);
		//cout << str2 << endl;
		cleanlastzero(str1);
		//cout << str1 << endl; 
		cleanlastzero(str2);
		//cout << str2 << endl;
		if ((strcmp(str1, str2) == 0&&flag)||(strcmp(str1, str2) == 0 && strcmp(str1, "0") == 0)) // 符号位相等并且2个字符串相等  
			cout << "YES" << endl;                                                                 //符号位不同但是都是0也相等  
		else
			cout << "NO" << endl;
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值