HDU 2054(string的删除运用(字符串数字的去0操作))

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42391248/article/details/82531246

A == B ?

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 126255    Accepted Submission(s): 20292

Problem Description

Give you two numbers A and B, if A is equal to B, you should print "YES", or print "NO".

Input

each test case contains two numbers A and B.

Output

for each case, if A is equal to B, you should print "YES", or print "NO".

Sample Input

1 2
2 2
3 3
4 3

Sample Output

NO
YES
YES
NO
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
void qu0(string &str)
{
	if(strchr(str.c_str(),'.'))
	{
		int last=str.length();
		while(str[--last]=='0')	
			str.erase(last,1);
		if(str[last]=='.')   
			str.erase(last,1);
	}
	while(str[0]=='0')
		if( str.length()!=1 ) 
			str.erase(0,1);
		else 
			return;
}
int main()
{
	string a,b;
	while(cin>>a>>b)
	{
		qu0(a);
		qu0(b);
		if(a.compare(b)==0) 
			cout<<"YES"<<endl;
		else 
			cout<<"NO"<<endl;
	}
}

 去0操作:

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
void qu0(string &str)
{
	int last=str.length();
	if(str.find('.')<10000000)
		while(str[--last]=='0')	
			str.erase(last,1);
		if(str[last]=='.')   
			str.erase(last,1);
	while(str[0]=='0')
		if( str.length()!=1 ) 
			str.erase(0,1);
		else 
			return;
	if(str[0]=='.')
		str="0"+str;
}
int main()
{
	string a,b;
	while(cin>>a>>b)
	{
		qu0(a);
		qu0(b);
		cout<<a<<endl;
		cout<<b<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_42391248/article/details/82531246