比大小
时间限制:
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; } }
-
每组测试数据占一行,输入两个不超过1000位的10进制整数a,b