PAT a 1060 Are They Equal (25 points)

Keywords: less

\quad is sorry that he didn't get the full mark and doesn't know what's wrong.

\What quad needs to pay attention to is that there is 00234.5 in the test point, so there is a number of zeros in front of it, which needs to be removed in advance; the second point is that there will be a number less than 0, such as 0.0123; the third point is to deal with the special situation that the input is 0, 0.0. I don't know what pit there is. Anyway, there is a small test point hanging (the test point of 2 points is generally a small pit). The code is as follows:

	#include <bits/stdc++.h>
	using namespace std;

	string solve(int n, string s)
	{
		int t = 0;
		while(s[t]=='0' && s[t+1]!='.')
		{
			s.erase(s.begin()); // There are examples like 0034.5, which need to be removed in advance
		}
		string res = "0.";
		if(s[0]=='0' && s[1]=='.')
		{
			int index = -1;
			for (int i = 2; i < s.length(); ++i)
			{
				if(s[i]!='0')
				{
					index = i;
					break;
				}
			}
			if(index==-1) 
			{
				for (int i = 0; i < n; ++i)
				{
					res += '0';
				}
				return res+"*10^0";
			}
			int zhishu = index-2;
			string temp = s.substr(index);
			if(temp.length()>=n) res += temp.substr(0, n);
			else
			{
				res += temp;
				for (int i = 0; i < n-temp.length(); ++i)
				{
					res += '0';
				}
			}
			res += "*10^";
			if(zhishu>0) 
			{
				res += '-';
				res += (char)(zhishu+'0');
			}
			else res += '0';
			return res;
		}
		int flag = s.find('.');
		if(flag==-1) flag = s.length();
		else
		{
			s.replace(flag, 1, "");
		}
		if(s.length()<n)
		{
			res += s.substr(0, s.length());
			for (int i = 0; i < n-s.length(); ++i)
			{
				res += '0';
			}
		}
		else res += s.substr(0, n);
		res += "*10^";
		res += (char)(flag+'0');
		return res;
	}
	int main(int argc, char const *argv[])
	{
		int n;
		string s1, s2;
		cin >> n >> s1 >> s2;
	    string r1 = solve(n, s1);
	    string r2 = solve(n, s2);
	    if(r1==r2)
	    {
	    	cout << "YES" << " " << r1 << endl;
	    }
	    else
	    {
	    	cout << "NO" << " " << r1 << " " << r2 << endl;
	    }
		return 0;
	}

Posted by Azad on Wed, 20 Nov 2019 08:18:12 -0800