Google 2014 online exam
1. I feel that it is very time-consuming to do Google online pen-writing questions. I only wrote one question at the time of the test, which is the second question. The first question was made that night... Suddenly I felt very watery...
Personal solutions need to solve the following sub-Problems
1) determine whether a digital lamp with a bad lamp shows a number.
2) determine whether a series of numbers are displayed in the descending order of a number.
3) Check a string of faulty tubes matching correct numbers.
4) the output shows a string of correctly decreasing numbers of tubes.
4) the output shows a string of lamps with broken tubes to display numbers.
#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 datareader>>num;data = new string[num] ;for(int j = 0;j<num;j++){reader>>data[j] ;}// deal datawriter<<"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 numstring 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 resultstring 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 resultbool 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 stylestring makebroken(string *data,int length,int minnum){bool brokenhash[7];for( int i=0;i<7;i++ ){brokenhash[i] = true;}// find all brokenfor( 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 lebsfor( int i=0;i<7;i++ ){for( int j=0;j<length;j++ ){if( brokenhash[i] == false && data[j][i] == '1'){return "ERROR!";}}}// get datastring result = myhash[minnum];for(int i=0;i<7;i++){if( brokenhash[i] == false )result[i] = '0';}return result;}
Question 2 2048
This is the only one that has been tested during the written test...
Personal Solutions
1 ). Move all data to closeness
2 ). Merge data
3 ). Move all data to closeness
#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){// upfor(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;}}}}// upfor(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){// rightfor(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;}}}}// rightfor(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){// downfor(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;}}}}// downfor(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){// leftfor(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;}}}}// leftfor(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++;}}}}}
Google 2014 online exam