第三届传智杯程序设计赛B组题解Java版

第三届传智杯程序设计赛B组初赛题解Java版

A - 课程报名

在这里插入图片描述

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();	// 总可买人数n 
        int v = in.nextInt();	// 初始定价 
        int m = in.nextInt();	// 每m个人提升一次价格 
        int a = in.nextInt();	// 每次提升a元 
        int sum = 0;
        int temp = 1;
        for(int i=0;i<n;i++){
            if(i==m*temp){
                temp++;
            }
            sum+=v+(temp-1)*a;
        }
        System.out.println(sum);
    }
}

B - 期末考试成绩

在这里插入图片描述

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double gpa = 0.0;
        int temp = 0;
        int course = 0;
        int x = in.nextInt();
        if(x >=90){
            gpa = 4.0;
        }else if(x>=60 && x<=89){
            temp = 90 - x;
            gpa = 4.0 - temp*0.1;
        }else{
            course = (int)Math.floor(Math.sqrt(x)*10);
            if(course >=90){
                gpa = 4.0;
            }else if(course>=60 && course<=89){
                temp = 90 - course;
                gpa = 4.0 - temp*0.1;
            }else{
                gpa = 0.0;
            }
        }
        System.out.println(String.format("%.1f",gpa));
    }
}

C - 志愿者

在这里插入图片描述

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

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Volunteer[] volunteerList = new Volunteer[n];
        for(int i=0;i<n;i++){
            int t = in.nextInt();
            int k = in.nextInt();
            volunteerList[i] = new Volunteer();
            volunteerList[i].i = (i+1);
            volunteerList[i].t = t;
            volunteerList[i].k = k;
            volunteerList[i].tk = (t*k);
        }
        Arrays.sort(volunteerList);
        for(int i=0;i<n;i++){
            if(i!=(n-1))
                System.out.print(volunteerList[i].i+" ");
            else
                System.out.print(volunteerList[i].i);
        }
    }
    static class Volunteer implements Comparable<Volunteer>{
        public int i;   // 志愿者编号
        public int t;   // 工作时长
        public int k;   // 难度系数
        public int tk;  // 贡献
        
        // 排序的比较方法

        /**
         * 相同贡献度的比时长 长的在前
         * 相同时长的比编号 小的在前
         */
        @Override
        public int compareTo(Volunteer b) {
            if(this.tk > b.tk){
                return -1;
            }else if(this.tk == b.tk){
                if(this.t > b.t){
                    return -1;
                }else if(this.t == b.t){
                    if(this.i < b.i)
                        return -1;
                }
            }
            return 1;
        }
    }
}

D - 终端

在这里插入图片描述

import java.util.*;

/**
 * 使用ArrayList
 * 利用其下标顺序来代替文件创建的先后顺序
 * touch:向list中新增一个元素
 * rename:将指定下标重新赋值
 * rm:删除list中元素 
 *      元素的相对顺序不会发生改变 参照ArrayList源码fastRemove 将删除位置后的元素统统向前拷贝 并将最后一位置为null
 * ls:遍历数组元素
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<String> fileSystem = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String file = null;
            String command = in.next();
            if (!"ls".equals(command))
                file = in.next();
            if ("touch".equals(command)){
                if (file != null){
                    fileSystem.add(file);
                }
            }else if ("rm".equals(command)){
                if (file != null){
                    if (fileSystem.contains(file)){
                        fileSystem.remove(file);
                    }
                }
            }else if ("ls".equals(command)){
                for (String s : fileSystem) {
                    System.out.println(s);
                }
            }else{
                String newfile = in.next();
                if (file != null){
                    if (fileSystem.contains(file) && !fileSystem.contains(newfile)) {
                        for (int j = fileSystem.size() - 1; j >= 0 ; j--) {
                            if (fileSystem.get(j).equals(file)){
                                fileSystem.set(j,newfile);
                            }
                        }
                    }
                }
            }
        }
    }
}

E - 运气

在这里插入图片描述

import java.util.*;

/**
 * 深度优先遍历
 * 每次遍历到终止条件时判断一下是否是k的倍数 如果是将结果值+1 不是 直接return
 * 最后将解按指定规则处理
 */
public class Main {
    private static int n;
    private static int k;
    private static long res;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        k = in.nextInt();
        if (n == 0){
            return;
        }
        dfs(0,"");
        System.out.println((long) (res%(Math.pow(10,9)+7)));
    }

    private static void check(String number){
        long parseLong = Long.parseLong(number);
        if (parseLong%k == 0){
            res++;
        }
    }

    private static void dfs(int level, String number) {
        if (level == n){
            check(number);
            return;
        }
        for (int i = 1; i <= 6; i++) {
            dfs(level+1,number+i);
        }
    }
}
  • 7
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值