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

Pages: 12
Oct 29, 2014 at 5:24am
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 Oct 29, 2014 at 6:44am
Oct 29, 2014 at 5:31am
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".
Oct 29, 2014 at 6:33am
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.
Oct 29, 2014 at 6:44am
Because these inputs may be 0000010 and 10
And?
Oct 29, 2014 at 9:21am
how can I compare the two by using ints?
Oct 29, 2014 at 9:26am
and it needs us to think of these situations:00000.2000==0.200 or 00000==0.00000 or much more...
Oct 29, 2014 at 10:37am
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.
Oct 29, 2014 at 11:01am
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.
Oct 29, 2014 at 12:14pm
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.
Oct 29, 2014 at 12:35pm
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.
Oct 29, 2014 at 12:39pm
That looks right to me Alphar, is this for a class? What are the requirements?
Oct 29, 2014 at 1:04pm
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:(
Oct 29, 2014 at 1:12pm
closed account (48T7M4Gy)
The code immediately above appears to work OK. What's wrong with it Alphar?
Oct 29, 2014 at 1:16pm
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 Oct 29, 2014 at 1:17pm
Oct 29, 2014 at 1:18pm
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
Oct 29, 2014 at 1:24pm
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 Oct 29, 2014 at 1:25pm
Oct 29, 2014 at 1:36pm
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;
}
Oct 29, 2014 at 2:32pm
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.
Oct 30, 2014 at 6:13am
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
Oct 30, 2014 at 8:45am
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