珠海某外企在线C++笔试题(算法)二面

要求在线答题,一定时间内,根据题目(全英文)编写程序,将答案贴到指定WEB页(对方即时可见)。
题1:代码Section 1处
题2:代码Section 2处
C++ Builder 6.0上完成,代码如下
//---------------------------------------------------------------------------
#include <iostream>
using namespace std;

#pragma hdrstop

void F_Sum(int A[]);
void F_Shrink(int A[]);
void F_Shrink2(int A[]);

int A_1[]=  {
                1,12,3,4,5,95,7,8,9,92,
                11,2,33,14,15,16,17,18,85,82,
                66,22,23,24,25,26,75,28,29,59,
                99,32,13,57,35,87,37,51,39,40,
                100,42,43,44,45,46,47,48,88,50,
                38,52,53,54,55,56,34,58,30,60,
                61,62,63,64,65,21,67,68,69,70,
                71,72,73,74,27,76,77,78,79,80,
                81,20,83,84,19,86,36,49,89,90,
                91,10,93,94,6,96,97,98,31,41
            };
//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
    F_Sum(A_1);     //和为50的元素对其值置为零
    
    F_Shrink(A_1);  //得到不含零值元素的新数组

    return 0;
}
//---------------------------------------------------------------------------

//Section 1: 给定一整型数组A[100],找出和为50的元素对,并将元素值置零(但元素本身值为25的,如"A[18]+A[18]=50"不作数)
//请问算法复杂度是多少?能否写出计算复杂度更小的算法?
void F_Sum(int A[])
{
	for (int i=0; i<100; i++)
	{
        if (A[i]>=50)
        {
            continue;   //执行下一轮循环
        }
        else
        {
            for ( int j=i+1; j<100; j++)
            {
                if ( A[i]+A[j] == 50)
                {
                    cout<<"A["<<i<<"]="<<A[i]<<", A["<<j<<"]="<<A[j]<<endl;
                    A[i]=0;
                    A[j]=0;
                    break;
                }
            }

        }
	}

    for (int i=0; i<100; i++)
    {
        cout<<A[i]<<' ';
    }
    cout<<endl;
}
//时间复杂度:O(N.^2)
//优化算法:【待给出】

//---------------------------------------------------------------------------

//Section 2:
//Implement a function to discard the 0 in the array A described in Section 1, and shrink the array, move the array member forward to fill member which has integer 0.
//For Example:
//A = 23, 11, 0, 54, 0, 88, 53, …
//The result is:
//A=23, 11, 54, 88, 53, …
//(Important: Not allowed to declare another array)
//请问算法复杂度是多少?能否写出计算复杂度更小的算法?
void F_Shrink(int A[])
{
	int i, j, count;
	for (i=0, j=99, count=0; i<99 && j>0 && i<j; )
	{
		if ( A[i] != 0  )
		{
			i++;
			count++;
            continue;
		}
		if ( A[j] == 0 )
		{
			j--;
            continue;
		}

        count++;
        A[i]=A[j];
        A[j]=0;
        i++;
        j--;  
	}

	for (int i=0; i<count; i++)
	{
		cout<<A[i]<<' ';
	}
    cout<<endl;
}
//时间复杂度:O(N)
//问题:是否可以不打乱原值顺序,比如此例结果应为:95,92,85,82……【待给出】

//---------------------------------------------------------------------------
笔试第二部分是表的联合查询,考查包括AVG、MAX等函数在内的SQL使用,比较简单,这里就不具体陈述了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值