目录
#牛客春招刷题训练营# + 【春招神助攻】牛客刷题营开启:每日一题攒牛币,大厂offer不是梦!_牛客网
#牛客春招刷题训练营# + 【春招神助攻】牛客刷题营开启:每日一题攒牛币,大厂offer不是梦!_牛客网
题目地址
1.杨辉三角的变形
我们打个表就不难发现规律
下面是我打的表
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
long n=in.nextLong();
if(n==1||n==2){
System.out.print("-1");
}
else if((n+1)%2==0){
System.out.println("2");
}else if(n%4==0){
System.out.println("3");
}else if((n-2)%4==0){
System.out.println("4");
}
}
}
2.计算日期到天数转换
我这边直接调用的Calendar 类
首先获得 Calendar 单例对象
然后用 set 方法挂载属性值
用 get 方法获取时间 传入的属性是字符串(成员变量)
import java.time.*;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int year = Integer.parseInt(in.next());
int month = Integer.parseInt(in.next()); ;
int day = Integer.parseInt(in.next());
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day);
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println(dayOfYear);
}
}
3.而后单调
首先严格递增的序列不行
其次有两个相邻元素一样的不行
仅供参考
import java.io.*;
import java.time.*;
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static String[] ss;
static String s;
public static void main(String[] args) throws IOException {
int t=sc.nextInt();
while (t-- > 0) {
solve();
}
}
public static void solve() throws IOException {
int n = sc.nextInt();
int m = sc.nextInt();
int nums [] = new int[n];
HashSet<Integer> set = new HashSet<>();
Integer[] tmp = new Integer[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
set.add(nums[i]);
tmp[i] = nums[i];
}
// 不能有重复元素
if(set.size()!=n){
System.out.println("NO");
return;
}
Arrays.sort(tmp);
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < tmp.length; i++) {
map.put(tmp[i],i);
}
for (int i = 0; i < nums.length; i++) {
nums[i] = map.get(nums[i]);
}
int max =0;
int cnt = 1;
for (int i = 1; i < nums.length; i++) {
if(Math.abs(nums[i]-nums[i-1])==1){
cnt++;
}else{
max = Math.max(max, cnt);
cnt=1;
}
}
max = Math.max(max, cnt);
System.out.println(max>=m?"YES":"NO");
}
}