码迷,mamicode.com
首页 > 其他好文 > 详细

九度 1349 数字在排序数组中出现的次数

时间:2014-10-05 19:54:58      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   ar   java   for   sp   

 

package com.wangzhu.njupt;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StreamTokenizer;

/**
 * 在已排序的数组中查找数出现的次数
 * 
 * @ClassName: Main1349
 * @Description: TODO
 * @author 王竹
 * @date 2014-10-5 下午5:02:27
 * 
 */
public class Main1349 {

    private static final int LEN = 1000001;
    private static int[] arr = new int[LEN];

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        //System.setIn(new FileInputStream("data.in"));
        StreamTokenizer in = new StreamTokenizer(new BufferedInputStream(
                System.in));
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            int n = (int) in.nval;
            for (int i = 0; i < n; i++) {
                in.nextToken();
                arr[i] = (int) in.nval;
            }
            in.nextToken();
            int t = (int) in.nval;
            for (int i = 0; i < t; i++) {
                in.nextToken();
                int m = (int) in.nval;
                int index = binarySearch(n, m);
                int count = 0;
                if (index != -1) {
                    count++;
                    int j = index - 1;
                    while (j >= 0 && arr[j--] == m) {
                        count++;
                    }
                    j = index + 1;
                    while (j < n && arr[j++] == m) {
                        count++;
                    }
                }
                System.out.println(count);
            }
        }
    }

    /**
     * 二分查找
     * 
     * @param len
     * @param key
     * @return
     */
    private static int binarySearch(int len, int key) {
        int left = 0, right = len - 1;
        while (left <= right) {
            int mid = (left + right) >> 1;
            if (arr[mid] > key) {
                right = mid - 1;
            } else if (arr[mid] < key) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

}

 

九度 1349 数字在排序数组中出现的次数

标签:des   style   blog   color   io   ar   java   for   sp   

原文地址:http://www.cnblogs.com/xiaoxian1369/p/4007170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!