某G电面题

先吐槽一下Google Doc。。还不如在collabedit上写呢。。拿word来coding伤不起。。


Hi, did you receive my phone call?

nope...


hmm, ok, please hold on for a moment

sure,





f(x) = ax^2 + bx +c


arr[] = {-6, 4, 6, 7, 9}

给定一个二次函数和一个升序数组,将这个升序数组根据f(x)的值重新排序该数组。

alpha = -1*(b/(2*a))

void process(double a,double alpha, int n){
	if(a==0){
		if(b>=0)
			return;
		reverse(arr,0,n)
		return;
	}
	int k = 0;
	while(k<n && arr[k]<=alpha)
		k++;
	if(a>0)
		reverse(arr,0,k);
	else
		reverse(arr,k,n);
	int *tmp = new int[n];
	int i = 0,j= k;
	int cnt = 0;
	while(i<k && j<n){
		if(f(arr[i])<f(arr[j])){
			tmp[cnt++] = arr[i++];
}else
	tmp[cnt++] = arr[j++];
	}
	while(i<k)
		tmp[cnt++] = arr[i++];
	while(j<n)
		tmp[cnt++] = arr[j++];
	
	for(int i=0;i<n;i++)
		arr[i] = tmp[i];
	delete[] tmp;
}


2.
5 / 3 = 1.66..

set<string> s;
double compute(int a,int b){
	double res = 0;
	int dec = 0;
	
	while(1){
	while(a<b){
string str = build(a,b);//a_b		
		set<string>::iterator it = s.find(str);
		if(it!=s.end()){
			//loop
			return ...
}
		s.insert(str);	
		a*=10;
		dec++;
}
double t = a/b;
a = a%b;
res += t/10^dec;
}
}


void process(double a,double alpha, int n){

if(a==0){

if(b>=0)

return;

reverse(arr,0,n)

return;

}

int k = 0;

while(k<n && arr[k]<=alpha)

k++;

if(a>0)

reverse(arr,0,k);

else

reverse(arr,k,n);

int *tmp = new int[n];

int i = 0,j= k;

int cnt = 0;

while(i<k && j<n){

if(f(arr[i])<f(arr[j])){

tmp[cnt++] = arr[i++];

}else

tmp[cnt++] = arr[j++];

}

while(i<k)

tmp[cnt++] = arr[i++];

while(j<n)

tmp[cnt++] = arr[j++];

for(int i=0;i<n;i++)

arr[i] = tmp[i];

delete[] tmp;

}



2.

5 / 3 = 1.66..

找循环节。

set<string> s;

double compute(int a,int b){

double res = 0;

int dec = 0;

while(1){

while(a<b){

string str = build(a,b);//a_b

set<string>::iterator it = s.find(str);

if(it!=s.end()){

//loop

return ...

}

s.insert(str);

a*=10;

dec++;

}

double t = a/b;

a = a%b;

res += t/10^dec;

}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值