第三届南桥杯Java第八题手机尾号

   30年的改革开放,给中国带来了翻天覆地的变化。2011全年中国手机产量约为11.72亿部。手机已经成为百姓的基本日用品!


    给手机选个好听又好记的号码可能是许多人的心愿。但号源有限,只能辅以有偿选号的方法了。


    这个程序的目的就是:根据给定的手机尾号(4位),按照一定的规则来打分。其规则如下:


    1. 如果出现连号,不管升序还是降序,都加5分。例如:5678,4321都满足加分标准。


    2. 前三个数字相同,或后三个数字相同,都加3分。例如:4888,6665,7777都满足加分的标准。注意:7777因为满足这条标准两次,所以这条规则给它加了6分。


    3. 符合AABB或者ABAB模式的加1分。例如:2255,3939,7777都符合这个模式,所以都被加分。注意:7777因为满足这条标准两次,所以这条标准给它加了2分。


    4. 含有:6,8,9中任何一个数字,每出现一次加1分。例如4326,6875,9918都符合加分标准。其中,6875被加2分;9918被加3分。


    尾号最终得分就是每条标准的加分总和!


    要求程序从标准输入接收数据,在标准输出上输出结果。


    输入格式为:第一行是一个整数n(<100),表示下边有多少输入行,接下来是n行4位一组的数据,就是等待计算加分的手机尾号。
    输出格式为:n行整数。


    例如,输入:
14
3045
0211
2345
6543
7777
8888
7878
7788
6688
2424
2244
9918
6789
8866
    则输出:
0
0
5
6
8
12
3
3
5
1
1
3
8

5



import java.util.ArrayList;
import java.util.Scanner;


public class CheckNum {
	
	
	/**
	 * 检查arr数组是否升序
	 * @param arr
	 * @return
	 */
	public static int checkAsc(int[] arr){
		int[] tempArr = CheckNum.copyArr(arr);
		for(int i=0; i<tempArr.length-1;i++) {
			for(int j=0; j<tempArr.length-1-i; j++) {
				if(tempArr[j] > tempArr[j+1]){
					int temp = tempArr[j];
					tempArr[j] = tempArr[j+1];
					tempArr[j+1] = temp;
				}
			}
		}
		//检查是否是连续的
		boolean flag = CheckNum.checkCountinue(tempArr, 1);
		boolean flag2 = CheckNum.checkEqual(arr, tempArr);
		//System.out.println(flag);
		if(flag == true && flag2 == true) {
			return 5;
		}
		return 0;
	}
	
	/**
	 * 检查arr数组是否降序
	 * @param arr
	 * @return
	 */
	public static int checkDsc(int[] arr){
		int[] tempArr = CheckNum.copyArr(arr);
		for(int i=0; i<tempArr.length-1;i++) {
			for(int j=0; j<tempArr.length-1-i; j++) {
				if(tempArr[j] < tempArr[j+1]){
					int temp = tempArr[j];
					tempArr[j] = tempArr[j+1];
					tempArr[j+1] = temp;
				}
			}
		}
		//检查是否是连续的
		boolean flag = CheckNum.checkCountinue(tempArr, 2);
		boolean flag2 = CheckNum.checkEqual(arr, tempArr);
		//System.out.println(flag);
		if(flag == true && flag2 == true) {
			return 5;
		}
		return 0;
	}
	
	
	
	/**
	 * 检查时候是连续的
	 * @param arr
	 * @return
	 */
	public static boolean checkCountinue(int arr[], int flag){
		 //asc
		if(flag == 1) { 
			for(int i=0; i<arr.length-1; i++){
				if(arr[i] + 1 != arr[i+1]){
					return false;
				}
			}
		}
		
		//dsc
		if(flag == 2) {
			for(int i=0; i<arr.length-1; i++){
				if(arr[i] - 1 != arr[i+1]){
					return false;
				}
			}
		}
		
		return true;
	}
	
	
	
	/**
	 * 检查两个数组是否相同
	 * @param arr1
	 * @param arr2
	 * @return
	 */
	public static boolean checkEqual(int[] arr1, int[] arr2){
		for(int i=0; i<arr1.length; i++) {
			if(arr1[i] != arr2[i]){
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 复制一个数组的内容并返回
	 * @param arr
	 * @return
	 */
	public static int[] copyArr(int[] arr){
		int[] temp = new int[arr.length];
		for(int i=0; i<arr.length; i++){
			temp[i] = arr[i];
		}
		return temp;
	}
	
	
	/**
	 * 
	 * 打印数组的内容
	 * @param arr
	 */
	public static void printArray(int [] arr){
		for(int i=0; i<arr.length; i++){
			System.out.printf(arr[i] + ",");
		}
		System.out.println("");
	}
	
	/**
	 * 符合前三个数字相同,或后三个数字相同,都加3分。例如:4888,6665,7777
	 * @param arr
	 * @return
	 */
	public static int CheckSecond(int[] arr){
		int[] tempArr = CheckNum.copyArr(arr); //3045
		int score = 0;
		if(tempArr[0] == tempArr[1] && tempArr[1]== tempArr[2]){
			score += 3;
		}
		
		if(tempArr[1] == tempArr[2] && tempArr[2]== tempArr[3]){
			score += 3;
		}
		return score;
	}
	
	
	/**
	 * 符合AABB或者ABAB模式的加1分
	 * @param arr
	 * @return
	 */
	public static int CheckThird(int[] arr){
		int[] tempArr = CheckNum.copyArr(arr);
		int score = 0;
		if(tempArr[0]==tempArr[1] && tempArr[2] == tempArr[3]){
			score += 1;
		}
		if(tempArr[0]==tempArr[2] && tempArr[1] == tempArr[3]){
			score += 1;
		}
		return score;
	}
	
	public static int checkFourth(int[] arr){
		int[] tempArr = CheckNum.copyArr(arr);
		int score = 0;
		for(int i=0; i<tempArr.length; i++){
			if(tempArr[i] == 6) {
				score += 1;
			}
		}
		
		for(int i=0; i<tempArr.length; i++){
			if(tempArr[i] == 8) {
				score += 1;
			}
		}
		
		for(int i=0; i<tempArr.length; i++){
			if(tempArr[i] == 9) {
				score += 1;
			}
		}
		
		return score;
	}
	
	public static int[] toArr(String temp){
		int[] arr = new int[temp.length()];
		for(int i=0; i<temp.length(); i++){
			arr[i] = temp.charAt(i) - 48;
			//System.out.println(arr[i]);
		}
		
		
		return arr;
	}
	
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		ArrayList<int[]> al = new ArrayList<int[]>();
		for(int i=0; i<num; i++){
			String temp = sc.next();
			al.add(CheckNum.toArr(temp));
		}
		System.out.println(al.toString());
		System.out.println("输出分数:");
		for(int i=0; i<al.size(); i++){
			int arr[] = al.get(i);
			int score = 0;
			score += CheckNum.checkAsc(arr);
			score += CheckNum.checkDsc(arr);
			score += CheckNum.CheckSecond(arr);
			score += CheckNum.CheckThird(arr);
			score += CheckNum.checkFourth(arr);
			System.out.println(score);
		}
		
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值