if A is equal to B ,print"Yes",or print "No"

Pages: 12
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

I've tried many times,the output is correct but when it's a wrong answer when I submit it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include<iostream>
#include<string>
using namespace std;

string cut(string X)
{
	long i; 
		if(X.find(".")!=X.npos)
		{
			i=X.length();
 			while((X[--i]=='0'||X[i]=='.')&&i >0) X.erase(i,1); 
		}
		while((X[0]=='0')&&X.length()>1) X.erase(0,1);
		if(X==".") X="0";
	return X;
}

int main()
{
	string A,B;
	while(cin>>A>>B)
	{
			if(cut(A)==cut(B)) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
	}
	return 0;
}
Last edited on
Why don't you simply take input into ints instead of strings and get rid of cut()?
Also, the problem statement says nothing about printing the input back out. It just says to print either "YES" or "NO".
Because these inputs may be 0000010 and 10,in this case,I chose strings
and sorry I posted it incorrectly in output .I've re-edited it,but it's also a wrong answer.
Because these inputs may be 0000010 and 10
And?
how can I compare the two by using ints?
and it needs us to think of these situations:00000.2000==0.200 or 00000==0.00000 or much more...
closed account (48T7M4Gy)
By the sound of it you have to choose the appropriate variable type that gives the greatest range to accommodate all versions of the generic type. eg use double where double and float is possible.
But I think string can give the greatest range QAQ because it offers a bigger space for us to compare
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
using namespace std;

string a, b;
char t;
long i;

int
main (void) {
    while (cin >> a >> b) {
        if (a.find(".") != a.npos) {
            i = a.length();
            while ((a[--i] == '0' || a[i] == '.') && i > 0) {
                t = a[i];
                a.erase(i, 1);
                if (t == '.') break;
            }
        }
        if (b.find(".") != b.npos) {
            i = b.length();
            while ((b[--i] == '0' || b[i] == '.') && i > 0) {
                t = b[i];
                b.erase(i, 1);
                if (t == '.') break;
            }
        }
        while ((a[0] == '0') && a.length() > 1) {a.erase(0, 1);}
        while ((b[0] == '0') && b.length() > 1) {b.erase(0, 1);}
        if (a == ".") a = "0";
        if (b == ".") b = "0";
        if (a == b) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}


this code above is right.I want to know why.
The code above is not right:
18.3E1 183
NO
-0 0
NO


You're making this far more complicated than it needs to be. Just use doubles.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>

int main() {
    float A, B;

    while (std::cin >> A >> B) 
    {
        if(A != B)
        {   
            std::cout << "NO" << std::endl;
        }
        else
        {   
            std::cout << "YES" << std::endl;
        }
     }
 }

I just posted like this,it's also wrong.
That looks right to me Alphar, is this for a class? What are the requirements?
what it said is just what I posted...
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

and it's our homework:(
closed account (48T7M4Gy)
The code immediately above appears to work OK. What's wrong with it Alphar?
it's our homework.when I handed in,it told me that I was wrong:(
someone else told me that the two numbers may be over 5000 bits,how can I deal with that?
Last edited on
If you have to take into account very large or very small numbers, why don't you use long or long long double? That's just my input
closed account (48T7M4Gy)
the two numbers may be over 5000 bits, how can I deal with that?


Check each number bit by bit. For inequality the first pair of corresponding bits that aren't equal is enough to say NO ...

In that case the two numbers can be represented as strings.
Last edited on
In that case the two numbers can be represented as strings.


so..why am I wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 #include<iostream>
#include<string>
using namespace std;

string cut(string X)
{
	long i; 
		if(X.find(".")!=X.npos)
		{
			i=X.length();
 			while((X[--i]=='0'||X[i]=='.')&&i >0) X.erase(i,1); 
		}
		while((X[0]=='0')&&X.length()>1) X.erase(0,1);
		if(X==".") X="0";
	return X;
}

int main()
{
	string A,B;
	while(cin>>A>>B)
	{
			if(cut(A)==cut(B)) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
	}
	return 0;
}
someone else told me that the two numbers may be over 5000 bits
I highly doubt that the point of this exercise was to make you consider such cases.
I highly doubt that the point of this exercise was to make you consider such cases.

oh yes,now I've solved this:) thank u
closed account (48T7M4Gy)
Play it the way you see it Alphar. Nobody here can read minds. You have a number of alternative pieces of advice and you need to use your own judgement on which one(s), if any, suit your solution. Best wishes. :)
Pages: 12