130道基础OJ编程题之: 68\~77

130道基础OJ编程题之: 68~77

@toc


68:BC72 平均身高

平均身高_牛客题霸_牛客网

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

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

       double sum = 0;
       for(int i = 0; i < 5; i++) {
        sum += in.nextDouble();
       }

       double avg = sum / 5;
       System.out.println(String.format("%.2f",avg));
    }
}

69:BC74 HTTP状态码

HTTP状态码_牛客题霸_牛客网

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int state = in.nextInt();

            switch(state) {
                case 200:
                  System.out.println("OK");
                  break;
                case 202:
                  System.out.println("Accepted");
                  break;
                case 400:
                  System.out.println("Bad Request");
                  break;
                case 403:
                  System.out.println("Forbidden");
                  break;
                case 404:
                  System.out.println("Not Found");
                  break;
                case 500:
                  System.out.println("Internal Server Error");
                  break;
                case 502:
                  System.out.println("Bad Gateway");
                  break;

            }
        }
    }
}

70:BC75 数字三角形

数字三角形_牛客题霸_牛客网

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNextInt()) {
            int n = in.nextInt();
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.print(j + " ");
                }
                System.out.println();
            }
        }
    }

}

71:BC76 公务员面试

公务员面试_牛客题霸_牛客网

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

import java.util.Arrays;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int[] arr = new int[7];
            int sum = 0;
            for (int i = 0; i < arr.length; i++) {
                arr[i] = in.nextInt();
                sum += arr[i];
            }

            Arrays.sort(arr); // 排序
            double averScoure = (sum - arr[0] - arr[arr.length - 1]) / 5.0;
            System.out.printf("%.2f\n", averScoure);
        }
    }
}

72:BC77 有序序列插入一个数

有序序列插入一个数_牛客题霸_牛客网

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

import java.util.Arrays;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int arr[] = new int[n+1];
            for(int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
            }

            arr[n] = in.nextInt();
            Arrays.sort(arr);
            for(int num : arr) {
                System.out.print(num + " ");
            }


        }
    }
}

73:BC78 筛选法求素数

筛选法求素数_牛客题霸_牛客网

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    /*
     判断该数是不是素数
    */
    public static int isPrime(int n) {
        for (int i = 2; i < n; i++) {
            if (n % i == 0) {
                return 0;  // 是素数
            }

        }
        return 1;  // 不是素数
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int arr[] = new int[n];
            for(int i = 2; i < n; i++) {
                arr[i] = i;
            }
            int count = 1;
            for(int i = 2; i < arr.length; i++) {
                if(isPrime(arr[i]) == 1) {
                    System.out.printf("%d ",arr[i]);
                } else {
                    count++;
                }
            }

            System.out.printf("\n%d\n",count);

        }
    }
}

74: BC79 图像相似度

图像相似度_牛客题霸_牛客网

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();

        int[][] array = new int[m][n];  // 第一幅图
        int[][] brray = new int[m][n];  // 第二幅图

        // 读取第一幅图的像素
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                array[i][j] = in.nextInt();
            }
        }

        // 读取第二幅图的像素
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                brray[i][j] = in.nextInt();
            }
        }

        // 计算对比两幅图的黑白像素
        double count = 0;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (array[i][j] == brray[i][j]) {
                    count++;
                }
            }
        }

        // System.out.printf("%d\n",count);
        // System.out.printf("%.2f",count/(m*n)*100.0);
        System.out.printf("%.2f", count / (m * n) * 100);

    }
}

75: BC80 登录验证

登录验证_牛客题霸_牛客网

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String name = in.next();
            String password = in.next();

            if("admin".equals(name) && "admin".equals(password)) {
                System.out.println("Login Success!");
            } else {
                System.out.println("Login Fail!");
            }
        }
    }
}

76: BC85 包含数字9的数

包含数字9的数_牛客题霸_牛客网

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = 0;
        for(int i = 0; i <= 2019; i++) {
            int temp = i;

            while(temp > 0) {
                if( temp % 10 == 9) {  // 判断该数是否含有 9 
                    count++;
                    break;
                }

                temp = temp / 10; // 将数值向左移动,去掉,最后一个数值
            }
        }

        System.out.println(count);
    
    }
}

77:BC86 奇偶统计

奇偶统计_牛客题霸_牛客网

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();

        int odd = 0;  // 奇数的个数
        int even = 0; // 偶数的个数

        for (int i = 1; i <= num; i ++) {  // 注意: 这里包括 num 本身
            if(i % 2 == 0) {  // 偶数
                even++;
            } else {
                odd++;
            }
        }

        System.out.printf("%d %d",odd,even);

    }
}

最后:

"在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。"

相关推荐
云浪20 分钟前
Go 并发实战:从零实现一个高并发安全的任务调度器
javascript·后端·go
苏三的开发日记30 分钟前
Docker 启动顺序优化(CentOS / Ubuntu)
后端
高频因子挖掘机2 小时前
同一交易日出现重复 K 线:去重前先确认时间、来源和粒度
后端·github·api
鶴哥只手遮天2 小时前
Spring AI 2.0.1 深度解析-第 14 章 MCP 服务端(上):注解式实现与工具来源
java·人工智能·开源
鶴哥只手遮天2 小时前
Spring AI 2.0.1 深度解析-第 15 章 MCP 服务端(下):三种协议形态、Streamable 传输与状态管理
java·人工智能·开源
鶴哥只手遮天2 小时前
Spring AI 2.0.1 深度解析-第 16 章 向量存储(上):`VectorStore` 接口体系、`BatchingStrategy`
java·人工智能·开源
鶴哥只手遮天2 小时前
Spring AI 2.0.1 深度解析-第 12 章 工具循环的流式形态、Usage 累加与扩展体系
java·人工智能·开源
鶴哥只手遮天2 小时前
Spring AI 2.0.1 深度解析-第 23 章 模型供应商实现:从 `OpenAiSetup` 到 Bedrock
java·人工智能·开源
鶴哥只手遮天3 小时前
Spring AI 2.0.1 深度解析-第 06 章 内置 Advisor 全景:13 个实现逐个剖到源码级
java·人工智能·开源
鶴哥只手遮天3 小时前
Spring AI 2.0.1 深度解析-第 03 章 消息体系与 Prompt:不可变模型、`ChatOptions` 与模板渲染
java·人工智能·开源