package com.packages.function;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HelloWorld {
// 有一对兔子,从出生后第3个月起每个月生一对兔子,小兔子长到第三个月后每月又生一对兔子;
// 如果兔子不死,问每月兔子总数;
// rabbit[0] represents rabbits number of 0 month old
// rabbit[1] represents rabbits number of 1 month old
// rabbit[2] represents rabbits number of 2 month old
// rabbit[3] represents rabbits number of 3 month old
private static int rabbit[] = { 2, 0, 0, 0 };
private void init_rabbits() {
rabbit[0] = 2;
rabbit[1] = 0;
rabbit[2] = 0;
rabbit[3] = 0;
}
public int test1_total_num_of_rabbits(int month) {
init_rabbits();
if (month < 0) {
return 0;
} else {
for (int ii = 0; ii < month; ++ii) {
rabbit[3] += rabbit[2];
rabbit[2] = rabbit[1];
rabbit[1] = rabbit[0];
rabbit[0] = rabbit[3];
}
}
// System.out.printf("[0] %d, [1] %d, [2] %d, [3] %d\n", rabbit[0],
// rabbit[1], rabbit[2], rabbit[3]);
return rabbit[0] + rabbit[1] + rabbit[2] + rabbit[3];
}
public void test2_get_prime_num(int begin, int end) {
assert (end >= begin && begin >= 2);
for (int i = begin; i <= end; ++i) {
int flag = 0;
for (int j = 2; j < begin; ++j) {
if (i % j == 0 && j != 2) {
flag = 1;
break;
}
}
if (flag == 0) {
System.out.println(i);
}
}
}
public void test3_get_num_of_daffodils(int begin, int end) {
assert (end >= begin && begin >= 100 && end <= 999);
for (int i = begin; i <= end; ++i) {
int h = i / 100;
int t = (i - h * 100) / 10;
int n = i % 10;
if (h * h * h + t * t * t + n * n * n == i) {
System.out.println(i);
}
}
}
public void test4_analyize_string() {
int n_num = 0;
int n_char = 0;
int n_other = 0;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
String line = br.readLine();
assert (line.length() > 0);
for (int i = 0; i < line.length(); ++i) {
char c = line.charAt(i);
if ('0' <= c && c <= '9') {
n_num++;
} else if (('a' <= c && c < 'z') || ('A' <= c && c <= 'Z')) {
n_char++;
} else {
n_other++;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.printf("n_num %d, n_char %d, n_other %d", n_num, n_char,
n_other);
}
public void test5_count_length(int times) {
// 一个球从100米落地,反弹后至一半高度;第十次落地后,运动距离?反弹多高?
assert (times > 0);
float height = 100;
float length = 0;
for (int t = 0; t < times; ++t) {
length += 2 * height;
height = height / 2;
}
length -= 100;
System.out.printf("length is %f, height is %f\n", length, height);
}
public void test6_print_prismatic(int len) {
assert (len > 2 && len % 2 == 1);
int num_star = 0;
int num_space = 0;
for (int i = 0; i < len; ++i) {
if (i <= len / 2) {
num_star = i * 2 + 1;
num_space = (len - num_star) / 2;
for (int j = 0; j < num_space; ++j) {
System.out.print(" ");
}
for (int k = 0; k < num_star; ++k) {
System.out.print("x");
}
} else {
int _i = len - i - 1;
num_star = _i * 2 + 1;
num_space = (len - num_star) / 2;
for (int j = 0; j < num_space; ++j) {
System.out.print(" ");
}
for (int k = 0; k < num_star; ++k) {
System.out.print("x");
}
}
System.out.println("");
}
}
/*
* ^ y
* |
* | 81
* | 49
* 21 22 23 24 25 26
* 20 7 8 9 10
* 19 6 1 2 11 ----->x
* 18 5 4 3 12
* 17 16 15 14 13
*
*
* 2 1 0 1 2 3 4 scale
*/
public int test7_print_number(int x, int y) {
int val = 1;
if (x == 0 && y == 0)
val = 1;
// value should be less equal than (scale*2 + 1)*(scale*2 + 1);
int scale = Math.abs(x) > Math.abs(y) ? Math.abs(x) : Math.abs(y);
int max_val = (scale * 2 + 1) * (scale * 2 + 1);
if (Math.abs(y) >= Math.abs(x)) {
if (y >= 0) {
val = max_val - (scale - x);
} else {
val = max_val - 2*(scale * 2 + 1 - 1) - (scale + x);
}
} else if (Math.abs(x) >= Math.abs(y)) {
if (x >= 0) {
val = max_val - 3 * (scale * 2 + 1 - 1) - (scale + y);
} else {
val = max_val - (scale * 2 + 1 -1) - (scale - y);
}
}
return val;
}
public void test8_bubble_sort(int array[]) {
int len = array.length;
assert(len > 0);
int temp = array[0];
for (int i = 0; i < len; ++i) {
for (int j = 0; j < len - i - 1; ++j) {
if (array[j] > array[j+1]) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
/*
* Today is Tuesday, 3.12 2013,
*
* if month == 7, print calendar like this:
*
* San Mon Tue Wed Thu Fri Sat
* 1 2 3 4 5 6
* 7 8 9 10 11 12 13
* 14 15 16 17 18 19 20
* 21 22 23 24 25 26 27
* 28 29 30 31
*/
public void test9_print_calendar(int month) {
final String MONTH[] = { "January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December" };
int days[] = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int _d[] = new int[]{-1, -1, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1};
assert (month >= 1 && month <=12);
int index = 0;
for (;index < 12; ++index) {
if (_d[index] > 0) {
break;
}
}
for (int i = 0; i < 12; ++i) {
if (i < index) {
int all_days_before_index = 0;
for (int j = i; j < index; ++j) {
all_days_before_index += days[j];
}
//System.out.println(all_days_before_index);
_d[i] = ( 7 + _d[index] - all_days_before_index % 7) % 7;
} else if (i > index) {
int all_days_after_index = 0;
for (int j = index; j < i; ++j) {
all_days_after_index += days[j];
}
//System.out.println(all_days_after_index);
_d[i] = (all_days_after_index % 7 + _d[index]) % 7;
}
}
// print calendar
System.out.println(MONTH[month - 1]);
// 1th of month is _d[month - 1]
System.out.printf("%4s%4s%4s%4s%4s%4s%4s\n", "San", "Mon", "Tue",
"Wed", "Thu", "Fri", "Sat");
for (int j = 1; j <= days[month - 1]; ++j) {
if (j == 1) {
for (int s = 0; s < _d[month - 1]; ++s) {
System.out.print(" ");
}
}
System.out.printf("%4d", j);
if ((j + _d[month - 1]) % 7 == 0) {
System.out.println();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloWorld hello = new HelloWorld();
System.out.print("\n------------------------------------------\n");
// test 1
for (int m = 0; m < 10; ++m) {
System.out.printf("%d month, %d rabbits\n", m,
hello.test1_total_num_of_rabbits(m));
}
System.out.print("\n------------------------------------------\n");
// test2
hello.test2_get_prime_num(100, 200);
System.out.print("\n------------------------------------------\n");
// test3
hello.test3_get_num_of_daffodils(100, 999);
System.out.print("\n------------------------------------------\n");
// test4
// hello.test4_analyize_string();
System.out.print("\n------------------------------------------\n");
// test5
hello.test5_count_length(4);
System.out.print("\n------------------------------------------\n");
// test6
hello.test6_print_prismatic(11);
System.out.print("\n------------------------------------------\n");
// test7
System.out.println(hello.test7_print_number(0, 0));
System.out.println(hello.test7_print_number(2, 2));
System.out.println(hello.test7_print_number(0, 3));
System.out.println(hello.test7_print_number(-2, 0));
System.out.println(hello.test7_print_number(-3, -3));
System.out.println(hello.test7_print_number(0, -3));
System.out.println(hello.test7_print_number(3, 0));
System.out.print("\n------------------------------------------\n");
// test8
int temp[] = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
hello.test8_bubble_sort(temp);
for (int a:temp) {
System.out.printf(" %d", a);
}
System.out.print("\n------------------------------------------\n");
// test8
hello.test9_print_calendar(7);
}
}
结果:
------------------------------------------
0 month, 2 rabbits
1 month, 2 rabbits
2 month, 2 rabbits
3 month, 4 rabbits
4 month, 6 rabbits
5 month, 8 rabbits
6 month, 12 rabbits
7 month, 18 rabbits
8 month, 26 rabbits
9 month, 38 rabbits
------------------------------------------
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
------------------------------------------
153
370
371
407
------------------------------------------
------------------------------------------
length is 275.000000, height is 6.250000
------------------------------------------
x
xxx
xxxxx
xxxxxxx
xxxxxxxxx
xxxxxxxxxxx
xxxxxxxxx
xxxxxxx
xxxxx
xxx
x
------------------------------------------
1
25
46
19
37
34
28
------------------------------------------
1 2 3 4 5 6 7 8 9
------------------------------------------
July
San Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31