总结以前去公司面试出的算法题,大致有这几类:一个for循环,两个for循环,排序,比较大小,递归,等等。希望对找工作的人有所帮助,面试题有的我到现在没弄明白呢!等我弄明白后,也会贴出来分享的!
1、写一个方法,要求:输入一个字符串ABCDEFG,要求倒序输出GFEDCBA:
- package com.cn.test;
-
- public class StringDaoXu { public String formatString(String s){
-
-
- for(int i=s.length()-1;i>=0;i--){
- System.out.print(s.charAt(i));
- }
- return s;
- }
-
-
-
-
- public static void main(String[] args) {
-
- StringDaoXu sdx = new StringDaoXu();
- sdx.formatString("abcdefghi");
- }
- }
- package com.cn.test;
-
- public class StringDaoXu { public String formatString(String s){
-
-
- for(int i=s.length()-1;i>=0;i--){
- System.out.print(s.charAt(i));
- }
- return s;
- }
-
-
-
-
- public static void main(String[] args) {
-
- StringDaoXu sdx = new StringDaoXu();
- sdx.formatString("abcdefghi");
- }
- }
运行结果如图:
2、有一个三位数,个位是c,十位是b,百位是a,
如上运算,求满足这种的三位数有几种情况?
- public class MoberTest1 {
-
-
-
-
- public static void main(String[] args) {
-
- 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();
- }
- }
- }
- }
- public class MoberTest1 {
-
-
-
-
- public static void main(String[] args) {
-
- 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();
- }
- }
- }
- }
运行结果如下图:
3、有一组数,求这组数的最大数和最小数的绝对值是多少?
- package com.cn.test;
-
- public class MaxAndMin {
-
- public int ChaZhi(int[] in){
- int temp = 0;
- for(int i=0;i
- for(int j=0;j
- if(in[i]>in[j]){
- temp = in[i];
- in[i] = in[j];
- in[j] = temp;
- }
- }
- }
-
- return Math.abs(in[0] - in[in.length-1]);
- }
-
-
-
- public static void main(String[] args) {
-
- int[] in = {10,20,30,40,50,20,33,80};
- MaxAndMin mam = new MaxAndMin();
- mam.ChaZhi(in);
- System.out.println(mam.ChaZhi(in));
- }
- }
- package com.cn.test;
-
- public class MaxAndMin {
-
- public int ChaZhi(int[] in){
- int temp = 0;
- for(int i=0;i
- for(int j=0;j
- if(in[i]>in[j]){
- temp = in[i];
- in[i] = in[j];
- in[j] = temp;
- }
- }
- }
-
- return Math.abs(in[0] - in[in.length-1]);
- }
-
-
-
- public static void main(String[] args) {
-
- int[] in = {10,20,30,40,50,20,33,80};
- MaxAndMin mam = new MaxAndMin();
- mam.ChaZhi(in);
- System.out.println(mam.ChaZhi(in));
- }
- }
运行结果如下图:
4、打印九九乘方表:
- package com.cn.test;
-
- public class 九九乘法 {
-
- 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();
- }
- }
- }
- package com.cn.test;
-
- public class 九九乘法 {
-
- 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();
- }
- }
- }
运行结果如下图:
5、利用递归求一个数的阶乘?
- package com.cn.test;
-
- public class DiGuiText {
-
-
-
-
- public static void main(String[] args) {
-
- DiGuiText dg = new DiGuiText();
- System.out.println("10的阶乘为:"+dg.diGui(10));
- }
- public long diGui(int n){
- if(n==1){
- return 1;
- }else{
- return n*diGui(n-1);
- }
- }
- }
- package com.cn.test;
-
- public class DiGuiText {
-
-
-
-
- public static void main(String[] args) {
-
- DiGuiText dg = new DiGuiText();
- System.out.println("10的阶乘为:"+dg.diGui(10));
- }
- public long diGui(int n){
- if(n==1){
- return 1;
- }else{
- return n*diGui(n-1);
- }
- }
- }
结果如下图:这个是6的介乘: