首页 > 代码库 > google 2014在线笔试题

google 2014在线笔试题

google 2014在线笔试题

1.个人做google在线笔试题感觉时间很紧,笔试时间只做出来了一个题目就是第二道题,这第一道题是当天晚上才做出来的。。。突然感觉自己很水。。。









个人解决方案需要解决如下子问题

1)判断一个具有坏灯管的数字灯是否显示的是某个数字

2)判断一串灯管显示的是否是以某个数字为首递减的一串数字

3)检查一串匹配正确数字的灯管中坏掉的灯管是哪些

4)输出显示一串正确递减数字的灯管

4)输出显示一串有坏灯管的来显示数字的灯管

#include<iostream>
#include <map>
#include <string>
#include<fstream>
using namespace std;


string myhash[] ={"1111110","0110000","1101101","1111001","0110011","1011011","1011111","1110000","1111111","1111011"};



bool checkNum(string numstr,int num);
string mymain(string *data,int length);
bool checkArray(string * strarr,int length,int bit,int num);
string makebroken(string *data,int length,int minnum);



int main()
{

	int num ;
	string *data ;
	ifstream reader ;
	reader.open("A.txt") ;
	ofstream writer;
	writer.open("test1.txt");
	int n;
	reader>>n;
	for(int i=0;i<n;i++)
	{

		// get in data
		reader>>num;
		data = new string[num] ;

		for(int j = 0;j<num;j++)
		{
			reader>>data[j] ;
		}

		// deal data
		writer<<"Case #"<<i+1<<": "<<mymain(data,num)<<endl;
	}
	reader.close();
	writer.close();
	system("pause");
	return 0;
}







// input num: length of data
// input data: all string data of num
// output: the next num
string mymain(string *data,int length)
{
	int resultnum = 0;
	
	const int incorint = -2;
	int correctint = incorint;
	int checknum ;

	for(int i=9;i >= 0;i--)
	{
		if( checkNum(data[0],i) && checkArray(data,length,0,i) )
		{
			if(correctint != incorint && correctint != i-length)
				resultnum++;
			else 
			{
				correctint = (i-length+10) % 10;

				// check result
				string checkresult = makebroken(data,length,correctint);
				if( checkresult == "ERROR!")
				{
					correctint = incorint ;
					resultnum = 0;
				}
				else
				{
					resultnum = 1;
				}			
			}
		}
	}

	string result;
	if(resultnum == 1)
		result = makebroken(data,length,correctint);
	else result = "ERROR!";



	return result;

}

// input numstr: the string data of num
// input num: the correct num
// output: the check result
bool checkNum(string numstr,int num)
{
	bool ok = true;
	for(int i=0;i<7;i++)
	{
		if(numstr[i] == '1' && myhash[num][i] == '0')
		{
			ok = false;
			break;
		}
	}
	return ok;
}

// input strarr: the array of data string
// input length: the length of the array before
// input bit: the get correct number of array bit
// input num: the get correct number
// output bool: true or false for the correct number === arrray[bit]
bool checkArray(string * strarr,int length,int bit,int num)
{
	bool result = true;
	int n = (num + bit)%10 ;
	if(n <= 9 && n >= 0)
	{
		for(int i=0;i<length;i++)
		{
			if(checkNum(strarr[i], (n-i+10)%10 ) == false)
			{
				result = false;
				break;
			}			
		}
	}
	else result = false;
	return result;

}


// input strarr: the array of data string
// input length: the length of the array before
// input minnum: the last next number of the array
// output string: the minnum of broken style
string makebroken(string *data,int length,int minnum)
{
	bool brokenhash[7];
	for( int i=0;i<7;i++ )
	{
		brokenhash[i] = true;
	}
	// find all broken
	for( int i=0;i<7;i++ )
	{
		int cornum = (minnum + length)%10;
		for( int j=0;j<length;j++ )
		{
			if(myhash[cornum][i] == '1' && data[j][i] == '0')
			{
				brokenhash[i] = false;
				break ; 
			}
			cornum --;
			cornum = (cornum+10) % 10 ;
		}
	}

	// check broken lebs
	for( int i=0;i<7;i++ )
	{
		for( int j=0;j<length;j++ )
		{
			if( brokenhash[i] == false && data[j][i] == '1')
			{
				return "ERROR!";
			}
		}
	}

	// get data
	string result = myhash[minnum];
	for(int i=0;i<7;i++)
	{
		if( brokenhash[i] == false )
			result[i] = '0';
	}
	return result;
}

第二个题 2048

这个是笔试时间内唯一做出来经过测试的。。。








个人解决方案

1)。移动所有数据至紧密

2)。合并数据

3)。移动所有数据至紧密


#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int n;
int **data;
string dir;


void move();
void inData();

int main()
{
	inData();
	system("pause");
	return 0;
}

void inData()
{
	ifstream reader;
	reader.open("B-small-attempt0.in");
	ofstream w;
	w.open("1.txt");
	int num ;
	reader>>num;
	for(int m=0;m<num;m++)
	{
		reader>>n;
		data = http://www.mamicode.com/new int*[n];>


google 2014在线笔试题