Problem C 比大小

比大小

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

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

比如123456789123456789要大于-123456

输入
每组测试数据占一行,输入两个不超过1000位的10进制整数a,b
数据保证输入的a,b没有前缀的0。
如果输入0 0表示输入结束。测试数据组数不超过10组
输出
如果a>b则输出“a>b”,如果a<b则输出“a<b”,如果相等则输出“a==b”。
样例输入
111111111111111111111111111 88888888888888888888
-1111111111111111111111111  22222222
0 0
样例输出
a>b
a<b
错误记录:一定要给自己在多出几组数据!!!另外看清题目要求也很重要!!!
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
bool ai(char a[],char b[]){
    int t1=strlen(a);
    int t2=strlen(b);
	if(t1<t2) return   true;
    if(t1>t2) return false;
	for(int i =0;i <min(t1,t2);i++)
	    if(a[i]>b[i]) return false;
	    else if(a[i]<b[i])return true;
	    else continue;
    
 	return true;				
}
int main()
{
	char a[1100];
	char b[1100];
	memset(a,'0',sizeof(a));
	memset(b,'0',sizeof(b));
	while(cin >>a>>b){
		
		if(a[0]=='0'&&b[0]=='0')break;
		else if(strcmp(a,b)==0){
		     cout<<"a==b"<<endl;
		}else if(a[0]!='-'&&b[0]!='-'){
			if(ai(a,b)) cout<<"a<b"<<endl;
			else cout<<"a>b"<<endl;
		}else if(a[0]!='-'&&b[0]=='-'){
			cout<<"a>b"<<endl;
		}else if(a[0]=='-'&&b[0]=='-'){
			if(ai(a,b)) cout<<"a>b"<<endl;
			else cout<< "a<b"<<endl;
		}else if(a[0]=='-'&&b[0]!='-'){
			cout<<"a<b"<<endl;
		}
	}
	return 0;
}

优秀代码:
#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;
			
	}
}        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值