CCF计算机软件能力认证201903前两题java满分解

第一题(格式化输出)

package ccf201903;

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

public class p1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double[] a = new double[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        Arrays.sort(a);
        double max = a[n - 1];
        double min = a[0];
        double mid;
        if (n % 2 == 0) {
            mid = (a[n / 2 - 1] + a[n / 2]) / 2;
        } else {
            mid = a[n / 2];
        }
        String res;
        if (mid - (int) mid == 0) {
            res = "%.0f %.0f %.0f";
        } else {
            res = "%.0f %.1f %.0f";
        }
        System.out.printf(res, max, mid, min);
    }
}

第二题(经典24点问题,使用栈解决;ccf不支持switch)

package ccf201903;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class p2 {
    public static void main(String[] args) throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(bf.readLine());
        String[][] line = new String[n][];
        for (int i = 0; i < n; i++) {
            line[i] = bf.readLine().split("");
        }
        for (int i = 0; i < n; i++) {
            if (getFin(line[i]) == 24) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }

    public static int getFin(String[] line) {
        int[] num = new int[4];
        int index = 1;
        num[0] = Integer.parseInt(line[0]);
        int fin = 0;
        for (int i = 1; i < line.length; i += 2) {
            if (line[i].equals("+")) {
                num[index] = Integer.parseInt(line[i + 1]);
                index++;
            } else if (line[i].equals("-")) {
                num[index] = Integer.parseInt(line[i + 1]) * (-1);
                index++;
            } else if ((line[i].equals("x"))) {
                num[index] = Integer.parseInt(line[i + 1]);
                int a = num[index - 1];
                int b = num[index];
                int res = a * b;
                num[index - 1] = res;
                num[index] = 0;
            } else if ((line[i].equals("/"))) {
                num[index] = Integer.parseInt(line[i + 1]);
                int a = num[index - 1];
                int b = num[index];
                int res = a / b;
                num[index - 1] = res;
                num[index] = 0;
            }
        }
        for (int i = 0; i < num.length; i++) {
            fin += num[i];
        }
        return fin;
    }
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值