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

文章讲述了两个Java程序,分别处理两个CCF2014竞赛题目:第一个是输入整数数组,找出重复元素并计数;第二个是输入矩阵,按照特定模式打印。展示了如何使用Scanner和数组操作实现这两个问题的解决方案。
摘要由CSDN通过智能技术生成

第一题

package ccf201412;

import java.util.Scanner;

public class p1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] x = new int[n];
        int[] y = new int[n + 1];
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            boolean flag = false;
            x[i] = sc.nextInt();
            for (int j = 0; j < i; j++) {
                if (x[j] == x[i]) {
                    flag = true;
                }
            }
            if (flag) {
                y[x[i]]++;
            } else {
                y[x[i]] = 1;
            }
            res[i] = y[x[i]];
        }
        for (int i = 0; i < n; i++) {
            if(i==n-1){
                System.out.print(res[i]);
            }else {
                System.out.print(res[i]+" ");
            }
        }
    }
}

第二题

package ccf201412;

import java.util.Scanner;

public class p2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] xy = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                xy[i][j] = sc.nextInt();
            }
        }
        int i = 0;
        int j = 0;
        System.out.print(xy[0][0] + " ");
        if (n == 1) {
            return;
        }
        while (true) {
            if (j == n - 1) {
                i++;
            } else {
                j++;
            }
            System.out.print(xy[i][j] + " ");
            while (i != n - 1 && j != 0) {
                System.out.print(xy[++i][--j] + " ");
            }
            if (i == n - 1) {
                j++;
            } else {
                i++;
            }
            System.out.print(xy[i][j] + " ");
            if (j == n - 1 && i == n - 1) {
                break;
            }
            while (i != 0 && j != n - 1) {
                System.out.print(xy[--i][++j] + " ");
            }
        }
    }
}
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值