杭電2054 A == B ?

看完題目後覺得題太簡單了,給的測試案例也都是整數,許多情況根本考慮不到。主要是考慮小數中末尾的0,如9.0和9.00是相等的數,所以輸入的數據類型最好定義成字符串,然後在比較兩個數前先把末尾的0去掉。另外,在定義字符串是如果你用一個a[100]來輸入交上去後也會報錯,當你開到a[50000]時它纔會讓你AC!!

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<cstring>
using namespace std;
void qq(char *a)
{
 int len,i;
 len=strlen(a)-1;
 for(i=len;i>=0;i--)
 {
  if(a[i]=='0')     
   len--;                             //去掉小數點後的0
  else
   break;
 }
 if(a[len]=='.')    //如果小數點後全是0,那小數點也得去掉
  len--;
 a[len+1]='\0';     //通過加結束標誌來去掉
}
int main()
{
 int i,len;
 char a[50000],b[50000];
 while(cin>>a>>b)
 {
  len=strlen(a);
  for(i=0;i<len;i++)
  {
   if(a[i]=='.')    //對有小數點的數據處理
    qq(a);
   

  }
  len=strlen(b);
  for(i=0;i<len;i++)
  {
   if(b[i]=='.')
    qq(b);
   
  }
  if(strcmp(a,b))
   cout<<"NO"<<endl;
  else
   cout<<"YES"<<endl;
 }
 return 0;
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章