Closest Number

题目描述:

        There is one Master In ACM_DIY called "白衣少年"(White) whose motto is "I can HOLD ANY FEMALE". Since White is really busy with HOLDING FEMALES, he has no idea of the work that is given by his boss(MALE, of course), so can you help him to solve such a simple and really easy problem: 

        Array A has N positive integers,for each A[i] (0<=i<N, indicating the i-th integer in the array A), it fits in a 32-bit signed integer ),find the closest number less than A[i] (if the distance is the same,we prefer the left one).

        If you can solve this problem, White may give you some "tips"(You know better!)

输入:

        T cases (1<=T<=5)

        For each case, the first line give an integer N(1<= N <= 10^6),then the second line has the array with N integers. (All the integers are guaranteed to fit in 32-bit signed integer)

输出:

        For each case, print one line with N space-seperated integers, where the i-th integer is the number who is less that A[i] and is closest to i if exists, otherwise it is 0. 

样例输入:
3
3
2 1 3
3
2 3 1
4
5 7 3 6
样例输出:
1 0 1
1 2 0
3 5 0 3

注意数组的大小和数组是否越界。

 1 #include<stdio.h>
 2 int a[1000002];
 3 int main()
 4 {
 5     int T, i, j, rlen, llen, n, min;
 6     scanf("%d", &T);
 7     while(T--)
 8     {
 9         scanf("%d", &n);
10         for(i=0; i<n;i++)
11         {
12             scanf("%d", &a[i]);
13             if (i==0)
14                 min=a[i];
15             if (a[i]<min)
16                 min=a[i];
17         }
18         for (i=0;i<n; i++)
19         {
20             if (min == a[i])
21             {
22                 printf("0");
23                 if (i!=n-1)
24                     printf(" ");
25                 else
26                     printf("\n");
27                 continue;
28             }
29             llen=rlen=0;
30             for (j=i+1; j<n; j++)
31             {
32                 if (a[i]>a[j])
33                 {
34                     rlen=j-i;
35                     break;
36                 }
37             }
38             for (j=i-1; j>=0; j--)
39             {
40                 if (a[i] > a[j])
41                 {
42                     llen=i-j;
43                     break;
44                 }
45             }
46             //printf("%d   %d\n", llen, rlen);
47             if (llen == 0 && rlen == 0)
48                 continue;
49             if (llen <= rlen)
50             {
51                 if (llen == 0)
52                     printf("%d", a[i+rlen]);
53                 else
54                     printf("%d", a[i-llen]);
55             }
56             else
57             {
58                 if (rlen == 0)
59                     printf("%d", a[i-llen]);
60                 else
61                     printf("%d", a[i+rlen]);
62             }
63             if (i!=n-1)
64             {
65                 printf(" ");
66             }
67             else
68             {
69                 printf("\n");
70             }
71         }
72     }
73     return 0;
74 }

 

转载于:https://www.cnblogs.com/zrq495/archive/2012/05/26/2519509.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值