Time limit : 2sec / Memory limit : 1024MB
Score : 100 points
Problem Statement
You have three tasks, all of which need to be completed.
First, you can complete any one task at cost 0.
Then, just after completing the i-th task, you can complete the j-th task at cost |Aj−Ai|.
Here, |x| denotes the absolute value of x.
Find the minimum total cost required to complete all the task.
Constraints
- All values in input are integers.
- 1≤A1,A2,A3≤100
Input
Input is given from Standard Input in the following format:
A1 A2 A3
Output
Print the minimum total cost required to complete all the task.
Sample Input 1
Copy
1 6 3
Sample Output 1
Copy
5
When the tasks are completed in the following order, the total cost will be 5, which is the minimum:
- Complete the first task at cost 0.
- Complete the third task at cost 2.
- Complete the second task at cost 3.
Sample Input 2
Copy
11 5 5
Sample Output 2
Copy
6
Sample Input 3
Copy
100 100 100
Sample Output 3
Copy
0
看着题目会觉得很复杂,其实不然,只要找到输入的三个数中的最大值和最小值,输出最大值减去最小值的差就行。我的做法是先将输入的三个数存放到a、b、c中,然后将a、b、c三个数排序,a最小,c最大,最后输出c-a的值就行了。
#include<stdio.h>
int main()
{
int a,b,c,t;
scanf("%d%d%d",&a,&b,&c);
if(a>b){t=a;a=b;b=t;}
if(b>c){t=b;b=c;c=t;}
if(a>b){t=a;a=b;b=t;}
printf("%d\n",c-a);
}