asuml

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

问题 Q: 比大小

时间限制: 1 Sec  内存限制: 128 MB 提交: 159  解决: 66 [提交][状态][讨论版]

题目描述

给你两个很大的数,你能不能判断出他们两个数的大小呢? 比如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
 1 #include <iostream>
 2 #include <string.h>
 3 
 4 using namespace std;
 5 
 6 char a[1111],b[1111];
 7 int cut;
 8 int na,nb;
 9 int main()
10 {
11     while(cin>>a>>b)
12     {
13         if(a[0]=='0'&&b[0]=='0')
14             break;
15         if(a[0]!='-'&&b[0]=='-')
16         {
17             cout<<"a>b"<<endl;
18             continue;
19         }
20         if(a[0]=='-'&&b[0]!='-')
21         {
22             cout<<"a<b"<<endl;
23             continue;
24         }
25         cut=0;
26         if(a[0]=='-'&&b[0]=='-')    cut=1;
27         na=strlen(a+cut);
28         nb=strlen(b+cut);
29         if(na>nb&&cut==0)
30         {
31             cout<<"a>b"<<endl;
32             continue;
33         }
34         if(na>nb&&cut==1)
35         {
36             cout<<"a<b"<<endl;
37             continue;
38         }
39         if(na<nb&&cut==0)
40         {
41             cout<<"a<b"<<endl;
42             continue;
43         }
44         if(na<nb&&cut==1)
45         {
46             cout<<"a>b"<<endl;
47             continue;
48         }
49         for(int i=cut;i<na+cut;i++)
50         {
51             if(a[i]>b[i])
52             {
53                 if(cut==0)
54                 {
55                     cout<<"a>b"<<endl;
56                     cut=-1;
57                     break;
58                 }
59                 else
60                 {
61                     cout<<"a<b"<<endl;
62                     cut=-1;
63                     break;
64                 }
65             }
66             if(a[i]<b[i])
67             {
68                 if(cut==0)
69                 {
70                     cout<<"a<b"<<endl;
71                     cut=-1;
72                     break;
73                 }
74                 else
75                 {
76                     cout<<"a>b"<<endl;
77                     cut=-1;
78                     break;
79                 }
80             }
81         }
82         if(cut!=-1)
83             cout<<"a==b"<<endl;
84     }
85     return 0;
86 }

 

posted on 2016-07-15 16:20  asuml  阅读(203)  评论(0编辑  收藏  举报