HDUOJ into the pit on the fifth day ------- A == B? (2054 title)

Today met with a title meaning pit is to determine whether two numbers are equal, I never thought of if statements directly determine the result. . . . . .
I was wrong
topics are as follows

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

Looking at the case you are not prompted to think very simple? Haha, I think, but still to be dug out of the pit,

  1. The numbers did not give you a range, where one can not get away to a int, (examples of deceptive, he is an integer, is sourly to mislead you)
  2. Numbers still problems, the digital comparator is not necessarily the same type, such as (1.0, 1) the two numbers are equal

Because of the different data types, so we consider using the same character array to store data, then (1.0000000, 1) comparison of two numbers, then we must first simplify it, get rid of some meaningless 0 (1.0000000 remove excess 0: ideas , decimal meaningless, also removed), directly after treatment strcmp (num1, num2) comparing two string (note that 0 is not valid is removed from the end of the start numbers)

code show as below:

#include <stdio.h>
#include <string.h>

char num1[1000];
char num2[1000];

void change0(int a, char num[1000]);

int main() {
	while (scanf("%s %s", num1, num2) != EOF)
	{
		for (int i = 0; num1[i] != '\0'; i++)
		{
			if (num1[i] == '.')
			{
				change0(strlen(num1)-1, num1);
			}
		}
		for (int k = 0; num2[k] != '\0'; k++)
		{
			if (num2[k] == '.')
			{
				change0(strlen(num2)-1, num2);
			}
		}
		getchar();
		if(strcmp(num1, num2))
		{
			printf("NO\n");
		} else 
		{
			printf("YES\n");
		}
	}
	
	return 0;
}

void change0(int a, char num[1000]) {
	for (int j = a; num[j] == '0'; j--)
	{
		if (num[j-1] != '0')
		{
			if (num[j-1] == '.')
			{
				num[j-1] = '\0';
			} else 
			{
				num[j] = '\0';
			}
			break;
		}
	}
}
Published 33 original articles · won praise 44 · views 20000 +

Guess you like

Origin blog.csdn.net/LioTomcat/article/details/105101346