ITeye博客

面试题记

jasstion · 2009-10-13 10:50 · Java学习 · 非技术 · Markdown

今天在学校里进行了第二次面试,这次面试官什么都不讲,只讲编程水平,便有下面两道编程题,在平时的话是很简单的,但在当时条件下,做的有点糟糕:过后又把它们做了一便:

1:数1,1,2,3,5,8,13,21.。。。。。求出第30个数?要求利用递归实现;
方法一:
#include "stdio.h"
#include "stdlib.h"
int Cacluate(int a,int b,int t){
   
    int m=0;
   m=a+b;a=b;b=m;
 
   t=t-1;
 
    if(t==0){return m;}
    else{
      return Cacluate(a,b,t); 
       }
}
int main(){
    int n=Cacluate(1,1,3);  
    printf("%d",n);
     getchar();
     return 0;
}
方法二:
#include "stdio.h"
#include "stdlib.h"
int Cacl(int n){
    if(n==1){return 1;}
    if(n==2){return 2;}
    else {return Cacl(n-1)+Cacl(n-2);}
}
int main(){
    int m=Cacl(1,1,3);  
    printf("%d",m);
     getchar();
     return 0;
}

2:给定一个数组a[100];要求随机的插入1—100个数并且不能重复;
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
int Cacluate(int a,int b,int t){
   
    int m=0;
   m=a+b;a=b;b=m;
 
   t=t-1;
 
    if(t==0){return m;}
    else{
      return Cacluate(a,b,t); 
       }
}

int main(){
    srand((unsigned)time(NULL));
    int a[100],b[100];
   
    for(int i=0;i<100;i++){
        b[i]=i+1; 
   
        //printf("%d/n",b[i]);
            }
for(int j=0;j<100;j++){
        int m=rand()%100;
        while(b[m]==0){
         m=rand()%100; }
        a[j]=b[m];
        b[m]=0;
        }
for(int j=0;j<100;j++){
        printf("%d/n",a[j]);
        }

     getchar();
     return 0;
}

评论(2)

  1. #2 楼 · jasstion · 2009-10-18 12:12

    星情泪 writes:

    第一题 [code=ruby] def cal(num, a = 1, b = 0) tmp = a a = b b += tmp if num == 1 b else cal(num-=1, a, b) end end

    p cal(1) p cal(2) p cal(3) p cal(4) p cal(5) p cal(6) p cal(30)

    1 1 2 3 5 8...

    [/code]

    第二题不知道有没有理解对,按照自己的理解写了一个 [code=ruby] a = Array.new(100) b = (1..100).to_a a.length.times do |index| a[index] = b.delete_at(rand(b.length)) end p a [/code]

    变量名瞎定义的,见谅 :oops:

    ruby实现这么容易啊汗

  2. #1 楼 · 星情泪 · 2009-10-17 22:52

    第一题

    
    def cal(num, a = 1, b = 0)
      tmp = a
      a = b
      b += tmp
    	if num == 1
    		b
    	else
    		cal(num-=1, a, b)
    	end
    end
    
    p cal(1)
    p cal(2)
    p cal(3)
    p cal(4)
    p cal(5)
    p cal(6)
    p cal(30)
    
    # 1 1 2 3 5 8...
    
    

    第二题不知道有没有理解对,按照自己的理解写了一个

    
    a = Array.new(100)
    b = (1..100).to_a
    a.length.times do |index|
      a[index] = b.delete_at(rand(b.length))
    end
    p a
    
    

    变量名瞎定义的,见谅 :oops:

评论功能维护中,当前仅展示历史评论。