2017年校招全国统一模拟笔试(第一场)参考代码 Java版

感谢大神@NotDeep和网友的思路和代码的分享,下面是我总结的Java版链接,仅供参考共勉。

好多鱼

分析

直接暴力枚举然后check就好了

参考code
import java.util.*;
public class Main{

public static void main(String[] args){

Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int min = in.nextInt();
int max = in.nextInt();
int n = in.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int size=in.nextInt();
list.add(size);
}
int ans = 0;
for (int i = min; i <= max; i++) {
boolean flag = true;
for (int j = 0; j < n; j++) {
if ((i >= list.get(j) * 2 && i <= list.get(j) * 10)
|| (i * 2 <= list.get(j) && i * 10 >= list.get(j))) {
flag = false;
}
}
if (flag) {
ans++;
}
}
System.out.println(ans);
}
}
}

循环单词

分析

范围比较小,直接暴力求每个串的最小表示。然后丢进set去重就好了

参考code

import java.util.*;
public class Main{

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int num = sc.nextInt();
sc.nextLine();
Set<String> set = new HashSet<String>();
int count = 0;
for (int i = 0; i < num; i++) {
String a = sc.nextLine();
if (!set.contains(a)) {
count++;
set.add(a);
String b = change(a);
while (!b.equals(a)) {
set.add(b);
b = change(b);
}
}
}
System.out.println(count);
}

}

public static String change(String a) {
String b = a.substring(a.length() - 1) + a.substring(0, a.length() - 1);
return b;
}
}

连续整数

分析

排序之后分别讨论几种可能的情况就OK了

参考code

import java.util.*;
public class Main{

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int n = in.nextInt();
ArrayList<Integer> num = new ArrayList<>();
for (int i = 0; i < n; i++) {
int data = in.nextInt();
num.add(data);
}
Collections.sort(num);
int cnt = 0;
for (int i = 0; i < n - 1; i++) {
if (num.get(i + 1) == num.get(i) + 2) {
cnt++;
}else if (num.get(i) == num.get(i + 1)
|| num.get(i + 1) > num.get(i) + 2) {
cnt++;
}
}
if (cnt > 1) {
System.out.print("mistake");
} else if (cnt == 1) {
for (int i = 0; i < n - 1; i++) {
if (num.get(i + 1) == num.get(i) + 2) {
System.out.print(num.get(i) + 1);
}
}
} else if (cnt == 0) {

if (num.get(0) == 1) {
System.out.print(num.get(n - 1) + 1);
} else {
System.out.print(num.get(0) - 1 + " "
+ (num.get(n - 1) + 1));
}
}
}
}
}


DNA合成

分析

暴力一遍,统计结果即可。

参考code


import java.util.*;
public class Main{

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
char[] s1=in.next().toCharArray();
char[] s2=in.next().toCharArray();
int ans = 0;
for(int i = 0; i < s1.length; i++){
if(s1[i] == 'A' && s2[i] == 'T') ans++;
if(s1[i] == 'T' && s2[i] == 'A') ans++;
if(s1[i] == 'C' && s2[i] == 'G') ans++;
if(s1[i] == 'G' && s2[i] == 'C') ans++;
}
System.out.println(s1.length - ans);
}
}
}

超级素数幂

分析

暴力枚举幂q,然后n开q次幂之后check一下是否相等和p是否是素数。注意一下精度问题就好了

参考code

import java.util.*;
public class Main{

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
long num = sc.nextLong();
double p = 0;
boolean flag = false;
for (long q = 2; q <= (long) Math.sqrt(num); q++) {
p = Math.pow((double) num, 1d / q);
if ((long) p == p && prime((long) p)) {
System.out.println((long) p + " " + q);
flag = true;
break;
}
}
if (!flag) {
System.out.println("No");
}
}
}

public static boolean prime(long n) {
if (n % 2 != 0 || n == 2) {
for (long j = 2; j <= (long) Math.sqrt(n); j++) {
if (n % j == 0 && n != 2)
return false;
}
return true;
}
return false;
}
}

序列和

分析

我们要找连续的一段长度大于等于L小于等于100整数和等于N,容易观察到合法的长度范围很小,于是我们从L开始枚举,然后找到第一个输出即可。

参考code

import java.util.*;
public class Main{

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int S = in.nextInt();
int L = in.nextInt();
Vector<Integer> ans = sequence(S, L);
if (ans.size() == 0) {
System.out.println("No");
} else if (ans.size() > 100) {
System.out.println("No");
} else {
for (int i = 0; i < ans.size(); i++) {
System.out.print(i == 0 ? ans.get(i) : " " + ans.get(i));
}
}
}
}

static Vector<Integer> sequence(int S, int L0) {
Vector<Integer> R = new Vector<>();
for (int L = L0; L <= 100; L++) {
if (S - L * (L - 1) / 2 >= 0 && (S - L * (L - 1) / 2) % L == 0) {
int A = (S - L * (L - 1) / 2) / L;
for (int i = 0; i < L; i++){
R.add(A + i);
}
return R;
}
}
return R;
}
}

页码统计

分析

一个很经典的问题。会的就直接数位dp好了。 不会的看看《编程之美》P132

参考code

import java.util.*;
public class Main{

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int n = in.nextInt();
Vector<Integer> ans = solve(n);
for (int i = 0; i < ans.size(); i++) {
System.out.print(i == 0 ? ans.get(i) : " " + ans.get(i));
}
}
}

static Vector<Integer> solve(int n) {
Vector<Integer> res = new Vector<>(10);
for (int i = 0; i < 10; i++) {
res.addElement(0);
}
if (n == 0) {
return res;
}
if (n % 10 < 9) {
res = solve(n - 1);
while (n != 0) {
res.setElementAt(new Integer(res.get(n % 10).intValue() + 1),
n % 10);
n /= 10;
}
return res;
}
res = solve(n / 10);
for (int i = 0; i < 10; i++) {
if (i > 0) {
res.setElementAt(new Integer(res.get(i).intValue() * 10 + n
/ 10 + 1), i);
} else {
res.setElementAt(new Integer(res.get(i).intValue() * 10 + n
/ 10), i);
}
}
return res;
}
}


01翻转

分析

这个题应该有多种解法。第一眼看起来很BFS但是复杂度大概是O((A+B)*K),应该需要怎么优化才可以。


参考code


import java.util.*;
public class Main{

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int A = in.nextInt();
int B = in.nextInt();
int K = in.nextInt();
int ans = solve(A, A + B, K);
System.out.println(ans);
}
}
static int solve(long  a, long  sum, long  k) {
long  mx = 0, rest, use;
if(a == 0) return 0;
for(int i = 1; i <= 210000; i++) {
mx += k;
if(mx < a) continue;
rest = mx - a;
use = (sum - a) * (i / 2);
use += a * ((i - 1) / 2);
use *= 2;
if(rest % 2 == 0 && rest <= use) return i;
}
return -1;
}
}
#安卓工程师##算法工程师#
全部评论
Java版本的比C++的冗长多了,虽然我用的是Java
点赞 回复 分享
发布于 2017-03-20 13:59
好棒啊!
点赞 回复 分享
发布于 2017-03-11 17:05
顶楼主
点赞 回复 分享
发布于 2017-03-11 17:03

相关推荐

草稿猫编程:查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务