Android interview Java Algorithm question [Android evolution 4]

Source: Internet
Author: User

Summarize the algorithm questions previously interviewed by the company. There are roughly the following types: one for loop, two for loops, sorting, comparison size, recursion, and so on. I hope it will be helpful to people looking for a job. I haven't figured out some of my questions yet! After I understand it, I will post it for sharing!

1. To write a method, enter a string abcdefg and output gfedcba in reverse order:

Package com.cn. test; public class stringdaoxu {Public String formatstring (string s) {// use the for reverse loop, and use the char type method to implement for (INT I = S. length ()-1; I> = 0; I --) {system. out. print (S. charat (I);} return s;}/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub stringdaoxu sdx = new stringdaoxu (); sdx. formatstring ("abcdefghi ");}}

Running result

2. There is a three-digit number. The single digit is C, the ten digits are B, and the hundred digits are,

In the preceding operation, how many situations does the three-digit number meet?

public class MoberTest1 { /**  * @param args  */ public static void main(String[] args) {  // TODO Auto-generated method stub  int b = 1;     for(int a=0;a<=9;a++){      for(int c=0;c<=9;c++){       if(a+c==13){        System.out.print("a="+a+" ");        System.out.print("b="+b+" ");        System.out.print("c="+c+"  ;");        System.out.println();       }      }     } }

The running result is as follows:

3. If there is a set of numbers, what is the absolute value of the maximum number and the minimum number?

package com.cn.test;public class MaxAndMin { public int ChaZhi(int[] in){  int temp = 0;  for(int i=0;i<in.length;i++){   for(int j=0;j<in.length;j++){    if(in[i]>in[j]){     temp = in[i];     in[i] = in[j];     in[j] = temp;    }   }  }//  System.out.println(in[0] - in[in.length-1]);  return Math.abs(in[0] - in[in.length-1]); } /**  * @param args  */ public static void main(String[] args) {  // TODO Auto-generated method stub  int[] in = {10,20,30,40,50,20,33,80};        MaxAndMin mam = new MaxAndMin();        mam.ChaZhi(in);        System.out.println(mam.ChaZhi(in)); }}

The running result is as follows:

4Print the 9-9 multiplication table:

Package com.cn. test; public class jiujiu multiplication {public static void main (string [] ARGs) {for (INT I = 1; I <= 9; I ++) {for (Int J = 1; j <= I; j ++) {system. out. print (J + "*" + I + "=" + I * j + "");} system. out. println ();}}}

The running result is as follows:

5. Use recursion to find the factorial of a number?

Package com.cn. test; public class diguitext {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub diguitext DG = new diguitext (); system. out. println ("10 factorial:" + DG. digui (10);} public long digui (int n) {If (n = 1) {return 1;} else {return N * digui (n-1 );}}}

The result is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.