3月蓝桥集训四周总结(Java解析)
1.受伤的皇后
题目描述
输入输出格式
输入输出样例及样例评测
代码解析
import java.util.Scanner;
//1:无需package
//2: 类名必须Main, 不可修改
public class Main {
static int n;//棋盘大小
static int[]arr=new int[20];//存放所有符合条件的皇马位置,下标为行,值为列
static int count=0;//统计所有的可能
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//在此输入您的代码...
n=sc.nextInt();//输入棋盘大小
dfs(0);//从0开始回溯
System.out.println(count);
sc.close();
}
public static void dfs(int k){
if(k==n){
count++;
}//终止条件
for(int i=0;i<n;i++){//因为每行只能放一个皇后,所以将所有的行枚举
if(check(k,i)){//约束条件,请看check()方法
arr[k]=i;//将符合条件的皇后存放好
dfs(k+1);
}
}
}
public static boolean check(int f,int j){//f,j表示当前的一个皇马
for(int i=0;i<f;i++){//表示和f行之前定好的皇马进行比较
if(arr[i]==j){//判断第一二哥个条件
return false;
}
if(Math.abs(i-f)==Math.abs(arr[i]-j)&&Math.abs(i-f)<=2){//判断第三个条件
return false;
}
}
return true;
}
}
2.完全平方数
题目描述
输入输出格式
样例输入输出及评测用例
代码解析
第一个无脑暴力过60。 第二个的方法看不太懂,我再看看/(ㄒoㄒ)/~~
import java.util.Scanner;
//1:无需package
//2: 类名必须Main, 不可修改
public class 完全平方数 {
static long n;
static long m;
static long t;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//在此输入您的代码...
n=sc.nextLong();
for(long i=1;i<=n;i++){
m=i*n;
t=(long) Math.sqrt(m);
if(m==t*t){
System.out.println(i);
return;
}
}
sc.close();
}
}
import java.util.Scanner;
//1:无需package
//2: 类名必须Main, 不可修改
public class Main {
static long n;
static long m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//在此输入您的代码...
n=sc.nextLong();
for(long i=1;i<=Math.sqrt(n);i++){
if(n%(i*i)==0){
m=n/(i*i);
}
}
System.out.println(m);
sc.close();
}
}
3.求阶乘
题目描述
输入输出格式
样例输入输出及评测用例
代码解析
这道题末尾有多少个0就可以拆出多少个5,比如1×5=5能拆出1个,2×5=10,…5×5=25,看梗的解析才知道计算n!的时候,末尾有多少个0,则看1-n能拆出多少个5,由于拆出5只能是5的倍数,所以直接用while(num>0){ans+=num/5; num/=5; }即可得出5的个数。记得用二分。
import java.util.Scanner;
//1:无需package
//2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//在此输入您的代码...
long k=sc.nextLong();
long l=1;
long r=(long) 9e18;
while(l<r){
long mid=(r+l)/2;
if(get5(mid)>=k){
r=mid;
}else{
l=mid+1;
}
}
if(get5(l)==k){
System.out.println(l);
}else{
System.out.println(-1);
}
sc.close();
}
public static long get5(long num){
long ans=0;
while(num>0){
ans+=num/5;
num/=5;
}
return ans;
}
}
4.长草
题目描述
输入输出格式
输入输出样例
代码解析
bfs模板
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static class node {
int x;
int y;
//int step;
public node(int x, int y) {
this.x = x;
this.y = y;
//this.step = step;
}
}
public static int[] dx = {-1, 1, 0, 0};
public static int[] dy = {0, 0, -1, 1};
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
int n = scan.nextInt();
int m = scan.nextInt();
char[][] arr = new char[n][m];
for (int i = 0; i < n; i++) {
arr[i] = scan.next().toCharArray();
}
int mon = scan.nextInt();
scan.close();
Queue<node> queue = new LinkedList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] == 'g') {
queue.offer(new node(i, j));
}
}
}
while (!queue.isEmpty() && mon > 0) {
int size = queue.size();
while (size > 0) {
node poll = queue.poll();
// if (poll.step == mon) {
// break;
// }
int x = poll.x;
int y = poll.y;
for (int i = 0; i < 4; i++) {
int x1 = x + dx[i];
int y1 = y + dy[i];
// 添加条件 arr[x1][y1] != 'g'减少运行时间
if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < m && arr[x1][y1] != 'g') {
queue.offer(new node(x1, y1));
arr[x1][y1] = 'g';
}
}
size--;
}
mon--;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (j + 1 == m) {
System.out.println(arr[i][j]);
}
else {
System.out.print(arr[i][j]);
}
}
}
}
}
5.旋转
问题描述
输入输出描述
输入输出样例
代码解析
import java.util.Scanner;
public class Main {
static int n, m;
static int[][] a;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
a = new int[m + 1][n + 1];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
a[j][n + 1 - i] = sc.nextInt();
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
sc.close();
}
}
6.全球变暖
题目描述
输入输出描述
样例输入输出
代码解析
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static int N=1010;//范围
static int[][] vis=new int[N][N];//用来看是否已经遍历,没有遍历就为0,遍历就为1
static int flag=0;//用来判断符合条件的岛屿
static int[][] d= {{0,1}, {0,-1}, {1,0}, {-1,0}};//四个方向
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
char[][] map=new char[n][n];
for(int i=0;i<n;i++) {
map[i]=(scan.next()).toCharArray();
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++) {//两层循环遍历海洋岛屿
if(vis[i][j]==0 && map[i][j]=='#') {
flag=0;
dfs(map,i,j);
if(flag==0) {
ans++;
}
}
}
}
System.out.println(ans);
}
public static void dfs(char[][] map,int x,int y) {
vis[x][y]=1;//表示已经遍历
if(map[x][y+1]=='#' && map[x][y-1]=='#' &&
map[x+1][y]=='#' && map[x-1][y]=='#') {
flag=1;//符合条件赋值1
}
for(int i = 0; i < 4; i++){
int nx = x + d[i][0], ny = y + d[i][1];//新的点
if(vis[nx][ny]==0 && map[nx][ny]=='#') {
dfs(map,nx,ny);
}
}
}
}
7.玩具
问题描述
代码解析
import java.io.*;
import java.util.Arrays;
public class 玩具 {
static PrintWriter out =new PrintWriter(System.out);
static BufferedReader ins=new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer in=new StreamTokenizer(ins);
public static void main(String[] args) throws IOException {
in.nextToken();
int N=(int) in.nval;
long[] nums=new long[N];
String[] strings=ins.readLine().split(" ");
for (int i = 0; i < N; i++) {
nums[i]=Long.parseLong(strings[i]);
}
Arrays.sort(nums);
long ans=0;
for (int i = N-1; i >=0 ; i-=2) {
ans+=nums[i];
}
out.println(ans);
out.flush();
}
}
8.储物
问题描述
代码解析
import java.io.*;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(br);
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception {
int mod= (int) (1e9+7);
int n=nextInt();
int m=nextInt();
long []arr=new long[n+1];
long []brr=new long[n+1];
long []crr=new long[n+1];
for (int i=2;i<=n;i++) arr[i]=(nextInt()+arr[i-1])%mod;//前缀和
for (int i=1;i<=n;i++) {
brr[i]=nextInt();//当前位置物品数
crr[i]=(crr[i-1]+brr[i]*arr[i])%mod;//全部送到1号位
brr[i]=(brr[i]+brr[i-1])%mod;//物品数前缀和
}
for (int i=0;i<m;i++){
int x=nextInt(),l=nextInt(),r=nextInt();
long ans = 0;
if(r<=x){//向右传输
ans = ((arr[x]%mod*(brr[r]-brr[l-1])%mod)%mod-(crr[r]-crr[l-1])%mod)%mod;
}else if(l>=x){//向左传输
ans = ((crr[r]-crr[l-1])%mod-(arr[x]%mod*(brr[r]-brr[l-1])%mod)%mod)%mod;
}else{//左边向右,右边向左
ans = ((arr[x]%mod*(brr[x]-brr[l-1])%mod)%mod-(crr[x]-crr[l-1])%mod)%mod;
ans = (ans%mod+mod)%mod;
ans += ((crr[r]-crr[x-1])%mod-(arr[x]%mod*(brr[r]-brr[x-1])%mod)%mod)%mod;
}
pw.println((ans%mod+mod)%mod);
}
pw.flush();
}
public static int nextInt() throws Exception {//int型
st.nextToken();
return (int) st.nval;
}
public static long nextLong() throws Exception {//long型
st.nextToken();
return (long) st.nval;
}
}
9.路标
问题描述
代码解析
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)1e5 + 10, l, n, k;
static int[] a = new int[N];
static int left = 0, right = Integer.MIN_VALUE;
public static boolean check(int x) {
int cnt = k, pre = 0;
for (int i = 1; i <= n; i++) {
if (cnt < 0) return false;
if (a[i] - pre <= x) {
pre = a[i];
continue;
}
pre += x;
i--;
cnt--;
}
return cnt >= 0;
}
public static void main(String[] args) throws Exception {
// input...
String[] lnk = in.readLine().split(" ");
l = Integer.parseInt(lnk[0]);
n = Integer.parseInt(lnk[1]);
k = Integer.parseInt(lnk[2]);
String[] s = in.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(s[i - 1]);
right = Math.max(right, a[i] - a[i - 1]);
}
while (left < right) {
int mid = left + ((right - left) >> 1);
if (check(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
out.println(left);
out.flush();
in.close();
}
}
10.愚人节(娱乐)
牛客
11.最长平衡子符串
问题描述
代码解析
class Solution {
public int findTheLongestBalancedSubstring(String s) {
int len = s.length();
char c[] = s.toCharArray();
int res = 0;
for(int i = 0; i < len; i++){
int start0 = i;
int num0 = 0;
int num1 = 0;
if(c[i] == '1'){
continue;
}
while(start0 < len && c[start0] == '0'){
num0++;
start0++;
}
while(start0 < len && c[start0] == '1'){
num1++;
start0++;
}
res = Math.max(Math.min(num0,num1),res);
i = start0 - 1;
}
return res*2;
}
}
12.代价
问题描述
代码解析
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
long x=sc.nextLong();
int count=0;
if(x==1){
System.out.println(1);
}
for(int i=0;i<100;i++){
if(x%2==0&&x!=2){
x/=2;
}
if(x%2!=0){
x-=1;
count++;
}
if(x==2){
System.out.println(count+1);
break;
}
}
}
}