牛客网 - vivo2020届春季

牛客网 - vivo2020届春季

1、[编程题]手机屏幕解锁模式

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M

现有一个 3x3 规格的 Android 智能手机锁屏程序和两个正整数 m 和 n ,请计算出使用最少m 个键和最多 n个键可以解锁该屏幕的所有有效模式总数。
其中有效模式是指:
1、每个模式必须连接至少m个键和最多n个键;
2、所有的键都必须是不同的;
3、如果在模式中连接两个连续键的行通过任何其他键,则其他键必须在模式中选择,不允许跳过非选择键(如图);
4、顺序相关,单键有效(这里可能跟部分手机不同)。

输入:m,n
代表允许解锁的最少m个键和最多n个键
输出:
满足m和n个键数的所有有效模式的总数

输入例子1:

1,2

输出例子1:

65

例子说明1:

输入m=1,n=2,表示最少1个键,最多2个键,符合要求的键数是1个键和2个键,其中1个键的有效模式有9种,两个键的有效模式有56种,所以最终有效模式总数是9+56=65种,最终输出65。

答案:

public static int solution (int m, int n) {
//		 递归实现
		 int count = 0;
		 n = n>9 ? 9 : n;
		 for(int i=m;i<=n;i++) {
			 boolean[][] flags = new boolean[3][3];
			 for(int j=0;j<3;j++) {
				 for(int t=0;t<3;t++) {
					 count += findNext(flags, j, t, 0, i);
				 }
			 }
		 }
		 return count;
	 }
	 
	 public static int findNext(boolean[][] flags,int curRow, int curCol, int steps, int n) {
		 int count = 0;
		 steps ++;
		 flags[curRow][curCol] = true;
//		 步数走完返回
		 if(steps == n)
			 return 1;
//		 如果可以走,那么返回 1
		 for(int i=0;i<3;i++) {
			 for(int j=0;j<3;j++) {
				 if(flags[i][j] == false && canStep(flags, curRow, curCol, i, j)) {
					 count += findNext(flags, i, j, steps, n);
//					 恢复状态
					 flags[i][j] = false;
				 }
			 }
		 }
		 flags[curRow][curCol] = false;
		 return count;
	 }
	 
	 public static boolean canStep(boolean[][] flags, int curRow, int curCol, int targetRow, int targetCol) {
//		 在同一行
		 if(curRow == targetRow) {
			 int low = curCol < targetCol?curCol:targetCol;
			 if(Math.abs(curCol - targetCol) >1 && flags[curRow][low+1] == false)
				 return false;
		 }
//		 在同一列
		 if(curCol == targetCol) {
			 int low = curRow < targetRow?curRow:targetRow;
			 if(Math.abs(curRow - targetRow) >1 && flags[low+1][curCol] == false)
				 return false;
		 }
//		 斜对角
		 if(Math.abs(curRow-targetRow)==2 && Math.abs(curCol-targetCol)==2 && flags[1][1] == false)
			 return false;
		 return true;
	 }

2、[编程题]数位之积

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M

现给定任意正整数 n,请寻找并输出最小的正整数 m(m>9),使得 m 的各位(个位、十位、百位 ... ...)之乘积等于n,若不存在则输出 -1。

输入例子1:

36

输出例子1:

49

输入例子2:

100

输出例子2:

455

答案:

	    public static int solution (int n) {
	        // write code here
	    	List<Integer> list = new ArrayList<>();
//	    	先因数分解
	    	int m = n;
	    	for(int i=2;i<m/2;i++) {
	    		if(m%i == 0) {
	    			list.add(i);
	    			m /= i;
	    			i--;
	    		}
	    	}
	    	if(m!=n)
	    		list.add(m);
	    	if(m > 9||list.size() ==0)
	    		return -1;
//	    	将List中的数乘起来小于两位数的尽量乘起来
//	    	从后往前!
	    	m = list.get(list.size()-1);
	    	for(int i=list.size()-2;i>=0;i--) {
	    		if(m * list.get(i) > 9) {
	    			m = list.get(i);
	    			continue;
	    		}
	    		m *= list.get(i);
	    		list.set(i, m);
	    		list.remove(i+1);
	    		i++;
	    	}
	    	
//	    	因数组合成最小的数
	    	Collections.sort(list);
	    	int res = list.get(0);
	    	for(int i=1;i<list.size();i++) {
	    		res = res * 10 + list.get(i);
	    	}
	    	return res;
	    }

3、[编程题]vivo智能手机产能

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M

在vivo产线上,每位职工随着对手机加工流程认识的熟悉和经验的增加,日产量也会不断攀升。
假设第一天量产1台,接下来2天(即第二、三天)每天量产2件,接下来3天(即第四、五、六天)每天量产3件 ... ...
以此类推,请编程计算出第n天总共可以量产的手机数量。

输入例子1:

36

输出例子1:

49

例子说明1:

第11天工人总共可以量产的手机数量

答案:

	 public static int solution (int n) {
	        // write code here
		 int[] a = new int[n];
		 int day = 1;
		 int per_day = 1;
		 int day_all = 0;
		 Outter:while(day <= n) {
			 for(int i = 0;i<per_day;i++) {
				 if(day > n)
					 break Outter;
				 day_all += per_day;
				 day++;
			 }
			 per_day++;
		 }
		 return day_all;
	 }
posted @ 2020-04-24 11:48  LewisYoung  阅读(474)  评论(0编辑  收藏  举报