第一题:
public class test1 {
/*
* 小明对数位中含有 2、0、1、9 的数字很感兴趣,在 1 到 40 中这样的数包 括 1、2、9、10 至 32、39 和 40,共 28 个,他们的和是
* 574。 请问,在 1 到 2019 中,所有这样的数的和是多少?
*/
public static void main(String[] args) {
long sum=0;
for(int i=1;i<=2019;i++) {
String num=i+"";
if(num.indexOf("2")!=-1||num.indexOf("0")!=-1||num.indexOf("1")!=-1||num.indexOf("9")!=-1) {
sum+=i;
}
}
System.out.println(sum);
}
}
第二题:
public class test2 {
/*
* B.矩形切割
*
* 【问题描述】
*
* 小明有一些矩形的材料,他要从这些矩形材料中切割出一些正方形。
*
* 当他面对一块矩形材料时,他总是从中间切割一刀,切出一块最大的正方 形,剩下一块矩形,然后再切割剩下的矩形材料,直到全部切为正方形为止。
* 例如,对于一块两边分别为 5 和 3 的材料(记为 5×3),小明会依次切出 3×3、2×2、1×1、1×1 共 4 个正方形。
* 现在小明有一块矩形的材料,两边长分别是 2019 和 324。请问小明最终会 切出多少个正方形?
*/
public static void main(String[] args) {
int width=2019;
int height=324;
int cnt=0;
while(height!=0) {
if(width>height) {
width-=height;
cnt++;
}else {
height-=width;
cnt++;
}
}
System.out.println(cnt);
}
}
第三题:
import java.util.TreeSet;
public class test3 {
/*
* C.不同子串
*
* 【问题描述】
*
* 一个字符串的非空子串是指字符串中长度至少为 1 的连续的一段字符组成 的串。例如,字符串aaab 有非空子串a, b, aa, ab, aaa, aab,
* aaab,一共 7 个。 注意在计算时,只算本质不同的串的个数。 请问,字符串0100110001010001 有多少个不同的非空子串?
*/
static TreeSet<String> set=new TreeSet<String>();
public static void main(String[] args) {
String str="0100110001010001";
op(str);
System.out.println(set.size());
}
private static void op(String str) {
for(int i=1;i<=str.length();i++) {
for(int j=0;j<str.length();j++) {
if(j+i<=str.length()) {
set.add(str.substring(j,j+i));
}
}
}
}
}
第四题:
public class test4 {
/*
* D.质数
*
* 【问题描述】
*
* 我们知道第一个质数是 2、第二个质数是 3、第三个质数是 5……请你计算 第 2019 个质数是多少?
*/
public static void main(String[] args) {
int cnt=0;
A:for(int i=2;;i++) {
for(int j=2;j<i;j++) {
if(i%j==0) {
continue A;
}
}
cnt++;
if(cnt==2019) {
System.out.println(i);
break;
}
}
}
}
第五题:
不会…
第六题:
import java.util.Scanner;
public class test6 {
/*
* F.旋转
*
* 【问题描述】
*
* 图片旋转是对图片最简单的处理方式之一,在本题中,你需要对图片顺时 针旋转 90 度。 我们用一个 n×m 的二维数组来表示一个图片,例如下面给出一个
* 3×4 的 图片的例子:
*/
//1 3 5 7
//9 8 7 6
//3 5 9 7
//3 9 1
//5 8 3
//9 7 5
//7 6 7
//1:0-0---0-2
//3:0-1---1-2
//5:0-2---2-2
//7:0-3---3-2
static int n;
static int m;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
int[][] data=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
data[i][j]=sc.nextInt();
}
}
int[][] res=new int[m][n];
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) { //3:0-0---2-0
res[i][j]=data[n-j-1][i]; //9:0-1---1-0
//1:0-2---0-0
}
}
for (int[] is : res) {
for (int i : is) {
System.out.print(i+" ");
}
System.out.println();
}
sc.close();
}
}
第七题:
import java.util.Scanner;
public class test7 {
/*
* G.外卖店优先级
*
* 【问题描述】
*
* “饱了么”外卖系统中维护着 N 家外卖店,编号 1 ∼ N。每家外卖店都有 一个优先级,初始时 (0 时刻) 优先级都为 0。 每经过 1
* 个时间单位,如果外卖店没有订单,则优先级会减少 1,最低减 到 0;而如果外卖店有订单,则优先级不减反加,每有一单优先级加 2。
* 如果某家外卖店某时刻优先级大于 5,则会被系统加入优先缓存中;如果 优先级小于等于 3,则会被清除出优先缓存。 给定 T 时刻以内的 M
* 条订单信息,请你计算 T 时刻时有多少外卖店在优 先缓存中。
*
* 【输入格式】 第一行包含 3 个整数 N、M 和 T。 以下 M 行每行包含两个整数 ts 和 id,表示 ts 时刻编号 id 的外卖店收到 一个订单。
*
* 【输出格式】
*
* 输出一个整数代表答案。
*
* 【样例输入】
*
* 2 6 6 1 1 5 2 3 1 6 2 2 1 6 2 1 2 3 4 5 6 7
* 【样例输出】
*
* 1
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int t = sc.nextInt();
int[][] data = new int[n][t];
for (int i = 0; i < m; i++) {
int ts = sc.nextInt();
int id = sc.nextInt();
data[id - 1][ts - 1] += 1;
}
int cnt = 0;
for (int i = 0; i < n; i++) {
int sum = 0;// 优先级
boolean cache = false;
for (int j = 0; j < t; j++) {
if (data[i][j] != 0) {
sum = sum + (data[i][j] * 2);
if (sum > 5) {
cache = true;
}
} else {
if (sum > 0) {
sum--;
if(sum<=3) {
cache=false;
}
} else {
sum = 0;
}
}
}
if (cache) {
cnt++;
}
}
System.out.println(cnt);
sc.close();
}
}
第八题:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class test8 {
/*
* H. 人物相关性分析
*
* 【问题描述】
*
* 小明正在分析一本小说中的人物相关性。他想知道在小说中 Alice 和 Bob 有多少次同时出现。 更准确的说,小明定义 Alice 和
* Bob“同时出现”的意思是:在小说文本 中 Alice 和 Bob 之间不超过 K 个字符。 例如以下文本: This is a story about
* Alice and Bob.Alice wants to send aprivate message to Bob. 假设 K = 20,则 Alice
* 和 Bob 同时出现了 2 次,分别是”Alice and Bob” 和”Bob. Alice”。前者 Alice 和 Bob 之间有 5 个字符,后者有
* 2 个字符。 注意: 1. Alice 和 Bob 是大小写敏感的,alice 或 bob 等并不计算在内。 2. Alice 和 Bob
* 应为单独的单词,前后可以有标点符号和空格,但是不能 有字母。例如 Bobbi 並不算出现了 Bob。
*
* 【输入格式】
*
* 第一行包含一个整数 K。 第二行包含一行字符串,只包含大小写字母、标点符号和空格。长度不超 过 1000000。
*
* 【输出格式】
*
* 输出一个整数,表示 Alice 和 Bob 同时出现的次数。
*
* 【样例输入】
*
20
This is a story about Alice and Bob.Alice wants to send aprivate message to Bob.
【样例输出】
2
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
int k=Integer.valueOf(s);
String str=sc.nextLine();
long cnt=f(str,k);
System.out.println(cnt);
sc.close();
}
private static long f(String str, int k) {
List<Integer> alist=find(str,"Alice");
List<Integer> blist=find(str,"Bob");
long cnt=0;
for (Integer a: alist) {
for (Integer b : blist) { //Bob01234567890123456789Alice01234567890123456789Bob
if(b-a<=k+3&&b-a>=-k-5) {//0(b) 23(a) 48(b)
cnt++;
}
}
}
return cnt;
}
private static List<Integer> find(String str,String name) {
List<Integer> list=new ArrayList<Integer>();
int index=str.indexOf(name);
while(index!=-1) {
String before=str.substring(index-1,index);
String after=str.substring(index+name.length(),index+name.length()+1);
if(isWord(before)||isWord(after)) {
continue;
}
list.add(index);
index=str.indexOf(name,index+1);
}
return list;
}
private static boolean isWord(String str) {
char c=str.charAt(0);
if(c>=65&&c<=65+26) {
return true;
}
if(c>=97&&c<=97+26) {
return true;
}
return false;
}
}
第九题:
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class test9 {
/*
* 数学老师给小明出了一道等差数列求和的题目。但是粗心的小明忘记了一 部分的数列,只记得其中 N 个整数。
*
* 现在给出这 N 个整数,小明想知道包含这 N 个整数的最短的等差数列有 几项?
*
* 【输入格式】
*
* 输入的第一行包含一个整数 N。 第二行包含 N 个整数 A1,A2,··· ,AN。(注意 A1 ∼ AN 并不一定是按等差数 列中的顺序给出)
*
* 【输出格式】
*
* 输出一个整数表示答案。
*
* 【样例输入】 5 2 6 4 10 20
*
* 【样例输出】 10
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<Long, Integer> map = new HashMap<>();
int n = sc.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
for (int i = 1; i < n; i++) {
long d = arr[i] - arr[i - 1];
if (map.get(d) != null) {
int value = map.get(d) + 1;
map.replace(d, value);
} else {
map.put(d, 1);
}
}
long cnt=0;
long d=0;
for (Map.Entry<Long, Integer> entry : map.entrySet()) {
long key=entry.getKey();
int value=map.get(key);
if(cnt<value) {
d=key;
cnt=value;
}
}
if(d==0) {
System.out.println(arr.length);
}else {
System.out.println((arr[n-1]-arr[0])/d+1);
}
sc.close();
}
}
第十题:
不会…