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

题目描述
输入3个非负大整数,位数不超过100位,按从小到大的顺序输出这三个整数。要求定义并使用如下函数比较两个大整数的大小。
int cmp(char *a,char *b)
{
//若大整数a大于b,返回1;
//若a小于b,返回-1;
// 若a与b相等,返回0
}
输入
输入有3行,每行输入一个大整数,位数不超过100位,输入不含前导0。
输出
输出3行,即排序后的3个大整数。
样例输入 Copy
1234567890123456789
99999999999999
111111111111111
样例输出 Copy
99999999999999
111111111111111
1234567890123456789

源代码 

#include <iostream>
#include <cstring>
#define gets(S) fgets(S,sizeof(S),stdin)
using namespace std;
const int N = 10000 + 10;
char a[N],b[N],c[N];
int cmp(char *a,char *b)
{
    if(strlen(a) > strlen(b))return 1;
    else if(strlen(a) < strlen(b))return -1;
    else if(strlen(a) == strlen(b))
    {
        for(int i = 0;i < strlen(a);i ++ )
        {
            if(a[i] > b[i])return 1;
            else if(a[i] < b[i])return -1;
            else continue; 
        }
    }
    return 0;
}
char str[5][N];
int main()
{
    cin >> str[1] >> str[2] >> str[3];
    for(int i = 1;i <= 3;i ++ )
    {
        for(int j = i + 1;j <= 3;j ++ )
        {
            if(cmp(str[i],str[j]) == 1)
            {
                char t[N];
                strcpy(t,str[i]);
                strcpy(str[i],str[j]);
                strcpy(str[j],t);
            }
        }
    }
    cout << str[1] << endl << str[2] << endl << str[3] << endl;
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/couchpotatoshy/article/details/126111112
今日推荐