隨筆-60  評(píng)論-117  文章-0  trackbacks-0
          做好了甜點(diǎn),洗好了甜甜的水果。屋子里甜甜的味道,等待他回家....
          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)咖啡。”

          posted on 2009-08-15 17:47 靜兒 閱讀(1005) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 技術(shù)
          主站蜘蛛池模板: 宜丰县| 沧州市| 会昌县| 陕西省| 曲阜市| 乐清市| 澎湖县| 静安区| 北京市| 苏尼特右旗| 晋宁县| 伽师县| 德格县| 南江县| 盐津县| 广河县| 中西区| 若羌县| 台中市| 民丰县| 巍山| 景泰县| 吴旗县| 施秉县| 剑阁县| 连城县| 台东市| 台山市| 彭泽县| 石景山区| 崇信县| 炎陵县| 新竹县| 卢湾区| 梁平县| 山阳县| 墨玉县| 化德县| 阜宁县| 金乡县| 崇仁县|