Java学习---程序设计面试题[2]

百度2017春招笔试真题编程题集合之买帽子

 1 //    2017-10-09
 2 //     题目描述
 3 //    度度熊想去商场买一顶帽子,商场里有N顶帽子,有些帽子的价格可能相同。度度熊想买一顶价格第三便宜的帽子,问第三便宜的帽子价格是多少?
 4 //    输入描述:
 5 //    首先输入一个正整数N(N <= 50),接下来输入N个数表示每顶帽子的价格(价格均是正整数,且小于等于1000)
 6 //    输出描述:
 7 //    如果存在第三便宜的帽子,请输出这个价格是多少,否则输出-1
 8 //    输入例子:
 9 //    10
10 //    10 10 10 10 20 20 30 30 40 40
11 //    输出例子:
12 //    30
13 
14 -------------------------------------------------
15 package com.huawei.test;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Scanner;
19 
20 /**
21  输入描述:
22     首先输入一个正整数N(N <= 50),接下来输入N个数表示每顶帽子的价格(价格均是正整数,且小于等于1000)
23 输出描述:
24     如果存在第三便宜的帽子,请输出这个价格是多少,否则输出-1
25 输入例子:
26     10
27     10 10 10 10 20 20 30 30 40 40
28 输出例子:
29     30
30 */
31 public class Test {
32     public static void main(String[] args){
33         Scanner scan = new Scanner(System.in);
34         while(scan.hasNext()){
35             //输入一个正整数
36             int n = scan.nextInt();
37             //开辟空间
38             int[] prices = new int[n];
39             //读取价格
40             for(int i = 0; i < prices.length; i++){
41                 prices[i] = scan.nextInt();
42             }
43             //价格排序
44             Arrays.sort(prices);
45             ArrayList<Integer> list = new ArrayList<>();
46             //去重复
47             for(int i = 0;i < n; i++){
48                 if(!list.contains(prices[i])){
49                     list.add(prices[i]);
50                 }
51             }
52             if(list.size()<3){
53                 System.out.println(-1);
54             }else{
55                 System.out.println(list.get(2));
56             }
57             
58         }
59     }
60 }
View Code

寻找三角形

  1 /**
  2  * 题目描述
  3  * 
  4  * 三维空间中有N个点,每个点可能是三种颜色的其中之一,三种颜色分别是红绿蓝,分别用'R', 'G', 'B'表示。
  5  * 现在要找出三个点,并组成一个三角形,使得这个三角形的面积最大。 
  6  * 但是三角形必须满足:三个点的颜色要么全部相同,要么全部不同。
  7  * 输入描述: 首先输入一个正整数N三维坐标系内的点的个数.(N <= 50) 接下来N行 
  8  * 每一行输入 c x y z,c为'R', 'G', 'B'的其中一个。
  9  * x,y,z是该点的坐标。(坐标均是0到999之间的整数)
 10  * 输出描述: 输出一个数表示最大的三角形面积,保留5位小数。 
 11  * 输入例子:
 12  *         5 
 13  *         R 0    0 0
 14  *         R 0 4 0 
 15  *         R 0 0 3 
 16  *        G 92 14 7
 17  *        G 12 16 8 
 18  * 输出例子: 6.00000
 19  */
 20 package com.ftl.test;
 21 import java.util.ArrayList;
 22 import java.util.Arrays;
 23 import java.util.Scanner;
 24 public class Test {
 25     public static void main(String[] args) {
 26         Scanner sc = new Scanner(System.in);
 27         // 输入一个正整数N
 28         System.out.println("Please input the Num:");
 29         int n = sc.nextInt();
 30         // N行 N 个数组
 31         String[] nums = new String[n];
 32         // 初始化
 33         double temp = 0.0;
 34         double area = Double.MAX_VALUE;
 35         // 输入N行,用数组保存
 36         for (int i = 0; i < n; i++) {
 37             nums[i] = sc.nextLine();
 38         }
 39         // 获取颜色
 40         char[] colors = new char[n];
 41         for (int i = 0; i < n; i++) {
 42             colors[i] = nums[i].split(" ")[0].charAt(0);
 43         }
 44         // 获取X轴
 45         int[] x = new int[n];
 46         for (int i = 0; i < n; i++) {
 47             x[i] = Integer.parseInt(nums[i].split(" ")[1]);
 48         }
 49         // 获取Y轴
 50         int[] y = new int[n];
 51         for (int i = 0; i < n; i++) {
 52             x[i] = Integer.parseInt(nums[i].split(" ")[2]);
 53         }
 54         // 获取Z轴
 55         int[] z = new int[n];
 56         for (int i = 0; i < n; i++) {
 57             x[i] = Integer.parseInt(nums[i].split(" ")[3]);
 58         }
 59         // 进行判断 三个点的颜色要么全部相同,要么全部不同
 60         for (int i = 0; i < n; i++) {
 61             for (int j = i + 1; j < n; j++) {
 62                 for (int k = j + 1; k < n; k++) {
 63                     if (colors[i] == colors[j] && colors[j] == colors[k]
 64                             || colors[i] != colors[j] && colors[j] != colors[k]
 65                             && colors[k] != colors[i]) {
 66                         if (colors[i] == colors[j] && colors[j] == colors[k]
 67                                 || colors[i] != colors[j]
 68                                 && colors[j] != colors[k]
 69                                 && colors[i] != colors[k]) {
 70                             double a = Math
 71                                     .pow(Math.pow(Math.abs(x[i] - x[j]), 2)
 72                                             + Math.pow(Math.abs(y[i] - y[j]), 2)
 73                                             + Math.pow(Math.abs(z[i] - z[j]), 2),
 74                                             0.5);
 75                             double b = Math
 76                                     .pow(Math.pow(Math.abs(x[i] - x[k]), 2)
 77                                             + Math.pow(Math.abs(y[i] - y[k]), 2)
 78                                             + Math.pow(Math.abs(z[i] - z[k]), 2),
 79                                             0.5);
 80                             double c = Math
 81                                     .pow(Math.pow(Math.abs(x[k] - x[j]), 2)
 82                                             + Math.pow(Math.abs(y[k] - y[j]), 2)
 83                                             + Math.pow(Math.abs(z[k] - z[j]), 2),
 84                                             0.5);
 85 
 86                             double p = (a + b + c) / 2;
 87                             // 海伦公式求空间三角形面积
 88                             temp = Math.pow(p * (p - a) * (p - b) * (p - c),
 89                                     0.5);
 90                             if (area < temp) {
 91                                 area = temp;
 92                             }
 93                         }
 94                     }
 95                 }
 96             }
 97         }
 98         System.out.printf("%.2f",area);
 99     }
100 }
View Code

输出不同的元素的个数

 1 package com.ftl;
 2 /** 
 3  *  输入字符串长度len1,字符串s1,字符串长度len2,字符串s2。
 4  *  从后向前比较,以最短字符串为标准,输出不同的元素的个数。 
 5  *  例如:  输入:s1="1,3,5"   len1=3        s2="2,4,1,7,5"   len2=5   
 6            输出:2   
 7        函数原型 public int getDiffNum(int len1, String s1, int len2, String s2) 
 8  * 
 9  * @author ftl 
10  */ 
11 
12 public class HWText {
13     public static void main(String[] args){
14          String s1="1,3,5";  
15          int len1=3;  
16          String s2="2,4,2,7,6";  
17          int len2=5;  
18          int diffNum= getDiffNum(s1,len1,s2,len2);  
19          System.out.println("不同的数目:"+diffNum); 
20     }
21     
22     public static int getDiffNum(String s1,int len1, String s2, int len2){
23         int num = 0;
24         //字符串转换为字符数组
25         String[] str1 = s1.split(",");
26         String[] str2 = s2.split(",");
27         //以最端的字符串为标准(换言之,就是确定循环的次数)
28         int len = len1<len2?len1:len2;
29         for (int i = 0; i < len; i++) {
30             //判断是否相等
31             if(!str1[len1-1-i].equals(str2[len2-1-i])){
32                 num++;
33             }
34         }
35         return num;
36     }
37 }
View Code

混合运算

 1 package com.ftl;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 /** 
 7  输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。
 8             例如:  输入:3+8×2/9-2  
 9             输出:2 
10  * @author ftl 
11  */ 
12 
13 public class HWText {
14     public static void main(String[] args){
15         String s="3+8×2/9-2 ";  
16         int result=getMyRet(s);  
17         System.out.println("最后结果:"+result); 
18     }
19     
20     public static int getMyRet(String s){
21         int len = s.length();
22         List<String> list = new ArrayList<>();
23         //字符串变成集合
24         for(int i = 0; i < len; i++){
25             list.add(s.charAt(i)+"");
26         }
27         System.out.println("list--->"+ list);//list--->[3, +, 8, ×, 2, /, 9, -, 2,  
28         for(int i = 0; i<list.size(); i++){
29             if(list.get(i).equals("x")){
30                 //计算乘法
31                 int ji = Integer.parseInt(list.get(i-1))*Integer.parseInt(list.get(i+1));
32                 //将积放在集合中,整个位置都后移一位,8现在是i的位置
33                 list.add(i-1,ji+"");
34                 //删除原来的位置
35                 list.remove(i);  //删除8
36                 list.remove(i);  //删除*
37                 list.remove(i);  //删除2
38                 System.out.println("list--x后->"+ list);//list--x后->[3, +, 16, /, 9, -, 2,  ]
39                 //回到积的位置,再次参与运算
40                 i--;  
41             }else if(list.get(i).equals("/")){
42                 int shang = Integer.parseInt(list.get(i-1))/Integer.parseInt(list.get(i+1));
43                 list.add(i-1,shang+"");
44                 list.remove(i); //删除16
45                 list.remove(i); //删除/
46                 list.remove(i); //删除9
47                 System.out.println("list--/后->"+ list);//list--/后->[3, +, 1, -, 2,  ] 
48                 i--;              //回到商的位置
49             }
50         }
51         //新的list,只有+-,注意用新的变量k,否则会编译报错            
52          for(int k=0;k<list.size();k++){//这个时候是新的size  
53              if(list.get(k).equals("+")){  
54                  int he=Integer.parseInt(list.get(k-1))+Integer.parseInt(list.get(k+1));  
55                  list.add(k-1,he+"");   
56                  list.remove(k);   
57                  list.remove(k);   
58                  list.remove(k);   
59                  System.out.println("list--+后->"+ list); //list--+后->[4, -, 2,  ]  
60                 k--;   
61              }  
62              if(list.get(k).equals("-")){  
63                  int cha=Integer.parseInt(list.get(k-1))-Integer.parseInt(list.get(k+1));  
64                  list.add(k-1,cha+"");   
65                  list.remove(k);   
66                  list.remove(k);   
67                  list.remove(k);   
68                  System.out.println("list--  -后->"+ list); //list--  -后->[2,  ]  
69                 k--;  
70              }  
71         }
72         return Integer.parseInt(list.get(0));
73     }
74 }
View Code

二叉树算法描述

1 二叉树的遍历是指按照某条搜索路径访问树中的每个节点,使得每个节点的均只被访问一次。由二叉树的递归定义,遍历一棵二叉树便要决定对根节点、左子树和右子树的访问顺序。
2 常见的遍历次序有先序遍历、中序遍历和后序遍历。其中序指的是根节点在何时被访问。
3 先序遍历(PreOrder):若二叉树非空,则先访问根节点,再访问左子树,最后访问右子树。
4 中序遍历(InOrder):若二叉树非空,则先访问左子树,再访问根节点,最后访问右子树。
5 后序遍历(PostOder):若二叉树非空,则先访问左子树,再访问右子树,最后访问根节点。
View Code

 统计字符串中某个字符出现的次数

 1 package com.ftl;
 2 
 3 public class HelloFtl {
 4     public static void main(String[] args) {
 5         // TODO 自动生成的方法存根
 6         System.out.println("----------------------------------------");
 7         String str = "want you know one thing";
 8         int count = 0;
 9         int count1 = 0;
10         char[] c = str.toCharArray();
11         for ( int i = 0; i < c.length; i++)
12         {
13             if (c[i] == 'o')
14             {
15                 count++;
16             }
17             if (c[i] == 'n')
18             {
19                 count1++;
20             }
21         }
22         System.out.println("O出现的次数为:" + count);
23         System.out.println("n出现的次数为:" + count1);
24         System.out.println("----------------------------------------");
25         int c1 = 0;
26         while ( str.indexOf("n") != -1) //indexOf存在则返回所处位置,否则返回-1 
27         {
28             
29             c1++;
30             //改变字符串数量
31             str = str.substring(str.indexOf("n") + 1);
32             System.out.println("Str "
33                     + "为:" + str);
34             
35         }
36         System.out.println("n出现的次数为:" + c1);
37     }
38 
39 }
View Code

简单的登录验证系统

 1 package com.ftl;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 
 7 class Check
 8 {
 9     private int age = 11;
10 
11     // 内部类
12      public boolean validate(String name, String password)
13     {
14          if (name.equals("ftl") && 
15                  password.equals("1012"))
16          {
17              return true;
18          }
19          else 
20          {
21              return false;
22          }
23              
24     }
25 }
26 
27 class Operate
28 {   
29     private String info[];
30     
31     public Operate(String info[])
32     {
33         this.info = info;
34     }
35     public String login()
36     {
37         Check check = new Check();
38         String name = this.info[0];
39         String password = this.info[1];
40         String str = null; 
41         if (check.validate(name, password))
42         {
43             str = "Welcome " + name.toUpperCase() +" comingg   "; 
44         }
45         else
46         {
47             str = "No Welcome " + name.toUpperCase() +" comingg   ";
48         }
49      return str;   
50     }
51 }
52 
53 public class HelloFtl
54 {
55     public static void main(String args[])
56     {
57         String[] arg = new String[2];
58         BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
59         System.out.println("Input the name and password[换行表示结束]: ") ;
60         for(int i = 0; i < arg.length; i++ ){
61              try
62             {
63                 arg[i] = buf.readLine() ;
64             } catch (IOException e)
65             {
66                 // TODO Auto-generated catch block
67                 e.printStackTrace();
68             }
69         }
70         boolean flag = new Check().validate(arg[0], arg[1]);
71         System.out.println("-------------------------------------------");
72         if(flag){
73             Operate oper = new Operate(arg);
74             System.out.println(oper.login());
75             System.out.println("黄沙百战穿金甲,不破楼兰终不还");
76         }
77        
78     }   
79 }
View Code

猜你喜欢

转载自www.cnblogs.com/ftl1012/p/interview.html