java 洛谷题单【入门6】函数与结构体

P5735 【深基7.例1】距离函数


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        double x3 = input.nextDouble();
        double y3 = input.nextDouble();
        double a = distance(x1, y1, x2, y2);
        double b = distance(x1, y1, x3, y3);
        double c = distance(x2, y2, x3, y3);
        String sum = String.format("%.2f", a + b + c);
        System.out.println(sum);

    }

    public static double distance(double x1, double y1, double x2, double y2){

        return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    }
}

P5736 【深基7.例2】质数筛

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for (int i = 1; i <= n; i++) {
            int x = input.nextInt();
            if (isPrime(x)){
                System.out.print(x + " ");
            }
        }


    }

    public static boolean isPrime(int num) {
        if (num <= 3) return num > 1;
        if (num % 6 != 1 && num % 6 != 5){
            return false;
        }
        int sqrt = (int) Math.sqrt(num);
        for (int i = 5; i <= sqrt; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0){
                return false;
            }
        }
        return true;
    }
}

 两个解法用到的判断质数的方法都是一样的,只是主函数的调用不同。

 import java.util.Scanner;

 public class Main {

     public static void main(String[] args) {
         Scanner input = new Scanner(System.in);
         int n = input.nextInt();int[] a = new int[n];
         for (int i = 0; i < n; i++) {
             int x = input.nextInt();
             a[i] = x;
         }
         for (int i = 0; i < n; i++) {
             if (isPrime(a[i])) {
                 System.out.print(a[i] + " ");
             }
         }

     }

     public static boolean isPrime(int num) {
         if (num <= 3) return num > 1;
         if (num % 6 != 1 && num % 6 != 5){
             return false;
         }
         int sqrt = (int) Math.sqrt(num);
         for (int i = 5; i <= sqrt; i += 6) {
             if (num % i != 0 || num % (i + 2) == 0){
                 return false;
             }
         }
         return true;
     }
 }

P5737 【深基7.例3】闰年展示


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int y1 = input.nextInt();
        int y2 = input.nextInt();
        isLeapYear(y1, y2);

    }

    public static void isLeapYear(int y1, int y2) {
        int count = 0;int i;int[] a = new int[1000];
        for (i = y1; i <= y2; i++) {
            if (i % 4 == 0 && i % 100!= 0 || i % 400 == 0){
                a[count++] = i;
            }
        }
        System.out.println(count);
        for (int j = 0; j < count; j++) {
            System.out.print(a[j] + " ");
        }

    }
}

P5738 【深基7.例4】歌唱比赛

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

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int m = input.nextInt();
        double max = 0;double sum = 0;
        while (n-- != 0){
            int[] arr = new int[m];
            for (int i = 0; i < m; i++) {
                arr[i] = input.nextInt();
                sum += arr[i];
            }
            Arrays.sort(arr);
            sum -= arr[0] + arr[m - 1];
            sum /= (m - 2);
            if (sum > max){
                max = sum;
            }
            sum = 0;
        }
        System.out.printf("%.2f",max);

    }

}

P5739 【深基7.例7】计算阶乘

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        System.out.println(factorial(n));
    }

    public static int factorial(int x) {
        if (x == 1) return 1;
        return x * factorial(x - 1);
    }

}

P5461 赦免战俘

import java.util.Scanner;

public class Main {
    static int p = 1;
    static int[][] a = new int[1025][1025];

    public static void function(int x, int row, int col){
        if (x == 2){
            a[row][col] = 0;
            return;
        }
        for (int i = row; i <= row + x / 2 - 1; i++) {
            for (int j = col; j <= col + x / 2 - 1; j++) {
                a[i][j] = 0;
            }
        }
        function(x / 2, row + x / 2, col);
        function(x / 2, row + x / 2, col + x / 2);
        function(x / 2, row, col + x / 2);

    }

    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for (int i = 1; i <= n; i++) {
            p *= 2;
        }
        for (int i = 1; i <= p; i++) {
            for (int j = 1; j <= p; j++) {
                a[i][j] = 1;
            }
        }

        function(p, 1, 1);

        for (int i = 1; i <= p; i++) {
            for (int j = 1; j <= p; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }

    }

}

P5740 【深基7.例9】最厉害的学生

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        String[] name = new String[1005];
        int[] ch = new int[1005];int[] ma = new int[1005];int[] en = new int[1005];
        int max = -99999999,t = 0;
        int n = input.nextInt();
        for (int i = 0; i < n; i++) {
            name[i] = input.next();
            ch[i] = input.nextInt();
            ma[i] = input.nextInt();
            en[i] = input.nextInt();
        }
        for (int i = 0; i < n; i++) {
            int sum = ch[i] + ma[i] + en[i];
            if (sum > max){
                max = sum;
                t = i;
            }
        }
        System.out.println(name[t] + " " + ch[t] + " " + ma[t] + " " + en[t]);
    }

}

P5741 【深基7.例10】旗鼓相当的对手 - 加强版

数组

通过对每个数据设定数组处理数据

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();String[] name = new String[1001];
        int[] ch = new int[1001];int[] ma = new int[1001];int[] en = new int[1001];
        int[] sum = new int[1001];
        for (int i = 1; i <= n; i++) {
            name[i] = input.next();
            ch[i] = input.nextInt();
            ma[i] = input.nextInt();
            en[i] = input.nextInt();
            sum[i] = ch[i] + ma[i] + en[i];
        }

        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                if (    Math.abs(ch[i] - ch[j]) <= 5 &&
                        Math.abs(en[i] - en[j]) <= 5 &&
                        Math.abs(ma[i] - ma[j]) <= 5 &&
                        Math.abs(sum[i] - sum[j]) <= 10){
                    System.out.println(name[i] + " " + name[j]);
                }
            }
        }

    }

}

 学生类

设计一个学生类,包括需要输出的变量,调用类方法解决问题

 import java.util.Scanner;

 public class Main {
     public static void main(String[] args) throws Exception {
         Scanner input = new Scanner(System.in);
         int n = input.nextInt();
         int ch = 0, ma = 0, en = 0;
         Student[] a = new Student[n];
         for (int i = 0; i < n; i++) {
             a[i] = new Student(null, ch, ma, en);
             a[i].name = input.next();
             a[i].ch = input.nextInt();
             a[i].ma = input.nextInt();
             a[i].en = input.nextInt();
             a[i].sum = a[i].ch + a[i].ma + a[i].en;
         }
         for (int i = 0; i < n; i++) {
             for (int j = i + 1; j < n; j++) {
                 if ((Math.abs(a[i].ch - a[j].ch) <= 5) &&
                         (Math.abs(a[i].ma - a[j].ma) <= 5) &&
                         (Math.abs(a[i].en - a[j].en) <= 5) &&
                         (Math.abs(a[i].sum - a[j].sum) <= 10)) {
                     System.out.println(a[i].name + " " + a[j].name);
                 }
             }
         }

     }

     public static class Student {
         private String name;
         private int ch;
         private int ma;
         private int en;
         private int sum;
         public Student(String name, int ch, int ma, int en){
             this.name = name;
             this.ch = ch;
             this.ma = ma;
             this.en = en;
             this.sum = ch + ma + en;
         }

     }
 }

P5742 【深基7.例11】评等级

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for (int i = 0; i < n; i++) {
            int num = input.nextInt();
            int grade = input.nextInt();
            int sc = input.nextInt();
            if (grade + sc > 140 && grade * 7 + sc * 3 >= 800){
                System.out.println("Excellent");
            }else System.out.println("Not excellent");
        }
    }

}

P1075 [NOIP2012 普及组] 质因数分解

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for (int i = 2; i <= n; i++) {
            if (n % i == 0) {
                System.out.println(n / i);
                break;
            }
        }
    }
}

P1304 哥德巴赫猜想

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for (int j = 4; j <= n; j += 2) {
            goldbach(j);
        }


    }

    public static void goldbach(int n){
        for (int i = 1; i <= n / 2; i++) {
            if (isPrime(i) && isPrime(n - i)) {
                if (i <= n - i) {
                    System.out.println(n + "=" + i + "+" + (n - i));
                    break;
                }else System.out.println(n + "=" + (n - i) + "+" + i);
            }
        }
    }

    public static boolean isPrime(int num){
        if (num <= 3) return num > 1;
        if (num % 6 != 1 && num % 6 != 5){
            return false;
        }
        int sqrt = (int) Math.sqrt(num);
        for (int i = 5; i <= sqrt; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0){
                return false;
            }
        }
        return true;
    }
}

P1217 [USACO1.5] 回文质数 Prime Palindromes

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        for (int i = a; i <= b; i++) {
            if (isPalindrome(i) && isPrime(i)){
                System.out.println(i);
            }
        }
    }

    public static boolean isPrime(int n){
        if (n <= 3) return n > 1;
        if (n % 6 != 1 && n % 6 != 5)return false;

        int sqrt = (int)Math.sqrt(n);
        for (int i = 5; i <= sqrt; i += 6) {
            if (n % i == 0 || n % (i + 2) == 0){
                return false;
            }
        }
        return true;
    }

    public static boolean isPalindrome(int n){
        if (n < 0 || (n > 0 && n % 10 == 0)) return false;
        int c = 0;
        while (n > c){
            c = c * 10 + n % 10;
            n /= 10;
        }
        return c == n || n == c / 10;
    }
}

P2415 集合求和

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        String inputString = input.nextLine();
        String[] stringArray = inputString.split(" ");
        int[] num = new int[stringArray.length];

        long ans = 0;int n = 0;
        for (int i = 0; i < stringArray.length; i++) {
            num[i] = Integer.parseInt(stringArray[i]);
            ans += num[i];
            n++;
        }

        for (int j = 1; j < n; j++) {
            ans *= 2;
        }

        System.out.println(ans);

    }

}

P5743 【深基7.习8】猴子吃桃

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        System.out.println(func(n));


    }

    public static int func(int day){
        int n = 1;
        for (int i = 1; i < day; i++) {
            n = 2*(n + 1);
        }
        return n;
    }

}

P5744 【深基7.习9】培训

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for (int i = 1; i <= n; i++) {
            String name = input.next();
            int age = input.nextInt();
            int sc = input.nextInt();
            age += 1;
            sc *= 1.2;
            if (sc <= 600) System.out.println(name + " " + age + " " + sc);
            else System.out.println(name + " " + age + " " + 600);
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeShen.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值