[HDU] [2083] Shortest Distance (Simplified Version)

Problem Description

Loading...

Problem Analysis

At first, my approach had a problem
Actually, given the number of points in this problem
a double nested loop is a good choice

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
#include<math.h>
int a[520];
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n;
		scanf("%d",&n);
		for(int i=0; i<n; i++)
			scanf("%d",&a[i]);
		int res=99999999;
		for(int i=0; i<n; i++) {
			int sum=0;
			for(int j=0; j<n; j++)
				sum+=abs(a[j]-a[i]);
			if(res>sum)
				res=sum;
		}
		printf("%d\n",res);
	}
	return 0;
}

Problem link:HDU 2083 Simplified Version of the Shortest Distance

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...