第一题:
public class Main {
public static void main(String[] args) {
int res=0;
A:for (int i = 1900; i <= 2020 ; i++) {
for (int j = 2; j < i; j++) {
if(i%j==0) {
continue A;
}
}
res++;
}
System.out.println(res);
}
}
第二题:
public class Main {
public static void main(String[] args) {
int res=80;
System.out.println((char)res);
}
}
第三题:
只有根结点和叶结点的二叉树是结点数最少的n1+n2+n0=n
public class Main {
public static void main(String[] args) {
//n0=n2+1 n2=2020 n0+n1+n2=n 2021+2020+n1=n1+2*n2+1
int n2=2020;
int n0=2021;
System.out.println(n2+n0);
}
}
第四题:
public class Main {
public static void main(String[] args) {
int n=101;
int[] fb=new int[n];
fb[1]=1;
fb[2]=1;
int res=0;
for (int i = 2; i < fb.length; i++) {
fb[i]=(fb[i-1]+fb[i-2])%3;
if(fb[i]==0) {
res++;
}
}
System.out.println(res);
System.out.println(n/4);
}
}
第5题:
public class Main {
public static void main(String[] args) {
//7 9 10 5 8 4 2 1 6 3
// String str=“34052419800101001X”;
String str=“11010120210221999”;
int sum=0;
int[] arr= {7,9,10,5,8,4,2,1,6,3};
for (int i = 0; i < str.length(); i++) {
String s=str.substring(i, i+1);
if(s.equals(“X”)) {
int temp=10;
sum+=temparr[i%10];
}else {
int temp=Integer.parseInt(str.substring(i, i+1));
sum+=temparr[i%10];
}
}
System.out.println(sum);
for (int j = 0; j <= 9; j++) {
int temp=sum+j;
if(temp%11==1) {
System.out.println(j);
}
}
}
}
第6题:
import java.util.Scanner;
public class Main {
/* 问题描述
小蓝在商店买文具。
一支钢笔 x 元,小蓝买了 a 支。
一个笔记本 y 元,小蓝买了 b 本。
请问,小蓝一共需要支付多少钱?
输入格式
输入四行。
第一行包含一个整数 x。
第二行包含一个整数 a。
第三行包含一个整数 y。
第四行包含一个整数 b。
输出格式
输出一个整数,表示答案。
样例输入
5
2
1
6
样例输出
16
样例输入
2
0
2
1
样例输出
2
数据规模和约定
对于所有评测用例,0 <= x, a, y, b <= 100。*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int a=sc.nextInt();
int y=sc.nextInt();
int b=sc.nextInt();
System.out.println(x*a+y*b);
sc.close();
}
}
第7题:
import java.util.Scanner;
public class Main {
/* 问题描述
给定一个单词,请将这个单词的首字母大写,后面的所有字母小写。
输入格式
输入一行包含一个字符串,表示给定的单词。
输出格式
输出变换后的单词。
样例输入
hello
样例输出
Hello
样例输入
WORLD
样例输出
World
样例输入
LanQiao
样例输出
Lanqiao
数据规模和约定
对于所有评测用例,单词的长度不超过 100,单词中只包含大小写英文字母。*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String old=str;
str=str.substring(0,1).toUpperCase();
str+=old.substring(1,old.length()).toLowerCase();
System.out.println(str);
sc.close();
}
}
第8题:
import java.util.Scanner;
public class Main {
/* 问题描述
小Hi的公司经常举办回馈社会的爱心活动。这次小Hi作为志愿者带领社区的孩子们参观了青少年天文馆。他发现孩子们对于摩尔斯电码非常感兴趣。
摩尔斯电码由两种基本的信号组成:短信号"滴"(用字符'.'表示)以及长信号"嗒"(用字符'-'表示)。下图是数字0-9的摩尔斯电码表示,每个数字都由5个字符组成:
.---- ..--- ...-- ....- ..... -.... --... ---.. ----. -----
1 2 3 4 5 6 7 8 9 0
为了让孩子们开心,小Hi决定把每位孩子的生日日期转化为摩尔斯码赠送给他们。例如日期20210101对应的摩尔斯电码是:
..--- ----- ..--- .---- ----- .---- ----- .----
你能写一个程序帮助小Hi吗?
输入格式
第一行是一个整数N,代表参加活动的孩子的人数。(1 <= N <= 100)
以下N行每行一个由0-9组成的字符串,代表一个生日日期。(日期格式:yyyymmdd,日期范围: 20000101至20210101)
输出格式
对于每个生日日期,输出一行表示转化后的摩尔斯码,数字之间用一个空格隔开。
样例输入
2
20161011
20000101
样例输出
..--- ----- .---- -.... .---- ----- .---- .----
..--- ----- ----- ----- ----- .---- ----- .----
*/
static String[] s= {"-----",".----","…—",
“…–”,"…-","…","-…","–…","—…","----."};
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] data=new int[n];
for (int i = 0; i < n; i++) {
data[i]=sc.nextInt();
}
for (int i = 0; i < n; i++) {
op(data[i]);
}
sc.close();
}
private static void op(int n) {
String str=n+"";
String res="";
for(int i=0;i<str.length();i++) {
int c=Integer.valueOf(str.substring(i,i+1));
res+=s[c]+" ";
}
System.out.println(res);
}
}
第9题:
import java.util.Scanner;
public class Main {
/* 问题描述
给定一个序列 S = (s[1], s[2], …, s[n]),
一个位置 p 上的变化度是指以这个位置为中心的相邻的 5 个元素的最大值与最小值的差,
即 s[p-2], s[p-1], s[p], s[p+1], s[p+2] 中最大值与最小值的差。
一个序列总共有 n-4 个变化度,位置 1, 2, n-1, n 没有变化度。
例如,对于下面的序列 (1, 0, 4, 8, 5, 7, 6),
总共有 3, 4, 5 三个位置有变化度,分别为 8, 8, 4。
给定一个序列,请求出这个序列的变化度。
输入格式
输入的第一行包含一个整数 n,表示给定的序列长度。
第二行包含 n 个整数 s[1], s[2], …, s[n],表示给定的序列。
输出格式
输出一行,包含 n-4 个非负整数,分别表示每个位置的变化度。
样例输入
7
1 0 4 8 5 7 6
样例输出
8 8 4
数据规模和约定
对于所有评测用例,5 <= n <= 1000,0 <= s[i] <= 1000000。*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long[] data=new long[n];
for (int i = 0; i < n; i++) {
data[i]=sc.nextLong();
}
for(int i=2;i<n-2;i++) {
long max=Long.MIN_VALUE;
long min=Long.MAX_VALUE;
for (int j = -2; j <= 2; j++) {
if(max<data[i+j]) {
max=data[i+j];
}
if(min>data[i+j]) {
min=data[i+j];
}
}
System.out.print((max-min)+" ");
}
sc.close();
}
}
第10题:
import java.util.Scanner;
public class Main {
/* 问题描述
给定一个序列 (a_1, a_2, ..., a_n),
定义序列中的一个递增三元组是指三个下标 i, j, k
对应的三个元素 a_i, a_j, a_k,这三个元素满足 a_i < a_j < a_k。
例如序列 (1, 1, 4, 3, 2, 4) 有以下 4 个递增三元组:
1. 下标 1, 4, 6 对应的 1, 3, 4;
2. 下标 1, 5, 6 对应的 1, 2, 4;
3. 下标 2, 4, 6 对应的 1, 3, 4;
4. 下标 2, 5, 6 对应的 1, 2, 4。
注意,可能有下标不同但对应数值相同的三元组,他们应当算成不同的三元组。
给定序列,请问序列中一共有多少个不同的递增三元组。
输入格式
输入第一行包含一个整数 n,表示序列的长度。
第二行包含 n 个整数 a_1, a_2, ..., a_n,表示给定的序列。
输出格式
输出一行,包含一个整数,表示序列中的递增三元组数量。
请注意答案可能很大,可能超过 32 位二进制整数的范围,建议使用 64 位二进制整数。
样例输入
6
1 1 4 3 2 4
样例输出
4
数据规模和约定
对于 30% 的评测用例,1 <= n <= 20, 0 <= a_i <= 10。
对于 50% 的评测用例,1 <= n <= 1000, 0 <= a_i <= 100。
对于 80% 的评测用例,1 <= n <= 10000, 0 <= a_i <= 100。
对于所有评测用例,1 <= n <= 100000, 0 <= a_i <= 100。*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long[] data=new long[n];
long[] dpBigBefore=new long[n];
long[] dpSmallAfter=new long[n];
for (int i = 0; i < n; i++) {
data[i]=sc.nextLong();
}
for (int i = n-1; i > 0; i--) {
long cnt=0;
for (int j =0 ; j < i; j++) {
if(data[i]>data[j]) {
cnt++;
}
}
dpBigBefore[i]=cnt;
}
for (int i = 0; i <n;i++) {
long cnt=0;
for (int j =i+1 ; j <n ; j++) {
if(data[i]<data[j]) {
cnt++;
}
}
dpSmallAfter[i]=cnt;
}
long res=0;
for(int i=0;i<data.length;i++) {
res+=dpBigBefore[i]*dpSmallAfter[i];
}
System.out.println(res);
sc.close();
}
}
这题应该会超时
这题考试的时候我没遇到。
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class test1 {
// 问题描述
// 小Hi正在研究一种特殊的栈。这种栈的元素既可以从栈顶出栈,也可以从栈底出栈。(进栈还是只能从栈顶进栈)
// 已知入栈的序列是1~N的一个排列,请你判断出栈序列能否是1, 2, 3, … N?
// 输入格式
// 输入包含多组数据。
// 输入第一行包含一个整数T,代表测试数据的组数。
// 以下每组数据占据2行。
// 第一行包含一个整数N。
// 第二行包含N个整数,整数中由空格隔开。表示入栈序列。
// 输出格式
// 对于每组数据输出YES或者NO,代表出栈序列能否是1, 2, 3, … N。
// 样例输入
// 2
// 5
// 2 4 1 3 5
// 5
// 4 3 1 5 2
// 样例输出
// YES
// NO
// 数据规模和约定
// 对于30%的评测用例,1 <= N <= 10
// 对于80%的评测用例,1 <= N <= 10000
// 对于所有评测用例,1 <= N <= 100000, 1 <= T <= 10。
static String[] pl;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.valueOf(sc.nextLine());
pl=new String[n];
String[][] strs = new String[n][2];
int index = 0;
while (n > 0) {
strs[index][0] = sc.nextLine();
strs[index][1] = sc.nextLine();
index++;
n–;
}
for (int i = 0; i < strs.length; i++) {
int len = Integer.valueOf(strs[i][0]);
pl[i]=op(len, strs[i][1].split(" "));
}
for (int i = 0; i < pl.length; i++) {
System.out.println(pl[i]);
}
sc.close();
}
private static String op(int len, String[] s) {
List<Integer> stack = new LinkedList<Integer>();
int i=1;
for (int j = 0; j < s.length; j++) {
int num=Integer.valueOf(s[j]);
if(stack.isEmpty()) {
if(num==i) {
i++;
}else {
stack.add(num);
}
}else {
i=pop(stack,i);
if(num==i) {
i++;
}else {
stack.add(num);
}
}
}
return i==len+1?"YES":"NO";
}
private static int pop(List<Integer> stack, int i) {
while(!stack.isEmpty()) {
int f=stack.get(0);
int r=stack.get(stack.size()-1);
if(f==i) {
i++;
stack.remove(0);
}else if(r==i) {
i++;
stack.remove(stack.size()-1);
}else {
break;
}
}
return i;
}
}
还有一个子序列的题emmm找不到规律,但暴力方法可以得点分。