杰拉斯的博客
[整理]ACM详解(5)——排序
杰拉斯 | 时间:2012-02-17, Fri | 5,181 views编程算法
[整理]ACM详解(4)——递归
杰拉斯 | 时间:2012-02-17, Fri | 4,962 views编程算法
递归在解决一些问题的时候非常直观,但是在是使用递归的时候要注意递归的深度,如果深度太深,可能会造成堆栈溢出。下面通过实例介绍如何使用。
[整理]ACM模拟题详解(3)——数论(续)
杰拉斯 | 时间:2012-02-17, Fri | 4,777 views编程算法
5、Prime Ring Problem
Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
Note: the number of first circle should always be 1.
Input
n (0 < n < 20).
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
[整理]ACM模拟题详解(2)——简单数论
杰拉斯 | 时间:2012-02-17, Fri | 8,501 views编程算法
有很多与数字相关的题目,主要考察基本的编程能力,如果数学比较好,对于解决这些问题有比较好的帮助。下面的题目是学生收集的题目,我进行了讲解。
1、Self Numbers
Description
In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)), d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence 33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ... The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97.
Input
No input for this problem.
Output
Write a program to output all positive self-numbers less than 10000 in increasing order, one per line.
[整理]ACM模拟题讲解(1)-高精度
杰拉斯 | 时间:2012-02-17, Fri | 5,786 views编程算法
Java中提供了byte、short、int和long表示整数,float和double来表示浮点数,每种类型都有一定的表示范围,当超过了这个范围之后就不能处理了。为了提供对非常大的整数和浮点数的处理,Java提供了BigDecimal和BigInteger。下面的代码演示了BigDecimal和BigInteger的基本用法:
BigDecimal data1 = new BigDecimal("23232123456789.123456789"); BigDecimal data2 = new BigDecimal("23423423123456789.123456789"); BigDecimal data3 = data1.add(data2); System.out.println(data3.toString()); BigInteger iData1 = new BigInteger("123123123456456789789123456789"); BigInteger iData2 = new BigInteger("12312312323232456456789789123456789"); BigInteger iData3 = iData1.add(iData2); System.out.println(iData3.toString());
如果不使用这些类库如何实现大整数和大浮点数的计算呢?下面通过ACM模拟题介绍。