- 汽水瓶
有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?
import java.util.Scanner;
/**
* 汽水瓶
* @author muyichun
*
*/
public class Main
{
static int[]arr = new int[10];
static int length = 0;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String num = sc.nextLine();
if (num.equals("0")) break;
else
arr[length++] = Integer.valueOf(num);
}
sc.close();
for (int i = 0; i < length; i++) {
System.out.println(arr[i]/2);
}
}
}
- 明明的随机数
明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤1000),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作(同一个测试用例里可能会有多组数据,希望大家能正确处理)。
Input Param
n 输入随机数的个数
inputArray n个随机整数组成的数组
Return Value
OutputArray 输出处理后的随机整数
注:测试用例保证输入参数的正确性,答题者无需验证。测试用例不止一组。
import java.util.Scanner;
import java.util.TreeSet;
/**
* 明明的随机数
* @author muyichun
*
*/
public class Main
{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
TreeSet<Integer> list = new TreeSet<Integer>();
int count = Integer.valueOf(sc.nextLine());
for (int i = 0; i < count; i++) {
list.add(Integer.valueOf(sc.nextLine()));
}
for (Object i : list.toArray()) {
System.out.println(i);
}
}
sc.close();
}
}
- 十六进制转十进制
写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。(多组同时输入 )
import java.util.Scanner;
import java.util.TreeSet;
/**
* 明明的随机数
* @author muyichun
*
*/
public class Main
{
public static long mul(long num, long count) {
long sum = 1;
for (int i = 0; i < count; i++) {
sum *= num;
}
return sum;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String num = sc.nextLine();
char[]nums = num.toCharArray();
long sum = 0;
for (int i = 2; i < nums.length; i++) {
if (nums[i] == 'A') {
sum += 10 * (mul(16, (nums.length - i - 1)));
}else if (nums[i] == 'B') {
sum += 11 * (mul(16, (nums.length - i - 1)));
}else if (nums[i] == 'C') {
sum += 12 * (mul(16, (nums.length - i - 1)));
}else if (nums[i] == 'D') {
sum += 13 * (mul(16, (nums.length - i - 1)));
}else if (nums[i] == 'E') {
sum += 14 * (mul(16, (nums.length - i - 1)));
}else if (nums[i] == 'F') {
sum += 15 * (mul(16, (nums.length - i - 1)));
}else {
sum += Long.valueOf(String.valueOf(nums[i])) * (mul(16, (nums.length - i - 1)));
}
}
System.out.println(sum);
}
sc.close();
}
}
- 删数
有一个数组a[N]顺序存放0~N-1,要求每隔两个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。以8个数(N=7)为例:{0,1,2,3,4,5,6,7},0->1->2(删除)->3->4->5(删除)->6->7->0(删除),如此循环直到最后一个数被删除。
import java.util.Scanner;
/**
*
* @author muyichun
*
*/
public class Main
{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int num = Integer.valueOf(sc.nextLine());
boolean[]flag = new boolean[num];
int index = 0;
for (int i = 0 ; i < num; i++) {
if (i == num - 1) { // 找出最后剩余一个
for (int j = 0; j < num; j++) {
if (!flag[j]) {
System.out.println(j);
break;
}
}
}else {
int count = 2; // 遍历查找
for (;index < num;) {
if (!flag[index]) {
count--;
if (count < 0) {
flag[index] = true;
if (index+1 == num) index = 0; //回头
else index++;
break;
}
}
if (index+1 == num) index = 0; //回头
else index++;
}
}
}
}
sc.close();
}
// public static void dfs(int start, int end, char[]arr) {
//
// }
//
// public static void swap(int a, int b, char[]arr) {
// char temp = arr[a];
// arr[a] = arr[b];
// arr[b] = temp;
// }
}
- 字符集合
输入一个字符串,求出该字符串包含的字符集合
import java.util.HashSet;
import java.util.Scanner;
/**
* 字符集合
* @author muyichun
*
*/
public class Main
{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
boolean[]flag = new boolean[123];
HashSet<Character> list = new HashSet<Character>();
char[] chars = sc.nextLine().toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
int temp = (int)chars[i];
if (!flag[temp]) {
flag[temp] = true;
sb.append(chars[i]);
}
}
System.out.println(sb.toString());
}
sc.close();
}
// public static void dfs(int start, int end, char[]arr) {
//
// }
//
// public static void swap(int a, int b, char[]arr) {
// char temp = arr[a];
// arr[a] = arr[b];
// arr[b] = temp;
// }
}
-
数独
地址 -
字符串运用-密码拦截
时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M 热度指数:41467
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解。比如进行下列变化 ABBA->12ABBA,ABA->ABAKK,123321->51233214 。因为截获的串太长了,而且存在多种可能的情况(abaaab可看作是aba,或baaab的加密形式),Cathcer的工作量实在是太大了,他只能向电脑高手求助,你能帮Catcher找出最长的有效密码串吗?
输入描述:
输入一个字符串
输出描述:
返回有效密码串的最大长度
示例1:
输入:
ABBA
输出:
4
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* 单词倒排 (DP)
* @author muyichun
*
*/
public class Main
{
public static long mul(long num, long count) {
long sum = 1;
for (int i = 0; i < count; i++) {
sum *= num;
}
return sum;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String sb = String.valueOf(sc.nextLine()).trim();
char[]ch = sb.toCharArray();
int dp[][] = new int[ch.length][2];
dp[0][0] = 1; //dp[i][0] 长度为i+1, 最长偶数回文为:dp[i][0]个
dp[0][1] = 1; //dp[i][1] 长度为i+1, 最长奇数回文为:dp[i][1]个
for (int i = 1; i < ch.length; i++) {
//偶数回文
if (dp[i-1][0] == 1) {
if (ch[i] == ch[i-1]) dp[i][0] = 2;
else dp[i][0] = 1;
}else{
if ( i - dp[i-1][0] - 1 >= 0 && ch[i] == ch[i - dp[i-1][0] -1]) {
dp[i][0] = dp[i-1][0] + 2; //回文数长度+2
}else {
for (int j = 1; j <= i/2; j++) {
boolean flag = true;
for (int q = i - j*2+1; q < i; q++)
if (ch[i] != ch[q]) {
flag = false;
break;
}
if (flag) dp[i][0] += 2;
}
}
}
//奇数回文
if (dp[i-1][1] == 1) {
if (i-2>=0 && ch[i] == ch[i-2]) dp[i][1] = 3;
else dp[i][1] = 1;
}else {
if ( i - dp[i-1][1] - 1 >= 0 && ch[i] == ch[i - dp[i-1][1] -1]) {
dp[i][1] = dp[i-1][1] + 2; //回文数长度+2
}else {
for (int j = 0; j <= i/2; j++) {
if (j == 0) dp[i][1] = 1;
else{
boolean flag = true;
for (int q = i-j*2 ; q < i-1; q++) {
if (ch[q] != ch[i]) {
flag = false; break;
}
}
if (flag) dp[i][1] += 2;
}
}
}
}
}
int sum = 0;
for (int i = 0; i < ch.length; i++) {
for (int j = 0; j < 2; j++) {
if (dp[i][j] > sum) sum = dp[i][j];
}
}
// for (int i = 0; i < ch.length; i++) {
// System.out.print(dp[i][0] + " ");
// }
// System.out.println();
// for (int i = 0; i < ch.length; i++)
// System.out.print(dp[i][1] + " ");
// System.out.println();
System.out.println(sum);
}
sc.close();
}
//判断是否回文;
public static boolean check(String str) {
char[]tmp = str.toCharArray();
for (int i = 0; i < tmp.length /2 ; i++) {
if (tmp[i] != tmp[tmp.length - i - 1]) return false;
}
return true;
}
}
-
Redraiment的走法
题目描述
题目描述Redraiment是走梅花桩的高手。Redraiment总是起点不限,从前到后,往高的桩子走,但走的步数最多,不知道为什么?你能替Redraiment研究他最多走的步数吗?
样例输入
6
2 5 1 5 4 5
样例输出
3
提示
Example:
6个点的高度各为 2 5 1 5 4 5
如从第1格开始走,最多为3步, 2 4 5
从第2格开始走,最多只有1步,5
而从第3格开始走最多有3步,1 4 5
从第5格开始走最多有2步,4 5
所以这个结果是3。
import java.util.Scanner;
/**
* 题目:Redraiment的走法
* @author muyichun
*
*/
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int n = sc.nextInt();
int[]arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
check(arr);
}
sc.close();
}
public static void check(int[]arr) {
int[]dp = new int[arr.length];
dp[0] = 1;
for (int i = 1; i < dp.length; i++) {
int temp = -1;
for (int j = 0; j < i; j++) {
if (arr[i] > arr[j] && temp < dp[j] ) {
temp = dp[j];
}
}
dp[i] = Math.max(1, temp+1);
}
int max = 1;
for (int i = 0; i < dp.length; i++) {
if (dp[i] > max) max = dp[i];
// System.out.print(dp[i]+" ");
}
System.out.println(max);
}
}