Description
n soldiers stand in a circle. For each soldier his height ai is known. A reconnaissance unit can be made of such two neighbouringsoldiers, whose heights difference is minimal, i.e. |ai - aj| is minimal. So each of them will be less noticeable with the other. Output any pair of soldiers that can form a reconnaissance unit.
Input
The first line contains integer n (2 ≤ n ≤ 100) — amount of soldiers. Then follow the heights of the soldiers in their order in the circle — n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1000). The soldier heights are given in clockwise or counterclockwise direction.
Output
Output two integers — indexes of neighbouring soldiers, who should form a reconnaissance unit. If there are many optimum solutions, output any of them. Remember, that the soldiers stand in a circle.
Sample Input
5 10 12 13 15 10
5 1
4 10 20 30 40
1 2
大体题意:n个人围成一个圈,下面分别是n个人的身高,求这个圈里面的相邻的两个人身高差最小的编号是那两个?其中编号按输入数据的顺序排列,第一个是1,最后一个是n
AC代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> int main() { int n; while(scanf("%d",&n)!=EOF) { int a[2101]; int ii,jj; int cha; int min = 100000; for(int i=1; i<=n; i++) { scanf("%d",&a[i]); if(i!=1) { cha = fabs(a[i] - a[i-1]); if(min > cha) { min = cha; ii = i-1; jj = i; } } } cha = fabs(a[1] - a[n]); if(min > cha) { min = cha; ii = n; jj = 1; } printf("%d %d\n",ii,jj); } return 0; }