X - A == B ?(第二季水)

本文介绍了一个C++程序设计问题,该程序需要比较两个输入的字符串是否相等,并去除小数点后的多余0。文章提供了两种解决方案,一种考虑了数字前导0的问题,另一种则简化了这一过程。

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
 
 
这个题神坑!!!
开始我写的代码考虑到数字前面多余的0和后面多余的0以及小数点
依旧是 结果错误!!!
最后发现不用考虑数字前面多余的0,出题的bug!!!   只需考虑小数点及其后末尾多余的0
以下为考虑全面的代码
#include<iostream>
#include<string.h>
using namespace std;
char a[20000],b[20000];
void f(char s[])
{
    int n;
    n=strlen(s);
    while(1){
        if(s[0]=='0'){
            for(int j=0;j<n;j++){
                s[j]=s[j+1];
            }
            n--;
        }
        else break;
    }
    if(strchr(s,'.')){
        for(int i=n-1;i>=0;i--){
            if(s[i]=='0'){
                s[i]='\0';
                n--;
            }
            else break;
        }
    }
    if(s[n-1]=='.')s[n-1]='\0';
}
int main()
{
    while(cin>>a>>b){
        f(a);
        f(b);
        if(strcmp(a,b))cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    return 0;
}


以下为AC代码

#include<iostream>
#include<string.h>
using namespace std;
char a[20000],b[20000];
void f(char s[])
{
    int n=strlen(s);
    if(strchr(s,'.')!=NULL){
        for(int i=n-1;i>=0;i--){
            if(s[i]=='0'){
                s[i]='\0';
                n--;
            }
            else break;
        }
    }
    if(s[n-1]=='.')s[n-1]='\0';
}
int main()
{
    while(cin>>a>>b){
        f(a);
        f(b); 
        if(strcmp(a,b))cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/farewell-farewell/p/5186093.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值