I was not happy after seeing a certain thing and wrote 150+ lines, so I wrote 33 lines of code with my bare hands (C++)

The original question roughly means: input two numbers a and b, if a=b, output "YES", otherwise output "NO".

This question seems to define two integer variables, but not long long, or double. However, these functions have a range, that is to say, no matter which function you use to define variables, when the value reaches a certain size , will always overflow, which means that these functions cannot be used, at least I won't use them. So, the question is, since these functions that define numbers can't be used, what should we use?

At this time, we naturally think of strings. Yes, for a string, there is no overflow.

When it comes to strings, we use strings, and there is another problem with strings. We know that the numbers 00201.00 and 201 are equal, but the strings 00201.00 and 201 are not equal. So we need to truncate the 0 before the 2 and the 0 after the 1, and then compare.

In this way, it is necessary to find the position of the closest 0 in front of 2 and behind 1. The position of 0 in front of 2 can be determined by the position of 2, and the position of 0 behind 1 can also be determined by the position of 1. Then use the find_first_not_of and find_last_not_of functions in the string class. It is not enough to find the position, but to cut off the middle section, you need to use the substr function. After the interception is completed, there is a problem again. 0201.00 has become 201. and 200 are still different. In this way, it is necessary to first determine whether there is a decimal point in the string, and intercept it normally, if not, add a ".". This uses the find function.

After the above processing, 0201.00 becomes 201., and 201 also becomes 201., so the two strings are equal. The specific code is as follows:

#include<iostream>

#include<string>

using namespace std;

string PrtStr(string a)

{

int b=a.find_first_not_of("0");

int c=a.find_last_not_of("0");

c++;

string d;

d=a.substr(b,c-b);

return d;

 }

 string PD(string::size_type a,string b)

 {

 a=b.find(".");

 if(a!=b.npos)return b;

 elsereturn b+".";

 }

 intmain()

 {

 string a,b;

while(cin>>a>>b)

{

 string::size_type m;

 a=PD(m,a);

 b=PD(m,b);

 string c=PrtStr(a);

 string d=PrtStr(b);

if(c==d)cout<<"YES\n";

elsecout<<"NO\n";

}

  return 0;

  }

After the code is pasted, let's talk about the usage of these functions in detail:

***The use of these functions requires the introduction of the header file string, ie #include<string>

(1) find_first_not_of and find_last_not_of: The usage of these two functions is the same. Take find_first_not_of as an example. This function appears in the form of str.find_first_not_of(str1), which means to find the string str1 in the string str, and its return value is an int constant, indicating that the first character of the string str is the first one that is not in str1. Characters (note that the previous substring is not necessarily a complete str1, as long as there are characters in str1, it will be automatically skipped until the characters that are in str but not in str1 are found, and the first character from left to right characters are recorded as 0 (this is just my own understanding)).

(2)substr:这个函数是用于截取字符串的,那么其返回值理所当然是字符串了。出现的形式为:str.substr(int1,int2),str是被截的字符串,int1和int2是两个int型常量,int1是截取的第一个字符在str中的位置(str从左往右第一个字符还是记作0),int2是截取的字符串的长度,也就是说子字符串是str的第int1位字符开始向右取int2个字符得到的字符串。

(3)find:它是判断字符串中是否含有某个子字符串,出现形式为str.find(str1),其返回值是一个string::size_type的数据(我也不知道是啥),当确认含有的时候(必须是整个str1字符串,而不是str1含有的某个字符),它返回str1第一个字符在str中的位置(同上。。。)如果确认不含的时候,它返回npos(出现形式为str.npos)。

基本上就这么多了。。。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326026521&siteId=291194637