Classic example 1, add the opposite numbers

To get the "opposite number" of a number, we reverse the numerical order of the number, and then add the original number to get the "opposite number". For example, to get the "opposite number" of 1325, first we reverse the digit order of the number, we get 5231, then add the original number, we get 5231+1325=6556. If the reversed number is prefixed with zero, Prefixed zeros will be ignored. For example, n = 100, which is 1 after inversion.

The difference between String and StringBuffer is scalability

 

    public static void main(String[] args) throws Exception {
        String a = "1234";
        StringBuffer d=new StringBuffer();
        char b[] = a.toCharArray();
        for(int i=b.length-1;i>=0;i--){        
	   String c = String.valueOf(b[i]);			
	   d.append(c);			 
        }
        int m=Integer.valueOf(d.toString());
        int n=m+Integer.valueOf(a);
        System.out.println(n);
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324688741&siteId=291194637