洛谷函数与结构体题单刷题记录 java

洛谷答题链接

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

题目描述
给出平面坐标上不在一条直线上三个点坐标 ( x 1 , y 1 ) , ( x 2 , y 2 ) , ( x 3 , y 3 ) (x_1,y_1),(x_2,y_2),(x_3,y_3) (x1,y1),(x2,y2),(x3,y3),坐标值是实数,且绝对值不超过 100.00,求围成的三角形周长。保留两位小数。

对于平面上的两个点 ( x 1 , y 1 ) , ( x 2 , y 2 ) (x_1,y_1),(x_2,y_2) (x1,y1),(x2,y2),则这两个点之间的距离 d i s = ( x 2 − x 1 ) 2 + ( y 2 − y 1 ) 2 dis=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2} dis=(x2x1)2+(y2y1)2

输入格式
输入三行,第 i i i 行表示坐标 ( x i , y i ) (x_i,y_i) (xi,yi),以一个空格隔开。

输出格式
输出一个两位小数,表示由这三个坐标围成的三角形的周长。

输入输出样例
输入

0 0
0 3
4 0

输出

12.00

提示
数据保证,坐标均为实数且绝对值不超过 100 100 100,小数点后最多仅有 3 3 3 位。

已通过代码

import java.util.*;
import java.lang.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double[][] a = new double[3][2];
        double ans = 0;
        for(int i = 0; i < 3; i++){
            a[i][0] = sc.nextDouble();
            a[i][1] = sc.nextDouble();
        }
        ans += dist(a[0][0], a[0][1], a[1][0], a[1][1]);
        ans += dist(a[1][0], a[1][1], a[2][0], a[2][1]);
        ans += dist(a[2][0], a[2][1], a[0][0], a[0][1]);
        System.out.printf("%.2f", ans);
    }
    public static double dist (double x1, double y1, double x2, double y2) {
        return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    }
}

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

题目描述
输入 n n n 个不大于 1 0 5 10^5 105 的正整数。要求全部储存在数组中,去除掉不是质数的数字,依次输出剩余的质数。

输入格式
第一行输入一个正整数 n n n,表示整数个数。
第二行输入 n n n 个正整数 a i a_i ai,以空格隔开。

输出格式
输出一行,依次输出 a i a_i ai 中剩余的质数,以空格隔开。

输入输出样例
输入

5
3 4 5 6 7

输出

3 5 7

提示
数据保证, 1 ≤ n ≤ 100 1\le n\le100 1n100 1 ≤ a i ≤ 1 0 5 1 \leq a_i \leq 10^5 1ai105

已通过代码

import java.util.*;
import java.lang.*;

public class Main {
    static boolean[] isPrime = new boolean[100005]; //用来标记质素和合数 false=为质数 true为合数

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

    // 欧拉筛是根据数的最小质因数来进行筛除
    // 除0和1以外的合数可以表达为最小质因子 * 某i值,而这两数都一定是小于该合数
    // 所以当遍历到这个合数的时候,这个合数已经在前面被筛掉了
    public static void prime(int n) {
        int cnt = 0;//保存质数的个数
        int[] prime = new int[n];//用来存放质数

        isPrime[0] = true;//筛除0
        isPrime[1] = true;//筛除1

        for (int i = 2; i < n; i++) {
            if (!isPrime[i]) { //如果是质数
                prime[++cnt] = i;
            }
            for (int j = 1; j <= cnt && prime[j] * i <= n; j++) {//遍历已有的质数
                isPrime[i * prime[j]] = true;//筛除合数
                if (i % prime[j] == 0) {
                    break;
                }
                // 用上述if语句可以确保每个数都只被筛选一次
                // 因为如果这里不break,接下来被判定的数将会是i * prime[j+1],
                // 已知i % prime[j] == 0,即i = m * prime[i],
                // 所以接下来被判定的数可表达为m * prime[j] * prime[j+1]
                // 可以明显看出,接下来被判定的数的最小质因数为prime[j],它应该被prime[j]筛除
                // 这里继续判定,则会让它在prime[j+1]时又被筛除一次,不符合欧拉筛根据数的最小质因数筛除的原则。
            }
        }
    }
}


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

题目描述
输入 x , y x,y x,y,输出 [ x , y ] [x,y] [x,y] 区间中闰年个数,并在下一行输出所有闰年年份数字,使用空格隔开。

输入格式
输入两个正整数 x , y x,y x,y,以空格隔开。

输出格式
第一行输出一个正整数,表示 [ x , y ] [x,y] [x,y] 区间中闰年个数。

第二行输出若干个正整数,按照年份单调递增的顺序输出所有闰年年份数字。

输入输出样例
输入

1989 2001

输出

3
1992 1996 2000

提示
数据保证, 1582 ≤ x < y ≤ 3000 1582\le x < y \le 3000 1582x<y3000

已通过代码

import java.util.*;
import java.lang.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        int count = 0;
        int[] years = new int[1500];
        for (int i = x; i <= y; i++) {
            if (leapYear(i)) {
                years[count++] = i;
            }
        }
        System.out.println(count);
        for (int i = 0; i < count; i++) {
            System.out.print(years[i] + " ");
        }
    }

    public static boolean leapYear(int year) {
        if ((year % 100 != 0 && year % 4 == 0) || year % 400 == 0) {
            return true;
        }
        return false;
    }
}

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

题目描述
n ( n ≤ 100 ) n(n\le 100) n(n100) 名同学参加歌唱比赛,并接受 m ( m ≤ 20 ) m(m\le 20) m(m20) 名评委的评分,评分范围是 0 0 0 10 10 10 分。这名同学的得分就是这些评委给分中去掉一个最高分,去掉一个最低分,剩下 m − 2 m-2 m2 个评分的平均数。请问得分最高的同学分数是多少?评分保留 2 2 2 位小数。

输入格式
第一行两个整数 n , m n,m n,m
接下来 n n n 行,每行各 m m m 个整数,表示得分。

输出格式
输出分数最高的同学的分数,保留两位小数。

输入输出样例
输入

7 6
4 7 2 6 10 7
0 5 0 10 3 10
2 6 8 4 3 6
6 3 6 7 5 8
5 9 3 3 8 1
5 9 9 3 2 0
5 8 0 4 1 10

输出

6.00

已通过代码

import java.util.*;
import java.lang.*;

public class Main {
    static int[] score = new int[20];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        double max = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                score[j] = sc.nextInt();
            }
            Arrays.sort(score, 0, m);
            double avrg = average(m);
            if (max < avrg) {
                max = avrg;
            }
        }
        System.out.printf("%.2f", max);
    }

    public static double average(int m) {
        int sum = 0;
        for (int i = 1; i < m - 1; i++) {
            sum += score[i];
        }
        return 1.0 * sum / (m - 2);
    }
}

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

题目描述
n ! n! n!,也就是 1 × 2 × 3 ⋯ × n 1\times2\times3\dots\times n 1×2×3×n

挑战:尝试不使用循环语句(for、while)完成这个任务。

输入格式
第一行输入一个正整数 n n n

输出格式
输出一个正整数,表示 n ! n! n!

输入输出样例
输入

3

输出

6

提示
数据保证, 1 ≤ n ≤ 12 1 \leq n\le12 1n12

已通过代码

import java.util.*;
import java.lang.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int fac = factorial(n);
        System.out.print(fac);
    }
    
//    for循环实现阶乘
//    public static int factorial(int n){
//        int fac = 1;
//        for(int i = 1; i <= n; i++){
//            fac *= i;
//        }
//        return fac;
//    }
    
//  递归实现阶乘  
    public static int factorial(int n){
        if(n == 1){
            return 1;
        }else{
            return n * factorial(n - 1);
        }
    }
}


P5461 赦免战俘

题目描述
现有 2 n × 2 n ( n ≤ 10 ) 2^n\times 2^n (n\le10) 2n×2n(n10) 名作弊者站成一个正方形方阵等候 kkksc03 的发落。kkksc03 决定赦免一些作弊者。他将正方形矩阵均分为 4 个更小的正方形矩阵,每个更小的矩阵的边长是原矩阵的一半。其中左上角那一个矩阵的所有作弊者都将得到赦免,剩下 3 个小矩阵中,每一个矩阵继续分为 4 个更小的矩阵,然后通过同样的方式赦免作弊者……直到矩阵无法再分下去为止。所有没有被赦免的作弊者都将被处以棕名处罚。

给出 n n n,请输出每名作弊者的命运,其中 0 代表被赦免,1 代表不被赦免。

输入格式
一个整数 n n n

输出格式
2 n × 2 n 2^n \times 2^n 2n×2n 的 01 矩阵,代表每个人是否被赦免。数字之间有一个空格。

输入输出样例
输入

3

输出

0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 1
0 0 0 0 0 1 0 1
0 0 0 0 1 1 1 1
0 0 0 1 0 0 0 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1
1 1 1 1 1 1 1 1

已通过代码

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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int len = 1 << n;//位运算比Math.pow(2, n)快
        int a[][] = new int[len][len];
        for (int i = 0; i < len; i++) {
            Arrays.fill(a[i], 1);//此函数比逐个赋值为1要快
        }

        pardon(a, 0, 0, a.length);

        // 用StringBuilder存储再输出,可以节省输出时间
        // 注:在二重for循环中逐个输出字符串会超时最后一个用例
        for (int i = 0; i < a.length; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < a.length - 1; j++) {
                sb.append(a[i][j]).append(" ");
            }
            sb.append(a[i][a.length - 1]);
            System.out.println(sb);
        }
    }
    
	// 递归函数实现赦免
    public static void pardon(int[][]a, int x, int y, int len) {
        for (int i = x; i < x + len / 2; i++) {
            for (int j = y; j < y + len / 2; j++) {
                a[i][j] = 0;
            }
        }
        // 当矩阵长度为2或3时,只用处理左上角
        // 此时已经处理完毕,无需再递归
        if (len == 2 || len == 3) {
            return;
        }
        pardon(a, x + len / 2, y, len / 2);
        pardon(a, x, y + len / 2, len / 2);
        pardon(a, x + len / 2, y + len / 2, len / 2);
    }
}

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

题目描述
现有 N N N 名同学参加了期末考试,并且获得了每名同学的信息:姓名(不超过 8 8 8 个字符的仅有英文小写字母的字符串)、语文、数学、英语成绩(均为不超过 150 150 150 的自然数)。总分最高的学生就是最厉害的,请输出最厉害的学生各项信息(姓名、各科成绩)。如果有多个总分相同的学生,输出靠前的那位。

输入格式
第一行输入一个正整数 N N N,表示学生个数。

第二行开始,往下 N N N 行,对于每一行首先先输入一个字符串表示学生姓名,再输入三个自然数表示语文、数学、英语的成绩。均用空格相隔。

输出格式
输出最厉害的学生。

输入输出样例
输入

3
senpai 114 51 4
lxl 114 10 23
fafa 51 42 60

输出

senpai 114 51 4

提示
数据保证, 1 ≤ N ≤ 1000 1 \leq N \leq 1000 1N1000,姓名为长度不超过 8 8 8 的字符串,语文、数学、英语成绩均为不超过 150 150 150 的自然数。

已通过代码

import java.util.*;

public class Main {
    public static class Student {
        String Name;
        int Chinese;
        int Math;
        int English;
        int Sum;

        public Student(String Name, int Chinese, int Math, int English, int Sum) {
            this.Name = Name;
            this.Chinese = Chinese;
            this.Math = Math;
            this.English = English;
            this.Sum = Sum;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Student stu[] = new Student[N];
        for (int i = 0; i < n; i++) {
            String name = sc.next();
            int Chinese = sc.nextInt();
            int Math = sc.nextInt();
            int English = sc.nextInt();
            int Sum = Chinese + Math + English;
            stu[i] = new Student(name, Chinese, Math, English, Sum);
        }

        int max = stu[0].Sum;
        int index = 0;
        for (int i = 0; i < n; i++) {
            if (stu[i].Sum > max) {
                max = stu[i].Sum;
                index = i;
            }
        }
        
        System.out.println(stu[index].Name + " " + stu[index].Chinese + " " + stu[index].Math + " " + stu[index].English);
    }
}

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

题目描述
现有 N ( N ≤ 1000 ) N(N\le 1000) N(N1000) 名同学参加了期末考试,并且获得了每名同学的信息:姓名(不超过 8 8 8 个字符的字符串,没有空格)、语文、数学、英语成绩(均为不超过 150 150 150 的自然数)。如果某对学生 < i , j > \text{<}i,j\text{>} <i,j> 的每一科成绩的分差都不大于 5 5 5,且总分分差不大于 10 10 10,那么这对学生就是“旗鼓相当的对手”。现在我们想知道这些同学中,哪些是“旗鼓相当的对手”?请输出他们的姓名。

所有人的姓名是按照字典序给出的,输出时也应该按照字典序输出所有对手组合。也就是说,这对组合的第一个名字的字典序应该小于第二个;如果两个组合中第一个名字不一样,则第一个名字字典序小的先输出;如果两个组合的第一个名字一样但第二个名字不同,则第二个名字字典序小的先输出。

输入格式
第一行输入一个正整数 N N N,表示学生个数。

第二行开始,往下 N N N 行,对于每一行首先先输入一个字符串表示学生姓名,再输入三个自然数表示语文、数学、英语的成绩。均用空格相隔。

输出格式
输出若干行,每行两个以空格隔开的字符串,表示一组旗鼓相当的对手。注意题目描述中的输出格式。

输入输出样例
输入

3
fafa 90 90 90
lxl 95 85 90
senpai 100 80 91

输出

fafa lxl
lxl senpai

提示
数据保证, 1 ≤ N ≤ 1000 1 \leq N \leq 1000 1N1000,姓名为长度不超过 8 8 8 的字符串,语文、数学、英语成绩均为不超过 150 150 150 的自然数。

已通过代码

import java.util.*;

public class Main {
    public static class Stu {
        String Name1;
        int Chinese1;
        int Math1;
        int English1;
        int Sum1;

        public Stu(String Name, int Chinese, int Math, int English, int Sum) {
            this.Name1 = Name;
            this.Chinese1 = Chinese;
            this.Math1 = Math;
            this.English1 = English;
            this.Sum1 = Sum;
        }

        public String judge(Stu b){
            int subChinese = Math.abs(this.Chinese1 - b.Chinese1);
            int subMath = Math.abs(this.Math1 - b.Math1);
            int subEnglish = Math.abs(this.English1 - b.English1);
            int subSum = Math.abs(this.Chinese1 + this.Math1 + this.English1 - b.Chinese1 - b.Math1 - b.English1);
            if (subChinese <= 5 && subMath <= 5 && subEnglish <= 5 && subSum <= 10) {
                StringBuffer sb = new StringBuffer(this.Name1 + " " + b.Name1);
                return sb.toString();
            }
            return null;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Stu[] stu = new Stu[n];
        for (int i = 0; i < n; i++) {
            String name = sc.next();
            int Chinese = sc.nextInt();
            int Math = sc.nextInt();
            int English = sc.nextInt();
            int Sum = Chinese + Math + English;
            stu[i] = new Stu(name, Chinese, Math, English, Sum);
        }

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                String ans = stu[i].judge(stu[j]);
                if(ans != null){
                    System.out.println(ans);
                }
            }
        }
    }
}

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

本题存在精度误差问题,请将 a * 0.7 + b * 0.3 与 80 比较 转化为 a * 7 + b * 3 与 800 比较。

题目描述
第一行一个整数 N N N

接下来 N N N 行,每行 3 3 3 个整数,依次代表学号、学业成绩和素质拓展成绩。

输入格式
N N N 行,如果第 i i i 名学生是优秀的,输出 Excellent,否则输出 Not excellent

输出格式

输入输出样例
输入

4
1223 95 59
1224 50 7
1473 32 45
1556 86 99

输出

Excellent
Not excellent
Not excellent
Excellent

提示
数据保证, 1 ≤ N ≤ 1000 1 \le N\le 1000 1N1000,学号为不超过 100000 100000 100000 的正整数,学业成绩和素质拓展成绩为 0 ∼ 100 0 \sim 100 0100 之间的正整数。

已通过代码

import java.util.*;

public class Main {
    public static class Stu {
        int ID;
        int SchoolScore;
        int QualityScore;
        int CompositeScore;

        public Stu(int id, int schoolScore, int qualityScore, int compositeScore) {
            this.ID = id;
            this.SchoolScore = schoolScore;
            this.QualityScore = qualityScore;
            this.CompositeScore = compositeScore;
        }

        public int getTotalScore(){
            return this.SchoolScore + this.QualityScore;
        }
    }

    public static boolean judge(int totalScore, int compositeScore){
        if(totalScore > 140 && compositeScore >= 800){
            return true;
        }else{
            return false;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Stu[] stu = new Stu[n];
        for (int i = 0; i < n; i++) {
            int id = sc.nextInt();
            int schoolScore = sc.nextInt();
            int qualityScore = sc.nextInt();
            int compositeScore = schoolScore * 7 + qualityScore * 3;
            stu[i] = new Stu(id, schoolScore, qualityScore, compositeScore);
        }
        for (int i = 0; i < n; i++) {
            if(judge(stu[i].getTotalScore(), stu[i].CompositeScore)){
                System.out.println("Excellent");
            }else{
                System.out.println("Not excellent");
            }
        }
    }
}

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

题目描述
已知正整数 n n n 是两个不同的质数的乘积,试求出两者中较大的那个质数。

输入格式
输入一个正整数 n n n

输出格式
输出一个正整数 p p p,即较大的那个质数。

输入输出样例
输入

21

输出

7

提示
1 ≤ n ≤ 2 × 1 0 9 1 \le n\le 2\times 10^9 1n2×109

已通过代码

import java.util.*;

public class Main {
    public static int maxPrime(int n){
        int i = 2;
        for (; i < n; i++) {
            if(n % i == 0){
                break;
            }
        }
        return n / i;
    }

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

P1304 哥德巴赫猜想

题目描述
输入一个偶数 N N N,验证 4 ∼ N 4\sim N 4N 所有偶数是否符合哥德巴赫猜想:任一大于 2 2 2 的偶数都可写成两个质数之和。如果一个数不止一种分法,则输出第一个加数相比其他分法最小的方案。例如 10 10 10 10 = 3 + 7 = 5 + 5 10=3+7=5+5 10=3+7=5+5,则 10 = 5 + 5 10=5+5 10=5+5 是错误答案。

输入格式
第一行输入一个正偶数 N N N

输出格式
输出 N − 2 2 \dfrac{N-2}{2} 2N2 行。对于第 i i i 行:

首先先输出正偶数 2 i + 2 2i+2 2i+2,然后输出等号,再输出加和为 2 i + 2 2i+2 2i+2 且第一个加数最小的两个质数,以加号隔开。

输入输出样例
输入

10

输出

4=2+2
6=3+3
8=3+5
10=3+7

提示

数据保证, 4 ≤ N ≤ 10000 4 \leq N\leq10000 4N10000

已通过代码

import java.util.*;

public class Main {
    public static int Prime(int n, boolean isNotPrime[], int prime[]){
        isNotPrime[0] = true;
        isNotPrime[1] = true;

        int count = 0;
        for(int i = 2; i < n; i++){
            if(isNotPrime[i] == false){
                prime[count++] = i;
            }
            for(int j = 0; i * prime[j] < n; j++){
                isNotPrime[i * prime[j]] = true;
                if(i % prime[j] == 0){
                    break;
                }
            }
        }
        return count;
    }

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

        boolean[] isNotPrime = new boolean[n];
        int[] prime = new int[n];
        int count = Prime(n, isNotPrime, prime);

        for (int i = 4; i <= n; i += 2){
            for(int j = 0; j < count; j++){
                if(!isNotPrime[i - prime[j]]){
                    System.out.println(i + "=" + prime[j] + "+" + (i - prime[j]));
                    break;
                }
            }
        }
    }
}

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

题目描述
因为 151 151 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 151 151 是回文质数。

写一个程序来找出范围 [ a , b ] ( 5 ≤ a < b ≤ 100 , 000 , 000 ) [a,b] (5 \le a < b \le 100,000,000) [a,b](5a<b100,000,000)(一亿)间的所有回文质数。

输入格式
第一行输入两个正整数 a a a b b b

输出格式
输出一个回文质数的列表,一行一个。

输入输出样例
输入

5 500

输出

5
7
11
101
131
151
181
191
313
353
373
383

提示
提示 1: 找出所有的回文数再判断它们是不是质数(素数).
提示 2: 要产生正确的回文数,你可能需要几个像下面这样的循环。
产生长度为 5 5 5 的回文数:

for (d1 = 1; d1 <= 9; d1+=2) {    // 只有奇数才会是素数
     for (d2 = 0; d2 <= 9; d2++) {
         for (d3 = 0; d3 <= 9; d3++) {
           palindrome = 10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1;//(处理回文数...)
         }
     }
 }

已通过代码

//注:这道题用质数筛会超时
//盲猜一个是因为a缩小了范围,而质数筛没办法把缩小的范围抛开
import java.util.*;
import java.lang.*;

public class Main {
    public static boolean isPrime(int n) {
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static boolean isReversed(int x){
        int x1 = 0;
        while(x1 < x){
            x1 = x1 * 10 + x % 10;
            x /= 10;
        }
        return x1 == x || x1 / 10 == x;
    }

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

P2415 集合求和

题目描述
给定一个集合 s s s(集合元素数量 ≤ 30 \le 30 30),求出此集合所有子集元素之和。

输入格式
集合中的元素(元素 ≤ 1000 \le 1000 1000

输出格式
s s s 所有子集元素之和。

输入输出样例
输入

2 3

输出

10

提示
【样例解释】

子集为: ∅ , { 2 } , { 3 } , { 2 , 3 } \varnothing, \{ 2 \}, \{ 3 \}, \{ 2, 3 \} ,{2},{3},{2,3},和为 2 + 3 + 2 + 3 = 10 2 + 3 + 2 + 3 = 10 2+3+2+3=10


【数据范围】

对于 100 % 100 \% 100% 的数据, 1 ≤ ∣ s ∣ ≤ 30 1 \le \lvert s \rvert \le 30 1s30 1 ≤ s i ≤ 1000 1 \le s_i \le 1000 1si1000 s s s 所有子集元素之和 ≤ 10 18 \le {10}^{18} 1018

已通过代码

import java.util.*;
import java.lang.*;
//分析可得,所有的元素和 sum 乘以 2 的 set.length-1 次方 等于集合所有子集的和
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] set = sc.nextLine().split(" ");
        long sum = 0;//如果要用位运算,那么sum必须是long而不能是int
        for(String element : set){
            int temp = Integer.parseInt(element);
            sum += temp;
        }
        long ans = sum << (set.length - 1);
        System.out.println(ans);
    }
}

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

题目描述
一只小猴买了若干个桃子。第一天他刚好吃了这些桃子的一半,又贪嘴多吃了一个;接下来的每一天它都会吃剩余的桃子的一半外加一个。第 n n n 天早上起来一看,只剩下 1 1 1 个桃子了。请问小猴买了几个桃子?

输入格式
输入一个正整数 n n n,表示天数。

输出格式
输出小猴买了多少个桃子。

输入输出样例
输入

4

输出

22

提示
数据保证, 1 ≤ n ≤ 20 1\le n\le20 1n20

已通过代码

import java.util.*;
import java.lang.*;

public class Main {
    public static int Days(int n){
        int pre = 1;
        for(int i = n - 1; i >= 1; i--){
            int now = (pre + 1) * 2;
            pre = now;
        }
        return pre;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int days = Days(n);
        System.out.println(days);
    }
}

P5744 【深基7.习9】培训

题目描述
某培训机构的学员有如下信息:

  • 姓名(字符串)
  • 年龄(周岁,整数)
  • 去年 NOIP 成绩(整数,且保证是 5 5 5 的倍数)

经过为期一年的培训,所有同学的成绩都有所提高,提升了 20 % 20\% 20%(当然 NOIP 满分是 600 600 600 分,不能超过这个得分)。

输入学员信息,请设计一个结构体储存这些学生信息,并设计一个函数模拟培训过程,其参数是这样的结构体类型,返回同样的结构体类型,并输出学员信息。

输入格式
第一行输入一个正整数 n n n,表示学员个数。

第二行开始往下 n n n 行。每行首先是一个字符串表示学员姓名,再是一个整数表示学员年龄,再是一个整数为去年 NOIP 成绩。

输出格式
输出 n n n 行,每行首先输出一个字符串表示学生姓名,再往后两个整数,表示经过一年的培训后学员的年龄和他们今年的 NOIP 成绩。以空格隔开。

输入输出样例
输入

3
kkksc03 24 0
chen_zhe 14 400
nzhtl1477 18 590

输出

kkksc03 25 0
chen_zhe 15 480
nzhtl1477 19 600

提示
数据保证, 1 ≤ n ≤ 5 1 \leq n \leq 5 1n5。年龄为 0 ∼ 100 0 \sim 100 0100(含 0 0 0 100 100 100)的整数。成绩为 0 ∼ 600 0 \sim 600 0600(含 0 0 0 600 600 600)的 5 5 5 的整倍数。

已通过代码

import java.util.*;
import java.lang.*;

public class Main {
    public static class Stu{
        String Name;
        int Age;
        int Score;
        public Stu(String name, int age, int score){
            this.Name = name;
            this.Age = age;
            this.Score = score;
        }
        public void Train(){
            this.Age++;
            if(this.Score * 1.2 <= 600){
                this.Score *= 1.2;
            }else{
                this.Score = 600;
            }
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Stu[] stus = new Stu[n];
        for(int i = 0; i < n; i++){
            String name = sc.next();
            int age = sc.nextInt();
            int score = sc.nextInt();
            stus[i] = new Stu(name, age, score);
        }
        for(int i = 0; i < n; i++){
            stus[i].Train();
            System.out.println(stus[i].Name + " " + stus[i].Age + " " + stus[i].Score);
        }
    }
}
  • 46
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值