close

此篇是製作字串顛倒輸出的函式,並介紹String的函式功能

package p004_030519;

public class c001 {
    public static String Inverse(String s) /*字串顛倒輸出的函式*/
    {
        String z="";
        int k = s.length(); /*s.length()是傳回s字串的長度*/
        for(int i=0;i<k;i++)
        {
            z = s.substring(i,i+1) + z; /*s.substring(A, B) 是傳回 s字串在A~B之間的字串(不包含B)*/
        }
        return z;
    }
   
    public static void main(String[] a)
    {
        int x=1234;
        String s="",z="";/*z is s' reverse string*/
        s = String.valueOf(x);
        
        z = Inverse(s);
        
        System.out.println("s= "+s);
        System.out.println("z= "+z);
    }
}


/*以下是支前的四位數問題:找尋四位數中乘上的積 是某數的顛倒。此為以字串法解題*/

package p004_030519;

public class c002 {
    public static void main(String[] a)
    {   
        int x=0,y=0,z=0; /* x*y=z, z=reverse(x)*/
        String s="", t="";
        
        for(int i=1000;i<=9999;i++)
        {
            x = i;
            s = String.valueOf(x);
            t = fun.Inverse(s); 將Inverse放入fun
            z = Integer.parseInt(t); 
            for( y=2;y<10;y++)
            {
                if(x*y==z){System.out.println(x+"*"+y+"="+z);}
            }
        }
    }
}

arrow
arrow
    創作者介紹
    創作者 Kuihao 的頭像
    Kuihao

    溫暖午後的金針田__孕育有趣的創新

    Kuihao 發表在 痞客邦 留言(0) 人氣()