n soldiers stand in a circle. For each soldier his height ai is known. A reconnaissance unit can be made of such two neighbouring soldiers, 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.
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 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.
5 10 12 13 15 10
5 1
4 10 20 30 40
1 2
我们输入完数组后,就要比较相邻两个数之差的绝对值,把绝对值最小的两个位置输出来,但是我们还要比较第一个和第n个位置,这就需要特别为它写一行代码,但是,如果我们可以写成a[i+1]%n的形式的话,最后一个数就可以直接与第一个数相比较,这就显得代码比较简单清晰。
int n; scanf("%d",&n); for (int i=0;i<n;i++) scanf("%d",&a[i]); int best=0; for (int i=0;i<n;i++) if (abs(a[i]-a[(i+1)%n])<abs(a[best]-a[(best+1)%n])) 从第一二个数的差的绝对值依次与第 best=i;printf("%d %d\n",best+1,(best+1)%n+1); return 0;}从第一二个数的差的绝对值依次与第i和i+1个进行比较如果第i和i+1个比前面的小,best就变成来那个绝对值最小的位置。