Reconnaissance 2
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 34A
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|
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
Sample Input
Input
5 10 12 13 15 10
Output
5 1
Input
4 10 20 30 40
Output
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;
}
标签:cha,34A,int,CodeForces,ii,jj,Input,Reconnaissance,include
From: https://blog.51cto.com/u_14834528/6210671