1.打印“水仙花數(shù)”(自己的練習(xí))
所謂"水仙花數(shù)"是指一個(gè)三位數(shù),其各位數(shù)字立方和等于該數(shù)本身。
public class DaffodilNum {
public static void main(String[] args) {
for(int i = 1; i < 10; i++) {
for(int j = 0; j < 10; j++) {
for(int z = 0; z < 10; z++) {
int num = i*100 + j*10 +z;
if(i*i*i + j*j*j + z*z*z == num) {
System.out.println(num);
}
}
}
}
}
}
2.兔子問(wèn)題(自己的練習(xí))有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長(zhǎng)到第三個(gè)月后每個(gè)月又生一對(duì)兔子,假如兔子都不死,問(wèn)某個(gè)月的兔子總數(shù)為多少?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Hare {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int month = 0;
try {
month = Integer.parseInt(reader.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int[] sum = new int[month];
sum[0] = 1;
sum[1] = 1;
for(int i = 2; i < month; i++) {
sum[i] = sum[i - 1] + sum[i - 2];
}
System.out.println(Arrays.toString(sum));
}
}
3.拆詞問(wèn)題(某公司筆試題)
將一段英文(標(biāo)點(diǎn)只含有句號(hào)和逗號(hào))拆分成單詞,并打印把每個(gè)詞出現(xiàn)的頻率按從大到小的順序打印出來(lái)
public class Statistics {
public static void main(String[] args) {
String source = "Both sides offered statistics to bolster their arguments." +
"She's studying statistics at university.";
Map<String, Integer> map = new HashMap<String, Integer>();
Set<String> set = new HashSet<String>();
String temp = "";
int length = source.length();
for(int i = 0; i < length; i++) {
String currentChar = source.substring(i, i+1);
if(" ".equals(currentChar) || ",".equals(currentChar) || ".".equals(currentChar)) {
int sizeOld = set.size();
set.add(temp);
if(set.size() == sizeOld && map.size() > 0) {
map.put(temp, map.get(temp) + 1);
} else {
map.put(temp, 1);
}
temp = "";
} else {
temp = temp + currentChar;
}
}
Integer[] values = map.values().toArray(new Integer[set.size()]);
for(int m = 0; m < values.length; m++) {
int tempNum = values[m];
for(int n = m + 1; n < values.length; n++) {
if(tempNum < values[n]) {
values[m] = values[n];
values[n] = tempNum;
}
}
}
System.out.println(Arrays.toString(values));
}
如果有一天,我們真的吵架了,也許到時(shí)候還能嗅到這甜甜的味道,說(shuō):“還應(yīng)該再煮一點(diǎn)咖啡。”