网易篇
网易(入门)
WY8下厨房
描述
输入描述:
每个输入包含 1 个测试用例。每个测试用例的第 i 行,表示完成第 i 件料理需要哪些材料,各个材料用空格隔开,输入只包含大写英文字母和空格,输入文件不超过 50 行,每一行不超过 50 个字符。
输出描述:
输出一行一个数字表示完成所有料理需要多少种不同的材料。
示例1
输入:
BUTTER FLOUR
HONEY FLOUR EGG
输出:
4
解题
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s;
HashSet<String> set = new HashSet<>(); // 用hashset不重复特性
while (sc.hasNext()) { // hasNext():读取空格回车就读取
set.add(sc.hasNext()); // 添加到set中
}
System.out.println(set.size());
}
}
WY10分苹果
描述
输入描述:
每个输入包含一个测试用例。每个测试用例的第一行包含一个整数 n(1 <= n <= 100),接下来的一行包含 n 个整数 ai(1 <= ai <= 100)。
输出描述:
输出一行表示最少需要移动多少次可以平分苹果,如果方案不存在则输出 -1。
示例1
输入:
4
7 15 9 5
输出:
3
解题
import java.util.*;
public class Main {
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) { // 获取输入的数据
int cowCount = sc.nextInt(); // 读取输入的牛数量
int[] appleOfCow = new int[cowCount]; // 存储牛对应的苹果数量
int appleCount = 0; // 总的苹果数量
for (int i = 0;i < appleOfCow.length; i++) {
int num = sc.nextInt(); // 读取输入的苹果数量
appleOfCow[i] = num;
appleCount += num; // 顺便累加得到苹果总数
}
// 判断是否可以平分苹果
if (appleCount % cowCount != 0) {
System.out.print(-1);
return;
}
int times = 0; // 记录次数,超出2个就+1,缺少两个就-1
int avgAppleCount = appleCount / cowCount; // 苹果平均数
int need = 0; // 超出苹果个数
for (int num: appleOfCow) {
need = num - avgAppleCount;
if (need % 2 == 1) { // 不能被2整除就不符合转移规定
System.out.print(-1);
return;
}else if (need > 0) { // 超过平均数苹果
// 拿出来的苹果刚好会给到缺少的,故此只需要统计拿出来的次数或者给出去的次数
times += need / 2;
}
}
System.out.print(times);
return;
}
}
}
WY11星际穿越
描述
数据范围:1 ≤ h ≤ 1018
输入描述:
每个输入包含一个测试用例。每个测试用例包含一行一个整数 h (1 <= h <= 1018)。
输出描述:
输出一行一个整数表示结果。
示例1
输入: 10
输出: 2
解题
import java.util.*;
public class Main {
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong(); // 使用long
for (long i = (long) Math.sqrt(n); ; i--) { // 开方
if ((i*i + i) <= n) {
System.out.print(i);
return;
}
}
}
}
WY12藏宝图
描述
输入描述:
每个输入包含一个测试用例。每个测试用例包含两行长度不超过 10 的不包含空格的可见 ASCII 字符串。
输出描述:
输出一行 “Yes” 或者 “No” 表示结果。
示例1
输入:
nowcodecom
ooo
输出:
Yes
解题
import java.util.*;
public class Main {
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine(); // 记录子串
char[] chs = str2.toCharArray(); // 将字串转为字符数组
for (int i = 0; i < chs.length; i++) { // 遍历字符数组
int index = str1.indexOf(chs[i]); // 返回首个字符索引,无则返回-1
if (index == -1) { // 没有则不是字串
System.out.print("No");
return;
}
str1 = str1.substring(index + 1); // 根据题目要求将作废的字符串舍去
}
System.out.print("Yes");
}
}