加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

HDU 2054 判断大数是否相等

发布时间:2020-12-31 19:49:04 所属栏目:大数据 来源:网络整理
导读:原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2054 题解:这个题不严谨,仅判断后向0就行,而且不用判正负。 代码: #includeiostreamusing namespace std;const int maxn = 100000;void cleanlastzero(char str[]) { int len = strlen(str),digit_

原题链接: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] = '';
        }
        if (i == digit_place)
            str[i] = '';
    }
}
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]=='')               ///如果遇到结束,说明全为00000 所以回退一格 变为0  碰到小数点,就直接格式变为.0001
		flag--;
	int i;
	for (i = 0;temp[flag]!='';i++) {
		str[i] = temp[flag++];
	}
	str[i] = '';
}
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] = '';
		}
		if (i&&i == digit_place)  /////如果遍历到小数点了并且不是字符串的第一个话就把小数点抹掉,比如23.0---23
			str[i] = '';           
		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;
	}
}

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读