zoukankan      html  css  js  c++  java
  • 【大数比较】NYOJ-73

    比大小

    时间限制: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
     1 //NYOJ-比大小
     2 //大数模板
     3 #include<cstring> 
     4 #include<iomanip> 
     5 #include<algorithm> 
     6 #include<cstdio>
     7 using namespace std; 
     8 #define MAXN 9999
     9 #define MAXSIZE 10
    10 #define DLEN 4
    11 char str1[1002],str2[1002];
    12 class BigNum
    13 { 
    14     private: 
    15         int a[500];    
    16         int len;      
    17     public: 
    18         BigNum(){ len = 1;memset(a,0,sizeof(a)); }  
    19         BigNum(const char*);    
    20         bool operator > (const BigNum & T)const;  
    21 }; 
    22 BigNum::BigNum(const char*s){
    23     int t,k,index,l,i;
    24     memset(a,0,sizeof(a));
    25     l=strlen(s);   
    26     len=l/DLEN;
    27     if(l%DLEN)
    28         len++;
    29     index=0;
    30     for(i=l-1;i>=0;i-=DLEN)
    31     {
    32         t=0;
    33         k=i-DLEN+1;
    34         if(k<0)
    35             k=0;
    36         for(int j=k;j<=i;j++)
    37             t=t*10+s[j]-'0';
    38         a[index++]=t;
    39     }
    40 }
    41 bool BigNum::operator>(const BigNum & T) const
    42 { 
    43     int ln;
    44     if(len > T.len)
    45         return true; 
    46     else if(len == T.len)
    47     { 
    48         ln = len - 1; 
    49         while(a[ln] == T.a[ln] && ln >= 0)
    50             ln--; 
    51         if(ln >= 0 && a[ln] > T.a[ln])
    52             return true; 
    53         else
    54             return false; 
    55     } 
    56     else
    57         return false; 
    58 }
    59     
    60 int main(){
    61     while(~scanf("%s %s",str1,str2)){
    62         if(str1[0]=='0' && str2[0]=='0'){
    63             break;
    64         }
    65         BigNum big1(str1);
    66         BigNum big2(str2);
    67         if(strcmp(str1,str2) == 0){
    68             printf("a==b
    ");
    69         }else if(str1[0] == '-' && str2[0] == '-'){
    70             big1 > big2 ? printf("a<b
    ") : printf("a>b
    ");
    71         }else if(str1[0] == '-' && str2[0] != '-'){
    72             printf("a<b
    ");
    73         }else if(str2[0] == '-' && str1[0] != '-'){
    74             printf("a>b
    ");
    75         }else{
    76             big1 > big2 ? printf("a>b
    ") : printf("a<b
    ");    
    77         }        
    78     }
    79     return 0;
    80 }
     1 //最优解
     2 #include<iostream>
     3 #include<string>
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     string a,b;    
     9     while(cin>>a>>b)
    10     {
    11         if(a=="0"&&b=="0")
    12             return 0;
    13         if(a==b)
    14             cout<<"a==b"<<endl;
    15         else if(a[0]=='-'&&b[0]=='-')
    16             {
    17                 if(a.substr(1,string::npos)>b.substr(1,string::npos)||a.length()>b.length())
    18                     cout<<"a<b"<<endl;
    19                 else cout<<"a>b"<<endl;
    20             }
    21         else if(a>"0"&&b>"0"||a<"0"&&b<"0"&&a.length()>b.length()||a>b)
    22                 cout<<"a>b"<<endl;
    23         else if(a<"0"&&b>"0"&&a.length()>b.length()||a>b)
    24                 cout<<"a<b"<<endl;
    25         
    26     }
    27 }  
  • 相关阅读:
    Vitamio中文API文档(3)—— MediaController
    [活动]hhhchina.net很暴力,还有更好的投票方式吗?
    [anytao.activity]也来拉票,不只为评选
    [活动]Scott,来了
    写在2007,行胜于言
    《你必须知道的.NET》,评价和推荐
    [你必须知道的.NET]第十七回:貌合神离:覆写和重载
    [你必须知道的.NET]第十八回:对象创建始末(上)
    当选2008 Microsoft MVP,从好的开始继续
    [你必须知道的.NET]第十九回:对象创建始末(下)
  • 原文地址:https://www.cnblogs.com/zhengbin/p/4392319.html
Copyright © 2011-2022 走看看