2021年度训练联盟热身训练赛第一场

ICPC训练周赛ACFour

  • Some Sum
  • This Ain't Your Grandpa's Checkerboard
  • Pulling Their Weight
  • On Average They're Purple
  • 总结

Some Sum

题目描述:
 Your friend has secretly picked N consecutive positive integers between 1 and 100, and wants you to guess if their sum is even or odd.
 If the sum must be even, output ‘Even’. If the sum must be odd, output ‘Odd’. If the sum could be even or could be odd,output ‘Either’.
https://ac.nowcoder.com/acm/contest/12606/D

输入描述:

The input is a single integer N with 1 ≤ N ≤ 10.

输出描述:

**Output a single word. The word should be 'Even', 'Odd', or 'Either', according to the rules given earlier.**

示例1
输入:
1
输出:
Either

示例2
输入:
2
输出:
Odd

题解:

import java.util.Scanner;
public class Main{
     
    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(n%2 == 1) System.out.println("Either");
        else if(n%4 == 0) System.out.println("Even");
        else System.out.println("Odd");
    }
}

This Ain’t Your Grandpa’s Checkerboard

题目描述:
 You are given an n−by−n grid where each square is colored either black or white. A grid is correct if all of the following conditions are satisfied:

 Every row has the same number of black squares as it has white squares.

 Every column has the same number of black squares as it has white squares.

 No row or column has 3 or more consecutive squares of the same color.

Given a grid, determine whether it is correct.
https://ac.nowcoder.com/acm/contest/12606/J

输入描述:

The first line contains an integer n(2≤n≤24; n is even ) . Each of the next n lines contains a string of length n consisting solely of the characters ‘B’ and ‘W’, representing the colors of the grid squares.

输出描述:

If the grid iscorrect, print the number 1 on a single line. Otherwise, print the number 0 on a single line.

示例1
输入:
4
WBBW
WBWB
BWWB
BWBW

输出:
1

示例2
输入:
4
BWWB
BWBB
WBBW
WBWW

输出:
0

题解:

import java.util.Scanner;
public class Main{
     
    public static void main(String[] args) {
     
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        char[][] c=new char[n][n];
        String s[]=new String[n];
        int cntrow=0;
        int cntcol=0;
        int ans=1;
 
        for(int i=0;i<n;i++){
     
            s[i]=sc.next();
            for(int j=0;j<n;j++){
     
                c[i][j]= s[i].charAt(j);
            }
        }
 
 
        for(int i=0;i<n;i++)
        {
     int now=0;
            for(int j=0;j<n;j++){
     
                if(i==0&&c[i][j]=='W')cntrow++;
                else if(c[i][j]=='W')now++;
 
            }
            if(i!=0&&now!=cntrow){
     
                ans=0;
                break;
            }
        }
        for(int i=0;i<n;i++){
     
            int now=0;
            int j;
            for(j=0;j<n;j++){
     
 
                if(i==0&&c[j][i]=='W')cntcol++;
                else if(c[j][i]=='W')now++;
            }
            if(i!=0&&now!=cntcol){
     
                ans=0;
                break;
            }
        }
        String s1="WWW";
        String s2="BBB";
        for(int i=0;i<n;i++){
     
            if(s[i].contains(s1)||s[i].contains(s2)){
     
                ans=0;
                break;
            }
        }
        for(int i=0;i<n;i++){
     
            String s3=new String();
            for(int j=0;j<n;j++){
     
                s3+=c[j][i];
            }
            if(s3.contains(s1)||s3.contains(s2)){
     
                ans=0;
                break;
            }
        }
 
        if(ans==0) System.out.println(0);
        else System.out.println(1);
 
    }
}

Pulling Their Weight

题目描述:

https://ac.nowcoder.com/acm/contest/12606/F

题解:
 将数据用数组存储,之后升序排序,使用双指针,当两个指针相遇后退出循环。
 最后 特殊情况需要判断,当group1与group2相等时,若 **x[i] == x[j]**即为输出 i j 指针任一个即可,若不相等 x[i]+1, 便是 the smallest one
 若group1与group2不相等时,差值便是 结果。

import java.util.Arrays;
import java.util.Scanner;
 
public class Main{
     
    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] x = new int[n];
        for (int i = 0; i < n; i++) {
     
            x[i] = sc.nextInt();
        }
        Arrays.sort(x);
 
        int front = x[0];
        int rear = x[n-1];
        int i = 0;
        int j = n-1;
        while (i+1 != j) {
     
            if (front < rear) {
     
                front += x[++i];
            } else {
     
                rear += x[--j];
            }
        }
        if (front == rear) {
     
            if (x[i] == x[j])
                System.out.println(x[i]);
            else
                System.out.println(x[i]+1);
        }else{
     
            System.out.println(Math.abs(front-rear));
        }
    }
}

On Average They’re Purple

题目描述:

https://ac.nowcoder.com/acm/contest/12606/H

题解:
比赛时,题目理解了,最后求最短生成树时,代码写不出来,最终未能AC,结束后参考大佬代码,完成迪杰斯特拉算法复习,本题为该算法拓展。

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main{
     
    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();

        // 创建集合数组对象,数组下标对应 node, 集合对象存储的是:点与点的关系
        ArrayList<Integer>[] graph = new ArrayList[n+1];
        // 初始化集合数组的每一个对象
        for (int i = 0; i <= n; i++) {
     
            graph[i] = new ArrayList<>();
        }

        // 获取键盘输入
        int from, to;
        for (int i = 0; i < m; i++) {
     
            from = sc.nextInt();
            to = sc.nextInt();
            // 无向图需要双向添加关系
            graph[from].add(to);
            graph[to].add(from);
        }

        // 创建访问数组,默认是 0代表该下标对应结点未访问。
        int[] visited = new int[n+1];
        // 创建距离数组,下标代表 1到 index 的距离
        int[] dis = new int[n+1];

        // 使用广度优先遍历,队列先进先出
        Queue<Integer> queue = new LinkedList<>();

        // 将首结点添加,并做标记
        queue.offer(1);
        visited[1] = 1;

        while (!queue.isEmpty()) {
     
            int node = queue.poll();
            for (int next : graph[node]) {
     
                // 如果当前结点的下一个连接点,访问过就跳过
                if (visited[next] == 1) continue;
                dis[next] = dis[node] + 1;
                visited[next] = 1;
                queue.offer(next);
            }
        }
        System.out.println(dis[n]-1);
    }
}

总结

 算法长时间不练,容易忘,另外运用不熟练,需要更加努力,和队友一起成功AC的感觉真的很爽!加油!

你可能感兴趣的:(Algorithm,算法)