xynuoj 1850 than size

1850: than size

Time Limit:  3 Sec   Memory Limit:  64 MB
[ Commit ][ Status ][ Discussion Board ]

Topic description

Given two large numbers, can you judge the size of the two numbers?

For example, 123456789123456789 is greater than -123456

enter

Each set of test data occupies one line, and input two decimal integers a and b with no more than 1000 digits. Make sure that the input a and b do not have a prefix of 0. If you enter 0 0 means the end of the input. The number of test data sets should not exceed 10 sets

output

If a>b, output "a>b", if a<b, output "a<b", if equal, output "a==b". < div="" style="box-sizing: inherit;">

sample input

111111111111111111111111111 88888888888888888888
-1111111111111111111111111  22222222
0 0

Sample output

a>b
a<b

hint

source

nyoj high precision operation 

One-by-one comparison, written in C, the idea is relatively clear, but it is a bit verbose

#include<stdio.h>
#include<string.h>
int main(){
	char str1[1010],str2[1010];
	while(scanf("%s %s",str1,str2)!=EOF){
		if(str1[0]=='0'&&str2[0]=='0')
		break;
		int len1=strlen(str1);
		int len2=strlen(str2);
		if(str1[0]=='-'&&str2[0]!='-'){
			printf("a<b\n");
		}
		else if(str1[0]!='-'&&str2[0]=='-'){
			printf("a>b\n");
		}
		else{
			int flag=0;
			if(str1[0]!='-'&&str2[0]!='-'){
				if(len1<len2){
					printf("a<b\n");
					continue;
				}
				if(len1>len2){
					printf("a>b\n");
					continue;
				}
				for(int i=0;i<len1&&i<len2;i++){
					if(str1[i]<str2[i]){
						printf("a<b\n");
						flag=1;
						break;
					}
					if(str1[i]>str2[i]){
						printf("a>b");
						flag=1;
						break;
					}
				}
				if(!flag)
				printf("a==b\n");
			}
			if(str1[0]=='-'&&str2[0]=='-'){
				if(len1<len2){
					printf("a>b\n");
					continue;
				}
				if(len1>len2){
					printf("a<b\n");
					continue;
				}
				for(int i=0;i<len1&&i<len2;i++){
					if(str1[i]<str2[i]){
						printf("a>b\n");
						flag=1;
						break;
					}
					if(str1[i]>str2[i]){
						printf("a<b");
						flag=1;
						break;
					}
				}
				if(!flag)
				printf("a==b\n");
			}
		}
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326501517&siteId=291194637