2016上海某公司面试题_4

今天某金融公司邀请我参加编程基础测试在线笔试,题目前两个蛮简单的:


1.判断题(2分): java数组有length方法吗?(B) 数组中的是属性!!!

A、对
B、错误


2.判断题(2分): java的String类有length方法吗?(A)字符串中是方法!!!

A、对
B、错误


3.编程题(30分):有一种数字口算游戏,N个人站成一排,编号为1到N,游戏规则:编号1-3的前3个人数字是确定的: 第一个人报2,第二个人报3,第三个人报5。从第4个人开始报数,编号为index的人说的数字为编号index-1和编号index-3的人的数字之和,减去编号为index-2的人的数字的2倍。


举个例子: 第四个人报1 (2+5-2*3=1),第五个人报 -6 (1+3-2*5= -6)。
请问第70个人报___,第99个人报___。
请编程实现,填空并且附上源代码或者伪代码!


  1. public class Test1 {  
  2.     public int getNumber(int n) {  
  3.         //这里写上您的代码  
  4.   
  5.     }  
  6. }  
public class Test1 {
    public int getNumber(int n) {
        //这里写上您的代码

    }
}

答:


70->560711502
99->1577520226

设第n个报f(n) 其中n>3,这有f(n)=f(n-1)+f(n-3)+2*f(n-2)


  1. public Map<Integer,Integer> cache = new HashMap<Integer,Integer>();  
  2. public class Test1 {  
  3.     public int getNumber(int n) {  
  4.         //这里写上您的代码  
  5.         if(n == 1){return 2;}  
  6.         if(n == 2){return 3;}  
  7.         if(n == 3){return 5;}  
  8.         Integer res = cache.get(n);  
  9.         if(res != null){return res;}  
  10.         res = f(n-1)+f(n-3)-(2*f(n-2));  
  11.         cache.put(n, res);  
  12.         return res;  
  13.     }  
  14.   
  15. }  
public Map<Integer,Integer> cache = new HashMap<Integer,Integer>();
public class Test1 {
    public int getNumber(int n) {
        //这里写上您的代码
        if(n == 1){return 2;}
        if(n == 2){return 3;}
        if(n == 3){return 5;}
        Integer res = cache.get(n);
        if(res != null){return res;}
        res = f(n-1)+f(n-3)-(2*f(n-2));
        cache.put(n, res);
        return res;
    }

}

4. 编程题(30分):如果一个字符串里面字母之间ASCII码值形成等差数列(等差大于等于1),那么我们称之为优美字符串,比如长度为3的优美字符串有abc, ace, bcd,xyz等, 长度为4的优美字符串有abcd, cdef, aceg等。现在给定a-z 26个小写字母集合,
请问长度为3的优美字符串总共有___个,长度为10的优美字符串总共有___个。
请编程实现,填空并且附上源代码或者伪代码!




5. 编程题(30分):(注意,本题运行时间不能超过1秒,请把循环次数控制在1千万次之内)魔术数是指一个自然数既是完全平方数(能表示成一个整数的平方),也能被5整除,比如25和400都是魔术数:25=5*5, 25%5=0;请问[1, 10^12] (1到1万亿之间,包含1和1万亿)之间所有的魔术数有___个。并把这些数字作为答案附在程序后面一起发回给我们。
请编程实现,填空并且附上源代码或者伪代码!


有100个(算上0的话101)
设要平方的数为x,要立方的数为y,其中x<1000000,y<10000,x=y的二分之三次方;x为整数


  1. public static void main(String[] args) {  
  2.         int maxY = 10001;  
  3.         int maxX = 1000001;  
  4.         int count = 0;  
  5.         long start = System.currentTimeMillis();  
  6.         for (int i = 1; i < maxY; i++) {  
  7.             double x = Math.pow(i, 1.5);  
  8.             if(x%1 == 0 && x < maxX){//x为整数  
  9.                 count++;  
  10.             }  
  11.         }  
  12.         long end = System.currentTimeMillis();  
  13.         System.out.println(“有:”+count+”个 耗时:”+(end-start)+” ms ”);  
  14.     }  
public static void main(String[] args) {
        int maxY = 10001;
        int maxX = 1000001;
        int count = 0;
        long start = System.currentTimeMillis();
        for (int i = 1; i < maxY; i++) {
            double x = Math.pow(i, 1.5);
            if(x%1 == 0 && x < maxX){//x为整数
                count++;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("有:"+count+"个 耗时:"+(end-start)+" ms ");
    }


输出结果:有:100个 耗时:0 ms (这个视机器情况)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值