I come, I see, I conquer

                    —Gaius Julius Caesar

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

SECTION Ⅰ

Instruction: You are not allowed to use any library or API calls. Your answer will be evaluated from the following aspects: correctness, algorithm efficiency, error handing. API interface, and boundary case handing. You may use any language to code the answer.

Given two arrays which are sorted ascendingly, write a function to get the N th  number from the merged and sorted array of them.

For example :

INPUT 
A : 1,4,6,7,9
B : 2,4,5,8,9
N : 3
MERGED : 1,2,(4),4,5,6,7,8,9

OUTPUT : 4

However, due to the performance requirement, the spatial complexity of the solution should be O(1) and temporal complexity of it should be O(N).

对于这道题,
我的思路如下:
因为数组a和b都是升序排列的,要想确定第N个数是什么,可以逐个比较两数组中的各元素,a[i]和b[j]。其中下标i,j分别指向数组a,b。

每次确定一个数在合并后数组中的位置。以index来记录我们当前在合并数组中所处的位置,确定一个数的位置后就将index的值增加1,当index值为N时候,我们也就找到了所需的数(下标i,j正好记录了该数的位置)

程序中没有利用辅助数组,所以空间复杂度为O(1),时间复杂度为O(n),满足要求。

程序如下:

#include <stdio.h>
#include 
<stdlib.h>

int get_nth_val(int a[], int b[], int sa, int sb, int n)
{
    
int i, j;
    
int index;
    
    
if (n < 0 || (n > sa+sb))
    {
        printf(
"invalid value!!!\n");
        exit(
-1);
    }

    i 
= j = 0;
    index 
= 0;    /* count nth number we now at */

    
while (i < sa && j < sb)
    {
        
if (a[i] < b[j])
        {
            index
++;
            
if (index == n)
                
return a[i];
            i
++;
        }
        
else
        {
            index
++;
            
if (index == n)
                
return b[j];
            j
++;
        }
    }
    
    
if (i == sa)
        
return b[n-index-1+j];
    
else
        
return a[n-index-1+i];
}

int main()
{
    
int a[] = {146791115173945};
    
int b[] = {24589/*12, 16, 20, 33, 56*/};
    
int sa = sizeof(a) / sizeof(a[0]);
    
int sb = sizeof(b) / sizeof(b[0]);
    
    
int n;
    
int c;
    n 
= 3;
    
    c 
= get_nth_val(a, b, sa, sb, n);
    printf(
"the %dth number is %d\n", n, c);
    
    
return 0;
}

 

SECTION Ⅱ

Instruction: Choose one of the following questions and answer it in English.

1. Describe a situation in which you had to convince others that your view, approach, or ideas were right or appropriate.

2. What experience have you had working in teams? Using a specific example, which role did you play on the team? How did you select that role? What were the most/least satisfying aspects of working on that team? What in the most difficult thing for you in working with a team?

3. Have you ever had an idea or a goal to achieve something that required action by other individuals beyond just yourself? How did you get the idea or come to set the goal? How did you find or mobilize the requisite resources to make the idea or goal become real? How did you deal with any un foreseen events along the way?


SECTION Ⅲ

Write a function which takes three integers, they represent length of each side of a triangle, and determine what kind of triangle those three integers three would construct. Return '1' if all sides are same length, return '2' if two sides are same, and return '3' if all sides are different. You should consider all possible conditions. Describe how you skill test this function.

给定3个整数,看它们能构成什么样的三角形,我直接按数学中三角形判断方法来做,不知道该题目有什么其他玄机。
程序如下:

#include <stdio.h>
#include 
<stdlib.h>

#define ABS(x)  ((x < 0) ? (-x) : (x))

int triangle(int a, int b, int c)
{
    
if (a < 1 || b < 1 || c < 1)
        
return -1;
    
if ((a == b) && (b == c))
        
return 1;
    
if ((a+> c) && (a+> b) && (b+c) > a
        
&& (ABS(a-b) < c) && (ABS(a-c) < b) && (ABS(b-c) < a))
    {
        
if (a == b || b == c || a == c)
            
return 2;
        
else
            
return 3;
    }
    
/* can't construct a triangle */
    
return -1;
}

int main()
{
    
int flag;
    flag 
= triangle(341);
    
switch(flag)
    {
    
case 1:
        printf(
"three edges are all equal\n"); break;
    
case 2:
        printf(
"two edges are equal\n"); break;
    
case 3:
        printf(
"no equal edges\n"); break;
    
default:
        printf(
"can't construct triangle\n");
    }
    
return 0;
}
posted on 2008-09-12 20:50  jcsu  阅读(873)  评论(0)    收藏  举报