码蹄杯真题分享


我的个人主页
我的专栏: 人工智能领域、java-数据结构、Javase、C语言,MySQL,希望能帮助到大家!!! 点赞👍收藏❤


1:房间打扫(题目链接


思路:要想要最多的完全干净的行数,则只需要统计行数中字符串相同的最大数

详解代码:

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);  // 创建Scanner对象用于读取输入
        int n = sc.nextInt();  // 读取整数n,表示矩阵的行数(也是列数)
        String[] str = new String[200];  // 创建字符串数组,最多存储200行(题目可能保证n≤200)
        
        // 循环读取n行字符串,每行代表矩阵的一行(由0和1组成)
        for (int i = 0; i < n; i++) {
            str[i] = sc.next();  // 读取一行字符串(如"010"表示该行的三个格子状态)
        }
        
        int num = 0;  // 用于记录最多的完全干净行数
        
        // 遍历每一行,统计相同模式(或互补模式)的出现次数
        for (int i = 0; i < n; i++) {
            if (str[i] == "") {  // 如果该行已经被处理过(标记为空字符串),跳过
                continue;
            }
            int tmp = 1;  // 初始化当前模式的计数(自身算一次)
            
            // 检查后续行是否与当前行模式相同
            for (int j = i + 1; j < n; j++) {
                if (str[i].equals(str[j])) {  // 如果模式相同
                    str[j] = "";  // 标记该行已处理,避免后续重复统计
                    tmp++;  // 计数加1
                }
            }
            num = Math.max(num, tmp);  // 更新最大计数
        }
        
        System.out.println(num);  // 输出最多的完全干净行数
    }
}

解法二使用hashMap去重的方法:

java 复制代码
import java.util.HashMap;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2025-06-02
 * Time:20:34
 */
public class Main {
  public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        HashMap<String,Integer> map=new HashMap<>();
        while(n-->0){
            String str=sc.next();
            map.put(str,map.getOrDefault(str,0)+1);
        }
        int count=0;
        for(int x:map.values()){
            if(count<x){
                count=x;
            }
        }
        System.out.println(count);
    }
}

2:项链(题目链接


思路:主要是先排序,先找最大的减去最小的,在找第二大的减去第二小的,以此类推就行

详解代码:

java 复制代码
package TrueTi6_2;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2025-06-02
 * Time:22:58
 */
public class Main1 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int [] arr=new int[n];
        for(int i=0;i<n;i++){
            arr[i]=sc.nextInt();
        }
        Arrays.sort(arr);
        int[] dp=new int[n+1];
        int l=0,r=n-1,p=0;
        while(l<=r){
            dp[p++]=arr[l];
            dp[p++]=arr[r];
            ++l;
            --r;
        }
        int ans=0;
        dp[n]=arr[0];
        for(int i=0;i<n;i++){
            ans+=Math.abs(dp[i]-dp[i+1]);
        }
        System.out.println(ans);
    }
}

3:白给(题目链接


代码解析:

java 复制代码
package TrueTi6_2;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2025-06-03
 * Time:16:54
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        int y=sc.nextInt();
        int n=sc.nextInt();
        while(x>=1&&y<=1e9&&n-->0){
            if(x>=y){
                int z=x/2;
                x=x-z;
                y=y+z;
            }else{
                int z=y/2;
                x=x+z;
                y=y-z;
            }
        }
        System.out.print(x+" "+y);

    }
}

4:曼哈顿距离矩阵(题目链接


代码解析:

java 复制代码
package TrueTi6_2;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2025-06-03
 * Time:17:30
 */
public class Main3 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        int y=sc.nextInt();
        int n=Math.abs(x)+Math.abs(y);
        int num=0;
        for(int i=1;i<n;i++){
            num+=4*i;
        }
        if(x>0&&y>=0){
            num+=x;
        }else if(x>=0&&y<0){
            num+=n-y;
        }else if(x<0&&y<=0){
            num+=2*n-x;
        }else if(x<=0&&y>0){
            num+=3*n+y;
        }else{
            num=0;
        }
        System.out.println(num);
    }
}

5:白日梦Ⅰ(题目链接


代码解析:

java 复制代码
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2025-06-03
 * Time:18:00
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] arr=new int[n];
        for(int i=0;i<n;i++){
            arr[i]=sc.nextInt();
        }
        double maxRate=arr[0]*1.0;
        double maxProfit=0.0;
        for(int i=1;i<n;i++){
            maxRate=Math.max(maxRate,arr[i-1]);
            maxProfit=Math.max(maxProfit,maxRate/arr[i]);
        }
        System.out.println(String.format("%.2f",Math.max(10.00,10.0*maxProfit)));
    }
}

在这里需要注意的是可以不对换,也就是当n天后小于10美元时候就不进行兑换。

6:甄别情报(题目链接

代码解析:

java 复制代码
package TrueTi6_2;

import java.util.HashMap;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2025-06-03
 * Time:22:14
 */
public class Main5 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int i=0;i<t;i++){
            String x=sc.next();
            String y=sc.next();
            HashMap<Character,Character> map=new HashMap<>();
            boolean correspond=true;
            if(x.length()!=y.length()||x.length()>100||y.length()>100){
                continue;
            }
            for(int j=0;j<x.length();j++) {
                char m = x.charAt(j);
                char n = y.charAt(j);
                if (map.containsKey(m)) {
                    if (map.get(m) != n) {
                        correspond = false;
                    }
                } else if (map.containsValue(n)) {
                    correspond = false;
                } else {
                    map.put(m, n);
                }
            }
                    if(correspond){
                        System.out.println("YES");
                    }else{
                        System.out.println("NO");
                    }
                }
        sc.close();
            }
}

7:水往低处流(题目链接


思路:比较当前格子的左,右,上,下。如果比左小,则count++,同理其他方向也一样。最终我们得到的是被影响才湿润的格子数量。所以最后用所有的格子数量减去count得到的才是最终结果。

代码解析:

java 复制代码
package TrueTi6_4;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2025-06-05
 * Time:23:00
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[][] arr=new int[n][n];
        int count=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                arr[i][j]=sc.nextInt();
            }
        }
        for (int i = 0; i <arr.length; i++) {
            for(int j=0;j< arr.length;j++){
                if(i>=1&&arr[i][j]<arr[i-1][j]){
                    count++;
                }else if(i+1<n&&arr[i][j]<arr[i+1][j]){
                    count++;
                }else if(j>=1&&arr[i][j]<arr[i][j-1]){
                    count++;
                }else if(j+1<n&&arr[i][j]<arr[i][j+1]){
                    count++;
                }
            }
        }
        System.out.println(n*n-count);
    }
}

8:码哥猜想(题目链接


代码解析:

java 复制代码
package TrueTi6_4;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2025-06-06
 * Time:19:05
 */
public class Main1 {
    public static void main(String[] args) {
         for(int i=1;i<=100;i++) {
            int a=i;
             boolean reacheOne=false;
             if(i==1){
                 System.out.print(i+" ");
             }
             while (a!=1) {
                 if (a % 2 == 0) {
                     a = a / 2;
                 } else {
                     a = a * 5 - 1;
                 }
                 if(a>=1e7){
                     break;
                 }
                 if(a==1){
                     reacheOne=true;
                     break;
                 }
             }
             if(reacheOne){
                 System.out.print(i+" ");
             }
         }
    }
}

9:降雨量(题目链接



注意:必须要判断H水位在哪一天是否小于等于0,如若已经小于等于0了,则将H按0进行接下来的天数计算,不然会有五个案例报错

代码解析:

java 复制代码
   

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2025-06-06
 * Time:22:57
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int A=sc.nextInt();
        int B=sc.nextInt();
        int H1=sc.nextInt();
        int H2=sc.nextInt();
        int H=sc.nextInt();
        int n=sc.nextInt();
        int []arr=new int[n];
        for(int i=0;i<n;i++){
            arr[i]=sc.nextInt();
        }
        boolean isFlood=H>H1;
        for(int i=0;i<arr.length;i++){
            if(isFlood){
                H=H+arr[i]-A-B;
            }else{
                H=H+arr[i]-A;
            }
                if(H<=0){
                H=0;
            }
            if(isFlood&&H<=H2){
                isFlood=false;
            }else if(!isFlood&&H>H1){
                isFlood=true;
            }
        }
        System.out.println(H);
    }
}
 

10:子集统计(题目链接


代码解析:

java 复制代码
package TrueTi6_4;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2025-06-07
 * Time:16:12
 */
public class Main3 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String a=sc.next();
        String b=sc.next();
        int length=a.length();
        int number1=Integer.parseInt(a,2);
        int number2=Integer.parseInt(b,2);
        int count=0;
        double pow=Math.pow(2,length);
        for(int i=0;i<pow;i++){
            String s=Integer.toBinaryString(i);
            int a1=Integer.parseInt(s,2);
            int i1=number1&a1;
            int i2=a1&number2;
            if(i1==a1&&i2==number2){
                count++;
            }
        }
        System.out.println(count);
    }
}

这次的码蹄杯真题就分享到这里了;

相关推荐
程序无涯海1 年前
算法篇-二叉树
java·算法·二叉树·笔试··算法题
freephp1 年前
挑战算法题:四数之和
foursum·四数之和·算法题
玥轩_5212 年前
算法竞赛入门【码蹄集新手村600题】(MT1120-1140)C语言
c语言·开发语言·数据结构·算法·蓝桥杯·码蹄杯