ZZULIOJ 1169: 大整数(指针专题)

题目描述输入3个大整数,位数不超过100位,按从小到大的顺序输出这三个整数。要求定义并使用如下函数比较两个大整数的大小。

int cmp(char *a,char *b)

{

//若大整数a大于b,返回1;

//若a小于b,返回-1;

// 若a与b相等,返回0

}
输入输入有3行,每行输入一个大整数,位数不超过100位,输入不含前导0。
输出输出3行,即排序后的3个大整数。
样例输入1234567890123456789
99999999999999
111111111111111

样例输出99999999999999
111111111111111
1234567890123456789

#include<stdio.h>
#include<string.h>
int bj(char* a,char* b);
int main(){
       char a[110],b[110],c[110],max[110];
       scanf("%s%s%s",a,b,c);
       if(bj(a,b)==1)
       strcpy(max,a),strcpy(a,b),strcpy(b,max);
       if(bj(a,c)==1)
       strcpy(max,a),strcpy(a,c),strcpy(c,max);
       if(bj(b,c)==1)
       strcpy(max,b),strcpy(b,c),strcpy(c,max);
       printf("%s\n%s\n%s\n",a,b,c);
       return 0;
         }
int bj(char* a,char* b){
       int len1=strlen(a),len2=strlen(b);
       if(a[0]=='-'&&b[0]!='-')return -1;   // a<b
       else
       if(a[0]!='-'&&b[0]=='-')return 1;    // a>b
       else
       if(a[0]=='-'&&b[0]=='-'){
       if(len1>len2) return -1;    // a<b
       else if(len2>len1) return 1;   //a>b
       else {
       if(strcmp(a,b)>0) return -1;
       else if(strcmp(a,b)<0) return 1;
       else return 0;
      }
    }
       if(a[0]!='-'&&b[0]!='-'){
       if(len1<len2) return -1;    // a<b
       else if(len2<len1) return 1;   //a>b
       else {
       if(strcmp(a,b)<0) return -1;  //a<b
       else if(strcmp(a,b)>0) return 1; //a>B
       else return 0;
    }
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_43731933/article/details/84668876
今日推荐