<ul id="oy0wm"><pre id="oy0wm"></pre></ul>

          Courage is grace under pressure

          用我一輩子去追求

          導(dǎo)航

          <2006年9月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          統(tǒng)計(jì)

          • 隨筆 - 17
          • 文章 - 1
          • 評論 - 2
          • 引用 - 0

          常用鏈接

          留言簿(43)

          隨筆檔案

          文章檔案

          相冊

          XML的Blog

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          三道簡單算法題

          第一題
          /*
          ?求兩個(gè)字符串的最大公共子串
          ?String s1 = "abcdefghigj";
          ?String s2 = "xyzabcdeigj";
          ?則輸出abcde
          */

          第二題
          /*
          輸出楊輝三角形
          1
          1 1
          1 2 1
          1 3 3 1
          1 4 6 4 1
          1 5 10 10 5 1
          */

          第三題
          /*
          String[] a = {"a","b","c","d","e","f","g","h","i","j","",""};
          String[] b = {"f","a","c","f","e","k","l","y","p","o"};

          如果b里面的數(shù)據(jù)在a里面沒有,則把沒有的數(shù)據(jù)放到""里面,超過的話,則報(bào)"已經(jīng)滿了,無法插入"~~
          */

          解析:
          第一題:
          public class first
          {
          ? public String search(String s1,String s2)
          ? {
          ? String max = "";
          ? for(int i=0; i<s1.length(); i++)
          ? {
          ??? for(int j=i; j<s1.length(); j++)
          ??? {
          ????? String sub = s1.substring(i,j);
          ????? if((s2.indexOf(sub)!=-1)&&sub.length()>max.length())
          ????? {
          ??????? max = sub;
          ????? }
          ??? }
          ? }?
          ? return max;
          ? }
          ?
          ? public static void main(String[] args)
          ? {
          ??? String s1 = "abcdefghigj";
          ??? String s2 = "xyzabcdefigj";
          ??? String output = new first().search(s1,s2);
          ??? System.out.println(output);
          ? }
          }

          第二題:
          public class second
          {
          ? public static int[] general(int[] data)
          ? {
          ??? int[] fanhui = new int[data.length+1];?
          ??? fanhui[0] = data[0];
          ??? for(int i=1,j=1; j<data.length; i++,j++)
          ??? {
          ????? fanhui[i] = data[j-1] + data[j];
          ??? }?????????
          ??? fanhui[fanhui.length-1] = data[data.length-1];
          ??? for(int k=0; k<fanhui.length; k++)
          ??? {
          ????? System.out.print(fanhui[k] + "\t");
          ??? }
          ??? System.out.print("\n");
          ??? return fanhui;
          ? }
          ? public static void main(String[] args)
          ? {
          ??? int times = 5;
          ??? int[] chushizhi = {1};
          ??? System.out.println(chushizhi[0]);
          ??? for(int i=0; i<times; i++)
          ??? {
          ??? chushizhi = second.general(chushizhi);???
          ??? }
          ? }
          }

          第三題:

          import java.util.ArrayList ;

          public class Third
          {
          public static void main(String[] args)
          {
          ??? String[] a = {"a","b","c","d","e","f","g","h","i","j","k","",""};
          ??? String[] b ={"f","a","c","f","e","k","l"};
          ??? Third third = new Third();
          ??? third.compareAndReplace(a,b);
          ??? StringBuffer output = new StringBuffer();
          ??? for(int i=0; i<a.length; i++)
          ??? output.append(a[i]);?
          ??? System.out.println("a已變成" + output.toString());
          ? }
          ?public void compareAndReplace(String[] a,String[] b)
          ?{
          ?? for(int i=0; i<b.length; i++)
          ?? {
          ????? outer:
          ???? for(int j=0; j<a.length; j++)
          ???? {
          ?????? if(b[i].equals(a[j]))
          ?????? break outer;
          ?????? if(j==a.length-1)
          ?????? {
          ?????? if(findFirstSpace(a)!=-1)
          ?????? {
          ???????? a[findFirstSpace(a)] = b[i];
          ?????? }
          ?????? else
          ?????? {
          ???????? System.out.println("已經(jīng)滿了,無法插入" + b[i]);
          ?????? }??????
          ???? }
          ?? }
          ?}
          ?}
          ?
          ?public int findFirstSpace(String[] arg)
          ?{
          ?? for(int m=0; m<arg.length; m++)
          ?? {
          ???? if(arg[m].equals(""))
          ???? return m;????
          ?? }
          ?? return -1;
          ?}
          }

          posted on 2006-09-08 10:40 xyh 閱讀(1601) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          設(shè)為首頁 加入收藏 與我聯(lián)系 您的瀏覽器:

          主站蜘蛛池模板: 鄱阳县| 文成县| 长兴县| 额尔古纳市| 昂仁县| 科尔| 加查县| 九龙县| 香河县| 东莞市| 凉城县| 宁都县| 清远市| 天等县| 嘉义市| 丰顺县| 蕉岭县| 双流县| 民权县| 南康市| 宜丰县| 潜山县| 三门县| 灌南县| 满洲里市| 静乐县| 新疆| 额尔古纳市| 封丘县| 英山县| 民县| 读书| 宣城市| 禄丰县| 宁德市| 武定县| 天祝| 屏南县| 长沙县| 宽甸| 广平县|
        1. <samp id="oy0wm"></samp>
        2. <strike id="oy0wm"></strike>