您的位置:首页 > 职场人生

想成为Google工程师?先回答这15个面试问题【这只是一必要条件】(一)

2012-02-07 20:30 501 查看
前段时间google在校内发表了一篇日志,名为《想成为Google工程师?先回答这15个面试问题【这只是一必要条件】》地址:http://page.renren.com/601020521/note/803001195?&ref=minifeed&sfet=2003&fin=3&ff_id=1803322995&feed=user_blogshare&statID=page_601020521_1&level=1,本着顶礼膜拜的心情看完了这几段题,2012年新的学习生涯就以这几道题开始吧,抛砖引玉一下,希望各位给出更合理的解决方案。

第一题,题目如下:

1.请写出一个整数乘法的算法
挑战: 这是一个相当开放性的问题,设计初衷是看看工程师是否会定义参数。

编程语言是什么?

是汇编语言吗?

是:那么你得白手起家开发出一套基本运算来。

否则的话:那就简单了,只需将数字套进去即可,因为大部分语言均支持数学运算。


这道题网上都有解决方案和源代码吧。写了一个,感觉不是很精简,有时候会很困惑,怎么样写出漂亮的代码呢?大家批评指正,希望高手指导一下。

// The program used to multiply two big integer  a  and b
//input: integer A and  B. A,B :(0, 99999999)
//output: the multiply of the  two integer
#include <iostream>
#include <fstream>

using namespace std;

const int SIZE = 10;

//multiply the two integer
int * multiply(char * a, char * b)
{
if(a == NULL|| b == NULL)
return NULL;

int al = strlen(a);
int bl = strlen(b);

int * result = new int[al+bl+1];
memset(result, 0, sizeof(int) * (al+bl+1));

//calculate the multiply
for (int i=al-1; i >=0; i --)
{
int anum = a[i] - '0';

for (int j=bl-1; j>=0; j --)
{
int bnum = b[j] - '0';
result[al+bl-i-j-1] +=  anum * bnum;
result[al+bl-i-j] += result[al+bl-i-j-1] / 10;
result[al+bl-i-j-1] %= 10;
}//end for j
}//end for i

result[0] = al + bl ;
return result;
}

int main()
{
ifstream fin("input.txt");
ofstream fout("output.txt");

//read the integer;
char * a = new char[SIZE];
char * b = new char[SIZE];

fin.getline(a,SIZE);
fin.getline(b,SIZE);

int * c = multiply(a, b);

for (int i =c[0]; i > 0; i --)
{
fout << c[i];
}

//release the memory
free(a);
free(b);
free(c);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: