标签:面试题
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; }
这个是笔试时间内唯一做出来经过测试的。。。
个人解决方案
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 = new int*[n]; reader>>dir; for(int i=0;i<n;i++) { data[i] = new int[n]; } for(int i =0;i<n;i++) { for(int j=0;j<n;j++) { reader>>data[i][j]; } } move(); w<<"Case #"<<m+1<<":"<<endl; for(int i =0;i<n;i++) { for(int j=0;j<n;j++) { w<<data[i][j]<<' '; } w<<endl; } } reader.close(); w.close(); } void move() { if(dir.compare("up") == 0) { // up for(int j=0;j<n;j++) { int bit = 0; for(int i=0;i<n;i++) { if(data[i][j] != 0) { if( bit != i) { data[bit][j] = data[i][j]; data[i][j] = 0; } bit++; } } } for(int i=0;i<n-1;i++) { for(int j=0;j<n;j++) { if(data [i][j] != 0 && data[i+1][j] == data [i][j]) { data[i][j] *= 2; for(int k = i+1;k < n-1;k++) { data[k][j] = data[k+1][j]; } data[n-1][j] = 0; } else if( data [i][j] == 0 ) { for(int k = i;k < n-1;k++) { data[k][j] = data[k+1][j]; } data[n-1][j] = 0; if( i>1 && data[i][j] == data [i-1][j]) { data[i-1][j] *= 2; for(int k = i;k < n-1;k++) { data[k][j] = data[k+1][j]; } data[n-1][j] = 0; } } } } // up for(int j=0;j<n;j++) { int bit = 0; for(int i=0;i<n;i++) { if(data[i][j] != 0) { if( bit != i) { data[bit][j] = data[i][j]; data[i][j] = 0; } bit++; } } } } else if(dir.compare("right") == 0) { // right for(int i=0;i<n;i++) { int bit = n-1; for(int j=n-1;j>=0;j--) { if(data[i][j] != 0 ) { if( j != bit) { data[i][bit] = data[i][j]; data[i][j] = 0; } bit--; } } } for(int j=n-1;j>0;j--) { for(int i=0;i<n;i++) { if(data [i][j] != 0 && data[i][j-1] == data [i][j]) { data[i][j] *= 2; for(int k = j-1;k > 0;k--) { data[i][k] = data[i][k-1]; } data[i][0] = 0; } else if( data [i][j] == 0) { for(int k = j;k > 0;k--) { data[i][k] = data[i][k-1]; } data[i][0] = 0; if( j<n-1 && data[i][j] == data[i][j+1]) { data[i][j+1] *= 2; for(int k = j;k > 0;k--) { data[i][k] = data[i][k-1]; } data[i][0] = 0; } } } } // right for(int i=0;i<n;i++) { int bit = n-1; for(int j=n-1;j>=0;j--) { if(data[i][j] != 0 ) { if( j != bit) { data[i][bit] = data[i][j]; data[i][j] = 0; } bit--; } } } } else if(dir.compare("down") == 0) { // down for(int j=0;j<n;j++) { int bit = n-1; for(int i=n-1;i>=0;i--) { if(data[i][j] != 0) { if(bit != i) { data[bit][j] = data[i][j]; data[i][j] = 0; } bit--; } } } for(int i = n-1;i>0;i--) { for(int j=0;j<n;j++) { if(data [i][j] != 0 && data[i-1][j] == data [i][j]) { data[i][j] *= 2; for(int k = i-1;k >0;k--) { data[k][j] = data[k-1][j]; } data[0][j] = 0; } else if( data [i][j] == 0 ) { for(int k = i;k >0;k--) { data[k][j] = data[k-1][j]; } data[0][j] = 0; if( i<n-1 && data[i][j] == data [i+1][j]) { data[i+1][j] *= 2; for(int k = i;k >0;k--) { data[k][j] = data[k-1][j]; } data[0][j] = 0; } } } } // down for(int j=0;j<n;j++) { int bit = n-1; for(int i=n-1;i>=0;i--) { if(data[i][j] != 0) { if(bit != i) { data[bit][j] = data[i][j]; data[i][j] = 0; } bit--; } } } } else if(dir.compare("left") ==0) { // left for(int i=0;i<n;i++) { int bit = 0; for(int j=0;j<n;j++) { if(data[i][j] != 0) { if(bit != j) { data[i][bit] = data[i][j]; data[i][j] = 0; } bit++; } } } for(int j=0;j<n-1;j++) { for(int i=0;i<n;i++) { if(data [i][j] != 0 && data[i][j+1] == data [i][j]) { data[i][j] *= 2; for(int k = j+1;k<n-1;k++) { data[i][k] = data[i][k+1]; } data[i][n-1] = 0; } else if( data [i][j] == 0) { for(int k = j;k <n-1;k++) { data[i][k] = data[i][k+1]; } data[i][n-1] = 0; if( j>0 && data[i][j] == data[i][j-1]) { data[i][j-1] *= 2; for(int k = j;k <n-1;k++) { data[i][k] = data[i][k+1]; } data[i][n-1] = 0; } } } } // left for(int i=0;i<n;i++) { int bit = 0; for(int j=0;j<n;j++) { if(data[i][j] != 0) { if(bit != j) { data[i][bit] = data[i][j]; data[i][j] = 0; } bit++; } } } } }
标签:面试题
原文地址:http://blog.csdn.net/cqs_experiment/article/details/39375331