微软面试题[1-2]

来源:互联网 发布:数据检索 编辑:程序博客网 时间:2024/05/01 07:17

1.Given a rectangular(cuboidal for the puritans) cake whith a rectangular piece removed (
any size or orientation), how would you cut the remainder of the cake into two equal halves with one straight cut of a knife?

译:给定一块已在任意位置用任意方向切去一个矩形块的矩形蛋糕(注意是立体的,质量均匀的),怎样用一刀切出相等重量的两块来?

 

找出这两个矩形块的中心点m和n,然后切面a可以垂直矩形的任意面,但是必须过m和n点,这样切后的两块蛋糕在体积上是相等了

 

2. You're given anarray containing both positive and negative integers and required to find thesub-array with the largest sum (O(N) a la KBL). Write a routine in C for theabove.

一个矩阵中包含正数和负数,要求寻找和最大的子矩阵

int main()
{
 
 int a[] ={ -2,30,-5,-13,-19,1,-3,2};
 int len=8;
 int sum=0,beg=0,end=0,maxBeg=0,maxEnd=0,maxSum=0,i=0;
 maxBeg=i;
 maxEnd=i;

 while (i < len)
 {
  sum +=a [ i ];
  if( sum > maxSum )
  {
   maxSum = sum;
   maxEnd = end;
   maxBeg = beg;
  }
  else if( sum < 0 )
  {
   beg = i + 1;
   sum = 0;
  }
  i++;
  end++;
 }
   
    printf("beg %d,end %d,sum %d",maxBeg,maxEnd,maxSum);
 
 
 return 0;
}

 

 

原创粉丝点击