java題目

package com.test;

import java.util.Arrays;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

public class Sort
{
    /**
     * 根據以下要求,比較兩個字符串的大小,並返回比較結果:
     * 1、比較兩字符串的大小。 2、忽視大小寫
     *  3、 按字典序 如果第一個字符串大於第二個字符串 返回大於0,
     * 如果第一個字符串等於第二個字符串 返回等於0
     * ,如果第一個字符串小於第二個字符串返回小於0。
     * 4、例子 compareToIgnoreCase(“HARD”,”hark”)的比較結果返回小於0 。
     *
     * @param str1
     * @param str2
     * @return
     * @see [類、類#方法、類#成員]
     */
    public static int  sortArray(String str1, String str2)
    {
       String str1Temp = str1.toLowerCase();
       String str2Temp = str2.toLowerCase();

       if(str1.compareToIgnoreCase(str2) > 0)
       {
           return 1;
       }else if(str1.compareToIgnoreCase(str2) < 0)
       {
           return -1;
       }else
       {
           return 0;
       }

    }

    /**
     * 把數組中的1-26的數字映射成a-z的小寫字母 如果輸入其他數字,則在頁面上打印"?"
     * 如:int [] arr={1,2,3,4,30} 輸出:a,b,c,d,?
     *
     * @param a
     * @return
     * @see [類、類#方法、類#成員]
     */
    public static String conver(int[] a)
    {
        String goal = "";
        for(int i= 0; i < a.length;i++)
        {
            if(a[i] > 26 || a[i] <1)
            {
                goal = goal +"?,";
            }
            else
            {
                int temp = a[i]+'a'-1;
                char str = (char)(temp);
                goal = goal+str+",";
            }
        }
        return goal.substring(0, goal.length()-1);
    }

    /**
     * 輸入A-Z26個字母,輸入一個大寫字母后,輸出該字母之後第5個字母的小寫。
     *  如輸入A,輸出f…… 輸入Z,則輸出e。 超出Z時,超過1個,
     *  則返回a,超過兩個,則返回b,以此類推。
     */
    public static char converTochar(char chars)
    {
        if (chars >= 'A' && chars <= 'Z')
        {
            if ((chars + 5) > 90)
            {
                return (char)(chars - 85 + 'a' - 1);
            }
            else
            {
                return (char)(chars + 37);
            }

        }    
            return chars;
    }

    /**
     * 判斷一個字符串是否是首字母大寫,其餘字母都是小寫。
     *  例如 輸入:True 輸出: true
     */



    /**
     * 給一個二維數組inArr[ ][ ],寫一個方法獲取每一列的最小值,輸出到一個一維數組outArr[ ]中。
     *  如:inArr[ ][ ]={{1,8,3},{6,5}},則輸出outArr[ ] = {1,5,3}
     */
    public static int[] findMin(int[][] inArr)
    {
        int maxLength = 0;
        for (int i = 0; i < inArr.length; i++)
        {
            if (inArr[i].length > maxLength)
            {
                maxLength = inArr[i].length;
            }
        }
        int[] outArr = new int[maxLength];
        for (int column = 0; column < maxLength; column++)
        {
            outArr[column] = Integer.MAX_VALUE;
            for (int row = 0; row < inArr.length; row++)
            {
                if (column < inArr[row].length)
                {
                    if (inArr[row][column] < outArr[column])
                    {
                        outArr[column] = inArr[row][column];
                    }
                }
            }
        }
        return outArr;
    }

    /**
     * 實現數組倒置算法: 如一數組:1,2,3 4,5,6 則倒置後:1,4 2,5 3,6
     */
    public static int[][] goal(int[][] arrays)
    {
        int[][] goal = new int[arrays[0].length][arrays.length];
        for (int i = 0; i < arrays.length; i++)
        {
            for (int j = 0; j < arrays[0].length; j++)
            {
                goal[j][i] = arrays[i][j];
            }
        }
        return goal;
    }
    /**
     * 輸入一個字符串,字符串是字母和數字的組合,編程實現輸出一個新的字符串,
     * 要求字母在前面,
     * 數字在後面,順序不變,例如:2s7ess83a 變成sessa2783
     */
    public static String splits(String str)
    {
        String strings = "";
        String numbers ="";
        for(int i = 0;i < str.length();i++)
        {
            if((str.charAt(i)>= 'A' && str.charAt(i) <='Z')||
                (str.charAt(i)>= 'a' && str.charAt(i) <='z'))
            {
                strings = strings + str.charAt(i);
            }else
            {
                numbers = numbers +str.charAt(i);
            }
        }
        return strings+numbers;
    }
   /**
    * 一個字符串,獲取最長的一個單詞,如有多個相同長度的單詞返回第一個單詞。
    * 入輸入:“hello china”則返回 hello
    */
    public static String getString(String str)
    {
        String[] strings = str.split(" ");
        String tempMaxLenth = strings[0];
        for(int i = 1;i < strings.length;i++)
        {
            if(tempMaxLenth.length() < strings[i].length())
            {
                tempMaxLenth = strings[i];
            }
        }
        return tempMaxLenth;

    }
/**
 * 迴文
 *
 */

    public static int huiwen(int str[])
    {
    int i,len,k=1;
    len=str.length;
   // len=str.len(str);
    for(i=0;i<len/2;i++)
    {
    if(str[i]!=str[len-i-1])
    {
    k=0;
    break;
    }
    }
    if(k==0)
   System.out.println(Arrays.toString(str) +"不是一個迴文數");
    else
        System.out.println(Arrays.toString(str)+"是一個迴文數");
    return 0;
    }


/**
 * 輸入一個字符串,字符串是字母和數字的組合,編程實現輸出一個新的字符串,
 * 要求字母在前面,數字在後面,順序不變,例如:2s7ess83a 變成sessa2783
 */

    public static String getString(String a)
    {
        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();

        char arr[] = a.toCharArray();

        for (int i = 0; i < arr.length; i++)
        {
            int temp = Integer.valueOf(arr[i]);

            if ('0' <= temp && temp <= '9')
            {
                sb2.append(arr[i]);
            }
            else
            {
                sb1.append(arr[i]);
            }
        }

        return sb1.append(sb2).toString();
    }


    /**
     *將一個字符裏出現最多的字母截取,如,addcbbs變爲acs。
     */

方法1:
   public static void main(String args[])
    {
    String str="213sdf452346dfhb";

    String temp=“”;

char[] c=str.toCharArray();

    for(int i=0; i<c.length;i++)
    {
        if(temp.indexOf(c[i])==-1)
        {
        temp+=c[i];
        
        }
    }
    System.out.println("去重後的字符竄爲:"+temp);
    }
    

方法2:
    public static String splits(String str)
        {
            String strings = "";
            String numbers = "";
            for (int i = 0; i < str.length(); i++)
            {
                if ((str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') ||
                    (str.charAt(i) >= 'a' && str.charAt(i) <= 'z'))
                {
                    strings = strings + str.charAt(i);
                }
                else
                {
                    numbers = numbers + str.charAt(i);
                }
            }
            return strings + numbers;
        }

        public static String subStrings(String str)
        {
            String goals = "";
            Set set = new HashSet<String>();
            List<String> goalList = new ArrayList<String>();
            for (int i = 0; i < str.length(); i++)
            {
                set.add(str.charAt(i) + "");
                goalList.add(str.charAt(i)+"");
            }
            // String[] goal = (String[])set.toArray();
            String[] goal = (String[])set.toArray(new String[0]);
            // char[] goalt = (char[])set.toArray();//(new char[0]);
            int[] count = new int[goal.length];


            for (int i = 0; i < goal.length; i++)
            {
                int tempCount = 0;
                for (int j = 0; j < str.length(); j++)
                {
                    if (goal[i].equals(str.charAt(j)+""))
                    {
                        tempCount++;
                    }
                }
                count[i] = tempCount;
            }
            int countMax = count[0];
            List<Integer> MaxCount = new ArrayList<Integer>();
            for (int i = 1; i < count.length; i++)
            {
                if (count[i] > countMax)
                {
                    countMax = count[i];
                }
            }

            List<String> goalListRemove = new ArrayList<String>();
            for (int i = 0; i < count.length; i++)
            {
                if (count[i] == countMax)
                {
                    goalListRemove.add(goal[i]);
                    MaxCount.add(i);
                }
            }

            for (Iterator it = goalList.iterator(); it.hasNext();)
            {
                String a = (String)it.next();

                if(goalListRemove.contains(a))
                {
                    it.remove();
                }
            }


            return goalList.toString();
        }




   //c[i]= a[i]+b[b.lenght()-i-1];
    public static void main(String args[])
    {

//        int [] arr={1,2,3,4,30,8};
//        Arrays.sort(arr);
//        System.out.print(Arrays.toString(arr));
//
//
//        char temp = 'A';
//       System.out.println(Sort.isHope("TruE"));
//
//       System.out.println(Arrays.deepToString(Sort.goal(arrays)));

        charstr[] a="12345654321";
        a.
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章