Problem E: 求最大值和最小值

时间:2022-03-09 15:14:18

Description

求出一些整数中的最大值和最小值。

Input

输入为多行,以EOF结束。每行为一个十进制的数字,全部由0~9组成,每行最长不超过1000个字符。有些数字会以0开头(不影响数值大小的前导0)。

Output

输出为输入中的最大值和最小值,格式见sample。

Sample Input

02010001201223

Sample Output

The maximum value is : 23The minimum value is : 2

HINT

由于输入已经超过64bit整数的数值上限,因此应该用字符串把输入存储下来,进行大小的判断。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  char max[2000];
  char min[2000],temp[2000];
  int flag=1;
  int m,n;
  int low,high;
  while(scanf("%s",temp)!=EOF){
     m=strlen(temp);
     n=0;
     for(n=0;temp[n]=='0'&&n<m;n++);
     m-=n;
     //求出第一非0位置
     if(m==0){
            temp[0]='0';
            temp[1]=0;
            n=0;

     }//如果输入的字符串为全0
     if(flag==1){
        strcpy(max,&temp[n]);
        strcpy(min,&temp[n]);
        low=m;
        high=m;
        flag=0;
     }//第一轮进入后使其变为最大最小字符串均为进入的字符串
     if(m<low){
        low=m;
        strcpy(min,&temp[n]);
     }
     if(m>high){
        high=m;
        strcpy(max,&temp[n]);
     }//长度大于或小于直接交换
     if(m==low){
        if(strcmp(&temp[n],min)<0){
            strcpy(min,&temp[n]);
        }
     }
     if(m==high){
        if(strcmp(&temp[n],max)>0)
             strcpy(max,&temp[n]);
     }

}    printf("The maximum value is : %s\n",max);
     printf("The minimum value is : %s\n",min);

     return 0;
}