首页 > 其他 > 详细

2014年华为实习机试题总结

时间:2014-03-22 21:24:27      阅读:408      评论:0      收藏:0      [点我收藏+]

昨天刚从杭州旅游回来,晚上11点到的南京,今天一早起来去华为参加机试,果然犯了很傻*的错误。中午回来之后回忆了没做出来的两道题目(是的,我只出来一道完整的中级题,请鄙视我吧!),花了一个小时做出来,给大家参考一下。

第一题:X个人去吃饭,消费Y元(X,Y为整数输入),其中男人消费3RMB/人,女人2RMB/人,小孩1RMB/人,问:一共有多少种可能的消费组合(小孩,男人,女人至少各一个的组合才算有效组合)?

这题其实很简单,就是循环嵌套,居然。。。当时懵了。。。不多说,说多了都是泪,上代码:

bubuko.com,布布扣
 1 import java.util.Scanner;
 2 
 3 
 4 public class Main {
 5 
 6     /**
 7      * @param args
 8      */
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         Scanner cin = new Scanner(System.in);
12         int x = cin.nextInt();
13         int y = cin.nextInt();
14         int result = 0;
15         
16         if(x < 0 || y < 0){
17             System.out.println(result);
18         }else
19             result++;
20         int a, b, c;
21         for(int i=0; i<=x; i++){
22             a = i;
23             for(int j=0; j<=x; j++){
24                 b = j;
25                 for(int m=0; m<=x; m++){
26                     c = m;
27                     if((a+b+c==x) && (3*a+2*b+c==y)){
28                         result++;
29                     }
30                 }
31             }
32         }
33         System.out.println(result);
34         
35     }
36 }
bubuko.com,布布扣

第三道高级题,其实也简单,思路很清晰,但是楼主又死在了要遍历3^8种所有组合的循环嵌套上(根本没想到这么暴力的方法啊),那一刻楼主仿佛从未学过编程。。。

输入一个整数X,问:“123456789”在数字中间插入“+”或者“-”使这个式子(如1+23-45+6-7-89)的运算结果等于X的组合有多少种?(大概就这意思,具体的表述我也记不清了)例如:输入5,应该输出21

bubuko.com,布布扣
 1 import java.util.Scanner;
 2 
 3 
 4 public class Main {
 5 
 6     /**
 7      * @param args
 8      */
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         Scanner cin = new Scanner(System.in);
12         int x = cin.nextInt();
13         
14         int count = 0;
15         String[] f = {"1", "", "2", "", "3", "", "4", "", "5", "", "6", "", "7", "", "8", "", "9"};
16         String[] op = {"+", "-", ""};
17         String target = "";
18         
19         for(int a=0; a<op.length; a++){
20             f[1] = op[a];
21             for(int b=0; b<op.length; b++){
22                 f[3] = op[b];
23                 for(int c=0; c<op.length; c++){
24                     f[5] = op[c];
25                     for(int d=0; d<op.length; d++){
26                         f[7] = op[d];
27                         for(int e=0; e<op.length; e++){
28                             f[9] = op[e];
29                             for(int g=0; g<op.length; g++){
30                                 f[11] = op[g];
31                                 for(int h=0; h<op.length; h++){
32                                     f[13] = op[h];
33                                     for(int i=0; i<op.length; i++){
34                                         f[15] = op[i];
35                                         for(int j=0; j<f.length; j++){
36                                             target = target + f[j];
37                                         }
38                                         if(x == compute(target)){
39 //                                            System.out.println(target);
40                                             count++;
41                                         }
42                                         target = "";
43                                     }
44                                 }
45                             }
46                         }
47                     }
48                 }
49             }
50         }
51         System.out.println(count);
52 //        System.out.println(compute("1+2+3-4+5+6-7-8"));
53     }
54     
55     public static int compute(String target){
56         while(target.contains("+") || target.contains("-")){
57             if(target.contains("+")){
58                 return compute(target.substring(0, target.indexOf("+"))) +
59                         compute(target.substring(target.indexOf("+") + 1));
60                         
61             }else{
62                 return compute(target.substring(0, target.lastIndexOf("-"))) -
63                         compute(target.substring(target.lastIndexOf("-") + 1)); 
64             }
65         }
66         return Integer.parseInt(target);
67     }
68 }
bubuko.com,布布扣

2014年华为实习机试题总结,布布扣,bubuko.com

2014年华为实习机试题总结

原文:http://www.cnblogs.com/blogOfMrProblem/p/3617840.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!