第四届传智杯 | JAVA | B组 初赛

A 组原成绩

在这里插入图片描述

输入:50 100 100
输出:90

题解:简单的输入输出
代码如下:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        double t=sc.nextDouble(),h=sc.nextDouble(),e=sc.nextDouble();
        System.out.print((int)(t*0.2+h*0.3+e*0.5));
    }
}

唯一需要注意的就是转型问题。


B 报告赋分

在这里插入图片描述

输入:
2
70 17
80 10
输出:
70
70

题解:简单的if else 判断 根据各种情况去输入各种结果

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a,p;
        for (int i = 0; i < n; i++) {
            a=sc.nextInt();
            p=sc.nextInt();
            if(p<16) a-=10;
            if(p>=20) a-=p-20;
            if(a<=0)a=0;
            System.out.println(a);
        }
    }
}

C. 竞争得分

在这里插入图片描述

输入:
3
1 2 3
输出:
0 50 100

题解:此题只要求出最大值和最小值 就算结束了 要注意时间复杂度的问题

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        double min=1001; double max=-1;
        double[] score=new double[n];

        for (int i = 0; i < n; i++) {
            score[i]=sc.nextDouble();
            if(score[i]>max) max=score[i];
            if(score[i]<min) min=score[i];
        }
        for (int i = 0; i <n ; i++) {
            System.out.print((int)(100*((score[i]-min)/(max-min)))+" ");;
        }

    }
}

D.小卡和质数2

在这里插入图片描述

输入:
9
5
6
7
8
9
10
100
1000
10000
输出:
2
4
4
2
2
4
22
163
1132

题解:此题难度起伏了,难度在于如何优化时间复杂度。不是我说,小卡真事多。未曾优化的垃圾代码,时间复杂度为O(n3)。
如下:

import java.util.Scanner;

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

        for (int i = 0; i < n; i++) {
            arr=sc.nextInt();
            for(int j=0;j<arr;j++)
            {
                if(isPrime(arr^j)==1){
                    count++;
                }
            }
            System.out.println(count);
            count=0;
        }
    }
    public static int isPrime(int n){
        int i=2;
        for(;i<n;i++){
            if(n%i==0){
                return 0;
            }
        }
        if(n==i){
            return 1;
        }
        return 0;
    }
}

上面代码所用时间:2299ms
经我刻苦优化之后,就是优化了一个判断质数这个模块。
判断质数代码如下:

  public static int isPrime(int n){
        int i=2;
        for(;i<=Math.sqrt(n);i++){
            if(n%i==0){
                return 0;
            }
        }
        if(n==i){
            return 1;
        }else{
            return 1;
        }
    }

此刻的代码所用时间为:1088ms 其实我觉得还可以优化 针对这道题,但是我现在要回去了。

E.萝卜数据库

在这里插入图片描述

输入
4 5
1 2 1 2 2 4
2 2 1 5
1 2 3 5 4 6
2 4 7 8

输出:
1
0

题解:按题目来就行了。

import java.util.Scanner;

import java.util.*;


public class   Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] m= new int[n];
        int p =0;
        String o[] = new String[k + 1];
        Arrays.fill(o, "");
        for (int i = 0; i < n; i++) {
            int a = sc.nextInt();
            if (a == 1) {
                int l = sc.nextInt();
                for (int j = 0; j < l; j++) {
                    int x = sc.nextInt();
                    int y = sc.nextInt();
                    o[x] += y + " ";

                }
            }
            if (a == 2) {
                int c = sc.nextInt();
                int min = sc.nextInt();
                int max = sc.nextInt();
                int s = 0;
                if(o[c]!=""){
                    String q[] = o[c].split(" ");
                    for (int j = 0; j < q.length; j++) {
                        if (q[j] != null) {
                            Integer h = Integer.valueOf(q[j]);
                            if (h >= min && h <= max) {
                                s++;
                            }
                        }
                    }
                    m[p]=s;
                    p++;
                }}
        }
        for (int i = 0; i <p ; i++) {
            System.out.println(m[i]);
        }
    }
}

总之吧,就非常顺畅。


  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值