比较大小

比大小

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 2
描述

给你两个很大的数,你能不能判断出他们两个数的大小呢?

比如123456789123456789要大于-123456

输入
每组测试数据占一行,输入两个不超过1000位的10进制整数a,b
数据保证输入的a,b没有前缀的0。
如果输入0 0表示输入结束。测试数据组数不超过100组
输出
如果a>b则输出“a>b”,如果a<b则输出“a<b”,如果相等则输出“a==b”。
样例输入
111111111111111111111111111 88888888888888888888
-1111111111111111111111111  22222222
0 0
样例输出
a>b

a<b

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1, s2;
	;
	while (cin >> s1 >> s2, s1[0] != '0' && s2[0] != '0') {
		if (s1[0] == '-' && s2[0] != '-') {
			cout << "a<b" << endl;
		}
		else if (s2[0] == '-' && s1[0] != '-') {
			cout << "a>b" << endl;
		}
		else {
			if (s1[0] == '-' && s2[0] == '-') { //为负数 
				if (s1.size() > s2.size())
					cout << "a<b" << endl;
				else if (s1.size() < s2.size())
					cout << "a>b" << endl;
				else if (s1 > s2) cout << "a<b" << endl;
				else if (s1 < s2) cout << "a>b" << endl;
				else cout << "a==b" << endl;
			}
			else { //为正数 
				if (s1.size() > s2.size())
					cout << "a>b" << endl;
				else if (s1.size() < s2.size())
					cout << "a<b" << endl;
				else if (s1 > s2) cout << "a>b" << endl;
				else if (s1 < s2) cout << "a<b" << endl;
				else cout << "a==b" << endl;
			}
		}
	}
}

另一种

 
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string a,b;	
	while(cin>>a>>b)
	{
		if(a=="0"&&b=="0")
			return 0;
		if(a==b)
			cout<<"a==b"<<endl;
		else if(a[0]=='-'&&b[0]=='-')
			{
				if(a.substr(1,string::npos)>b.substr(1,string::npos)||a.length()>b.length())
					cout<<"a<b"<<endl;
				else cout<<"a>b"<<endl;
			}
		else if(a>"0"&&b>"0"||a<"0"&&b<"0"&&a.length()>b.length()||a>b)
				cout<<"a>b"<<endl;
		else if(a<"0"&&b>"0"&&a.length()>b.length()||a>b)
				cout<<"a<b"<<endl;
			
	}
}        



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值