杭电2054

最开始看到这道题是很早之前,当时刚刚开始在hdu写题想着这题太简单了,就吧啦吧啦按照一般常规的思维判断两个数相等,等WA之后想了好久才发现它并没有给出输入的数的范围,所以这里要用字符串的输入来解决了。总结一下这道题要注意的地方:

1、没给出数的范围,要用字符串来输入。

2、用字符串输入要注意0的处理。

3、最先想到是用字符串数组,但是对于一个比较懒的人来说,当然是怎么写简单怎么写的啦。所以就想到了直接用string解决。

先贴代码:

 1 #include<iostream>
 2 #include <cstring>
 3 #include <string>
 4 
 5 using namespace std;
 6 string solve(string a){
 7     if(a.find(".")!=a.npos){//如果是小数
 8         while((a.substr(a.length()-1,1))=="0"){//删掉末尾的0
 9             a=a.substr(0,a.length()-1);
10         }
11         if((a.substr(a.length()-1,1))=="."){//这是判断如果小数点后全是0,则把小数点一起去掉
12             a=a.substr(0,a.length()-1);
13         }
14     }
15     while(a[0]=='0'){//删掉最前面的0
16             a=a.substr(1);
17     }
18     return a;
19 }
20 int main(){
21     string a,b;
22     while(cin>>a>>b){
23         a=solve(a);
24         b=solve(b);
25         if(a.substr(0)!=b.substr(0)){
26             cout<<"NO"<<endl;
27         }else{
28             cout<<"YES"<<endl;
29         }
30     }
31     return 0;
32 }

然后也学到了新知识:

1、判断str1里面是否有str2:(首先要有string库)

  str1.find(str2);//找到了返回下标,找不到返回str1.npos

2.s.substr();//s.substr(index)取以index起始到结尾的子串 s.substr(index,length)取以index为起始,长度为length的子串。

  删掉字符串最后一位:a=a.substr(0,a.length()-1),删掉第0位:a=a.substr(1,a.length()-1);或a=a.substr(1)

  判断最后一位是不是0:a.substr(a.length()-1,1)=="0"或a[a.length()-1]=='0';

3.字符串相关操作:在尾部追加字符串的话可以a=a+str,同样也可以在首部追加a=str+a,取长度a.length();

4.函数传值是复制,要记得return。(有待进一步了解)

Follow my heart and hold on!
原文地址:https://www.cnblogs.com/Young-C/p/7748408.html