YTU.2782: 用数字造数字

2782: 用数字造数字

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 206   Solved: 174
[ Submit][ Status][ Web Board]

Description

输入一个3位以上的整数,求其中最大的数字最小的数字之间的差。例如:输入8729,输出7(即9-2=7),再如,输入24825,输出6(即8-2=6)。

Input

一个3位以上的整数

Output

输入整数的最大的数字最小的数字之间的差。

Sample Input

8729

Sample Output

7

HINT

可以在分离各数字过程中找最大、最小数字,也可以先将分离好的数字存储在数组中,再从数组中找出最大最小值。

AC代码:

#include 
#include
#include
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    int a[20];
    int i=0;
    while(n>0)
    {
        a[i]=n%10;
        n=n/10;
        i++;
    }
    int j;
    sort(a,a+i); //表示对a[0] a[1] a[2] ... a[i-1] 排序
    printf("%d\n",a[i-1]-a[0]);
    return 0;
}

多运用C++里的库函数,可以很方便的解决一些问题。

你可能感兴趣的:(YTU.2782: 用数字造数字)