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

文章包含了两段Java代码,分别解决不同问题:第一题是计算给定整数列表中连续相同值的计分规则,第二题是处理一个二维数组,根据特定条件改变数组元素并输出结果。
摘要由CSDN通过智能技术生成

第一题

package ccf201803;

import java.util.ArrayList;
import java.util.Scanner;

public class p1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<>();
        int res = 0;
        int get;
        do {
            get = sc.nextInt();
            list.add(get);
        } while (get != 0);
        int score = 2;
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                if (list.get(i) == 2) {
                    res += 2;
                } else if (list.get(i) == 1) {
                    res += 1;
                } else {
                    break;
                }
            } else {
                if (list.get(i) == 0) break;
                if (list.get(i) == 2) {
                    if (list.get(i - 1) == 1) {
                        res += score;
                    } else {
                        score += 2;
                        res += score;
                    }
                } else {
                    res += 1;
                    score = 2;
                }
            }
        }
        System.out.println(res);
    }
}

第二题

package ccf201803;

import java.util.Scanner;

public class p2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int L = sc.nextInt();
        int t = sc.nextInt();
        int[][] a = new int[n + 1][2];
        for (int i = 1; i <= n; i++) {
            a[i][0] = sc.nextInt();
            a[i][1] = 1;
        }
        for (int i = 0; i < t; i++) {
            for (int j = 1; j <= n; j++) {
                if (a[j][0] == L || a[j][0] == 0) {
                    a[j][1] = a[j][1] * (-1);
                }
                int k=j+1;
                if(k==n){
                    a[j][0] += a[j][1];
                }else {
                    for (; k <= n; k++) {
                        if (a[j][0] == a[k][0]) {
                            a[j][1] = a[j][1] * (-1);
                            a[k][1] = a[k][1] * (-1);
                        }
                    }
                    a[j][0] += a[j][1];
                }

            }
        }
        for (int i = 1; i <= n; i++) {
            System.out.print(a[i][0]+" ");
        }
    }
}
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值