A - Three Friends

The meaning of problems: on a coordinate axis, with three points, each movable up time up one position, and the minimum distance to make three points.

Ideas: certainly more to the middle by the number, the smaller the distance between the three.

#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int main(){
        int q,a[5],i;
        while(~scanf("%d",&q)){
                while(q--){
                        for(i=0;i<3;i++){
                                scanf("%d",&a[i]);
                        }
                        sort(a,a+3);
                        if(a[0]==a[1]&&a[2]>a[1]){
                               a[0]++;
                               a[1]++;
                               if(a[1]!=a[2]){
                                a[2]--;
                               }
                        }else if(a[1]==a[2]&&a[1]>a[0]){
                                a[1]--;
                                a[2]--;
                                if(a[0]!=a[1]){
                                        a[0]++;
                                }
                        }else{
                                if(a[0]<a[1])
                                        a[0]++;
                                if(a[2]>a[1])
                                        a[2]--;
                        }
                        printf("%d\n",abs(a[0]-a[1])+abs(a[0]-a[2])+abs(a[1]-a[2]));
                }
        }
}
View Code

Guess you like

Origin www.cnblogs.com/DreamingBetter/p/12189461.html