当前位置: 首页 > news >正文

CCF201312--模拟练习试题参考答案(Java)

来源:CCF计算机职业资格网站。


问题参见:CCF201312赛题。


为了帮助大家熟悉 CCF 软件能力认证考试的操作方式与答题环境,了解试题的大致难度,做好考前的准备,故在此提供试题的参考答案。Java 程序是灵活的,为了解决同一个问题,即使结果相同,程序的内容也不一定是完全一致的,仅供各位在练习时参考。

1. 出现次数最多的数

import java.util.*;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	public void run() {
		Scanner fin = new Scanner(System.in);
		int N = fin.nextInt();
		int[] count = new int[10001];
		for (int i = 0; i < N; ++i) {
			++count[fin.nextInt()];
		}
		int maxCount = -1;
		int result = 0;
		for (int i = 1; i <= 10000; ++i) {
			if (count[i] > maxCount) {
				maxCount = count[i];
				result = i;
			}
		}
		System.out.println(result);
	}
}

2. ISBN 号码

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

public class Main {
	public static void main(String args[]) {
		BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
		try{
			int sum=0;char cc='0';
			String isbn_0 = bin.readLine();
			String isbn = isbn_0.replace("-", "");

			for(int i=0; i<9; i++){
				int ii = (int)isbn.charAt(i)-48;
				sum += ii * (i+1);
			}

			sum = sum % 11;
			if(sum == 10) cc = 'X';//
			else cc = (char)(sum+48);
			if(cc == isbn.charAt(9)) System.out.println("Right");
			else{
				isbn_0 = isbn_0.substring(0,12) + cc;
				System.out.println(isbn_0);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

3.最大的矩形

import java.util.*;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	public void run() {
		Scanner fin = new Scanner(System.in);int N = fin.nextInt();
		int[] height = new int[N];
		for (int i = 0; i < N; ++i) height[i] = fin.nextInt();
		
		int result = 0;
		for (int i = 0; i < N; ++i) {
			int width = 1;
			for (int j = i - 1; j >= 0; --j) {
				if (height[j] < height[i]) break;
				++width;
			}
			for (int j = i + 1; j < N; ++j) {
				if (height[j] < height[i]) break;
				++width;
			}
			int area = width * height[i];
			result = Math.max(result, area);
		}

		System.out.println(result);
	}
}

4.有趣的数

import java.util.*;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	public void run() {
		Scanner fin = new Scanner(System.in);
		int N = fin.nextInt();
		long[] count = new long[8];
		count[6] = 0;
		count[7] = 1;
		long mod = 1000000007;for (int i = 2; i <= N; ++i) {
			long[] newCount = new long[8];
			newCount[0] = (count[0] * 2 + count[1] + count[3]) % mod;
			newCount[1] = (count[1] * 2 + count[2] + count[5]) % mod;
			newCount[2] = (count[2] + count[6]) % mod;
			newCount[3] = (count[3] * 2 + count[4] + count[5]) % mod;
			newCount[4] = (count[4] + count[7]) % mod;
			newCount[5] = (count[5] * 2 + count[6] + count[7]) % mod;
			newCount[6] = 0;
			newCount[7] = 1;
			count = newCount;
		}

		System.out.println(count[0]);
	}
}

5.I’m stuck!

import java.util.*;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	public void run() {
		Scanner fin = new Scanner(System.in);
		int N = fin.nextInt();
		long[] count = new long[8];
		count[6] = 0;
		count[7] = 1;
		long mod = 1000000007;for (int i = 2; i <= N; ++i) {
			long[] newCount = new long[8];
			newCount[0] = (count[0] * 2 + count[1] + count[3]) % mod;
			newCount[1] = (count[1] * 2 + count[2] + count[5]) % mod;
			newCount[2] = (count[2] + count[6]) % mod;
			newCount[3] = (count[3] * 2 + count[4] + count[5]) % mod;
			newCount[4] = (count[4] + count[7]) % mod;
			newCount[5] = (count[5] * 2 + count[6] + count[7]) % mod;
			newCount[6] = 0;
			newCount[7] = 1;
			count = newCount;
		}

		System.out.println(count[0]);
	}
}

5.I’m stuck!
import java.util.*;

public class Main {
	public static void main(String[] args) {
	new Main().run();
}

public void run() {
	Scanner fin = new Scanner(System.in);
	int R = fin.nextInt();
	int C = fin.nextInt();
	fin.nextLine();
	int[][] board = new int[R + 2][C + 2];
	int rowStart = 0, colStart = 0, rowEnd = 0, colEnd = 0;
	for (int i = 1; i <= R; ++i) {
		String line = fin.nextLine();
		for (int j = 1; j <= C; ++j) {
			char c = line.charAt(j - 1);
			switch (c) {case '#': break;
				case '-': board[i][j] = 5; break;
				case '|': board[i][j] = 0xA; break;
				case '+':
				case 'S':
				case 'T':
					board[i][j] = 0xF; break;
				case '.': board[i][j] = 0x8; break;
				default: break;
			}
			if (c == 'S') {
				rowStart = i;
				colStart = j;
			} else if (c == 'T') {
				rowEnd = i;
				colEnd = j;
			}
		}
	}
		
	int[] dr = new int[] {0, -1, 0, 1};
	int[] dc = new int[] {1, 0, -1, 0};
	// Scan 1: find all cells which can reach T
	boolean[][] visited = new boolean[R + 2][C + 2];
	boolean[][] canReachT = new boolean[R + 2][C + 2];
	initVisited(visited);
	canReachT[rowEnd][colEnd] = true;
	visited[rowEnd][colEnd] = true;
	Queue<Integer> queue = new LinkedList<Integer>();
	queue.add(rowEnd);
	queue.add(colEnd);
	while (!queue.isEmpty()) {
		int r = queue.remove();
		int c = queue.remove();
		for (int i = 0; i < 4; ++i) {
			int nr = r + dr[i];int nc = c + dc[i];
			if (visited[nr][nc]) continue;
			if ((board[nr][nc] & (1 << ((i + 2) % 4))) != 0) {
				canReachT[nr][nc] = true;
				queue.add(nr);
				queue.add(nc);
				visited[nr][nc] = true;
			}
		}
	}
	/*
	for (int i = 1; i <= R; ++i) {
		for (int j = 1; j <= C; ++j) {
			if (canReachT[i][j]) {
				System.out.println("i = " + i + ", j = " + j);
			}
		}
	}
	*/
	if (!canReachT[rowStart][colStart]) {
		System.out.println("I'm stuck!");
		return;
	}
	// Scan 2: get result
	boolean[][] rCanReach = new boolean[R + 2][C + 2];
	initVisited(visited);
	queue.clear();
	visited[rowStart][colStart] = true;
	rCanReach[rowStart][colStart] = true;
	queue.add(rowStart);
	queue.add(colStart);
	while (!queue.isEmpty()) {
		int r = queue.remove();
		int c = queue.remove();
		for (int i = 0; i < 4; ++i) {
			if ((board[r][c] & (1 << i)) == 0) continue;int nr = r + dr[i];
			int nc = c + dc[i];
			if (visited[nr][nc]) continue;
			if (board[nr][nc] == 0) continue;
			rCanReach[nr][nc] = true;
			queue.add(nr);
			queue.add(nc);
			visited[nr][nc] = true;
		}
	}
	int result = 0;
	for (int i = 1; i <= R; ++i) {
		for (int j = 1; j <= C; ++j) {
			/*
			if (rCanReach[i][j]) {
				System.out.println("i = " + i + ", j = " + j);
			}
			*/
			if (rCanReach[i][j] && (!canReachT[i][j])) ++result;
		}
	}
	System.out.println(result);
}

private void initVisited(boolean[][] visited) {
	int R = visited.length - 2;
	int C = visited[0].length - 2;
	for (int i = 0; i <= R + 1; ++i) {
		visited[i][0] = true;
		visited[i][C + 1] = true;
	}

	for (int j = 0; j <= C + 1; ++j) {
		visited[0][j] = true;
		visited[R + 1][j] = true;
	}

	for (int i = 1; i <= R; ++i) {
		for (int j = 1; j <= C; ++j) {
			visited[i][j] = false;}
		}
	}
}





转载于:https://www.cnblogs.com/tigerisland/p/7564249.html

相关文章:

  • XAMP安装Apacher无法启动
  • AES加密算法的JAVA实现
  • mysql 1449 : The user specified as a definer (\'root\'@\'%\') does not exist 解决方法
  • 事务操作的统计,TPS的计算,隔离级别的读提交
  • JavaScript的parseint()函数
  • 枚举 类和结构体
  • MongoDB 查询总结
  • 【成长之路】【python】python基础3
  • 一路风景,一路欣赏,一路有你
  • 唇读、生成式对抗网络、自动音乐生成都是些什么鬼?且看人工智能年度科技盘点(提供PDF干货下载)...
  • proguard 不混淆第三方jar包的问题
  • 使用mysqlbinlog提取二进制日志
  • centos7源码编译安装Subversion 1.9.5
  • 数据分析入门--https://cn.udacity.com/
  • 基于组件的设计工作流与界面抽象
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • canvas 绘制双线技巧
  • eclipse(luna)创建web工程
  • Iterator 和 for...of 循环
  • js ES6 求数组的交集,并集,还有差集
  • Nodejs和JavaWeb协助开发
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • PHP 小技巧
  • Python爬虫--- 1.3 BS4库的解析器
  • TypeScript迭代器
  • UMLCHINA 首席专家潘加宇鼎力推荐
  • v-if和v-for连用出现的问题
  • Vue2.x学习三:事件处理生命周期钩子
  • 给新手的新浪微博 SDK 集成教程【一】
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 如何编写一个可升级的智能合约
  • 三栏布局总结
  • 我从编程教室毕业
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • (八)Flask之app.route装饰器函数的参数
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (转)四层和七层负载均衡的区别
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • .Mobi域名介绍
  • .NET Core 2.1路线图
  • .NET Framework与.NET Framework SDK有什么不同?
  • .NET 中让 Task 支持带超时的异步等待
  • .NETCORE 开发登录接口MFA谷歌多因子身份验证
  • .NetCore 如何动态路由
  • ?.的用法
  • @Transactional 详解
  • [100天算法】-不同路径 III(day 73)
  • [ACM] hdu 1201 18岁生日
  • [bzoj1006]: [HNOI2008]神奇的国度(最大势算法)
  • [ICCV2017]Neural Person Search Machines
  • [jQuery]使用jQuery.Validate进行客户端验证(中级篇-上)——不使用微软验证控件的理由...
  • [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
  • [LeetCode系列]子集枚举问题[无重复元素]
  • [NBIoT]NBIoT相关知识