发奖金(搜狐2016研发笔试题)

发奖金(搜狐2016研发笔试题)


题目:公司进行了一次黑客马拉松大赛,全公司一共分为了N个组,每组一个房间排成一排开始比赛,比赛结束后没有公布成绩,但是每个组能够看到自己相邻的两个组里比自己成绩低的组的成绩,比赛结束之后要发奖金,以1w为单位,每个组都至少会发1w的奖金,另外,如果一个组发现自己的奖金没有高于比自己成绩低的组发的奖金,就会不满意,作为比赛的组织方,根据成绩计算出至少需要发多少奖金才能让所有的组满意。 


输入描述:

每组数据先输入N,然后N行输入N个正整数,每个数表示每个组的比赛成绩。


输出描述:

输出至少需要多少w的奖金


示例1


输入


10

20 

32 

12 

32 

45 

11 

21 

31 

41 

33


输出

20





java版本的代码实现:
package cn.cat.test;

import java.util.Arrays;
import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		System.out.println("请输入组数据:");
		Scanner scan = new Scanner(System.in);
		//组数量
		int groupCount = Integer.parseInt(scan.nextLine());
		//组分数集。
		int[] groupScore = new int[groupCount];
		//存入每组的成绩
		for (int i = 0; i < groupCount; i++) {
			groupScore[i] = Integer.parseInt(scan.nextLine());
		}
		scan.close();
		
		int minBounsSum = calculateMinBonus(groupScore);
		System.out.println("至少需要奖金: " + minBounsSum);
		System.out.println("--end--");
	}
	
	
	/** 计算最少的奖金数
	 * @Description: 
	 * @author gwj
	 * @Created 2017年7月31日 上午10:55:59 
	 * @param groupScore
	 * @return
	 */
	private static int calculateMinBonus(int[] groupScore) {
		//groupScore是每组的分数集。
		int len = groupScore.length;
		//每组的奖金集。
		int[] groupBonus = new int[len];
		for (int i = 0; i < len; i++) {
			//如果还未设置奖金,则给予最低的奖金数1w。
			if (groupBonus[i] == 0) {
				groupBonus[i] = 1;
				//如果当前组的分数小于下一个相邻组的分数,则 下组奖金 = 当前组奖金 + 1w。
				while (hasNext(i, len) && groupScore[i + 1] > groupScore [i]) {
					groupBonus[i + 1] = groupBonus[i] + 1;
					//继续往下遍历,找出更多符合条件的记录,给予相应的奖金数。
					i++;
				}
			}
		}
		System.out.println("对应的每组奖金为:" + Arrays.toString(groupBonus));
		int bounsSum = 0;
		for (int bonus : groupBonus) {
			bounsSum += bonus;
		}
		return bounsSum;
	}
	
	
	/** 是否还有后面元素
	 * @Description: 
	 * @author gwj
	 * @Created 2017年7月31日 上午10:58:27 
	 * @param index 当前元素索引
	 * @param len 数组长度
	 * @return
	 */
	private static boolean hasNext(int index, int len) {
		return index < len - 1;
	}
	
	
}



posted @ 2017-09-07 09:45  奕辰杰  阅读(269)  评论(0编辑  收藏  举报