CareerCup 5.7 & 13.1

CareerCup 5.7 Missing Number

#include <iostream>
#include <vector>
using namespace std;

bool fetchBit(int num, int column) {
	if(column < 0 || column >= 32)
		return -1;

	return ((num & (1 << (31 - column))) != 0 );
}

int findMissing(vector<int> input, int column) {
	if(column < 0 || column > 31) {   //base case and error condition
		return 0;
	}

	vector<int> oneBits, zeroBits;

	for(int i = 0; i < input.size(); i++) {
		if(fetchBit(input[i], column)) {
			oneBits.push_back(input[i]);
		} else {
			zeroBits.push_back(input[i]);
		}
	}

	if(zeroBits.size() <= oneBits.size()) {
		int v = findMissing(zeroBits, column - 1);
		return (v << 1 | 0);
	} else {
		int v = findMissing(oneBits, column - 1);
		return (v << 1 | 1);
	}
}

int findMissing(int array[], int n) {
	vector<int> nums;
	for(int i = 0; i < n; i++) {
		nums.push_back(array[i]);
	}
	return findMissing(nums, 31);
}

int main(int argc, const char *argv[]){
    int a[6] = { 0, 1, 2, 4, 5, 6};
    int ret = findMissing(a, 6);
    cout<<ret<<endl;

}



Career Cup 13.1

//============================================================================
// Name        : CareerCup131.cpp
// Author      : SK2
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <fstream>
#include <string>

#define K 10
using namespace std;


void printLastKLines(char * fileName) {
	ifstream inputfile(fileName);
	string array[K];
	int size = 0;

	while(inputfile.good()) {
		::getline(inputfile, array[size % K]);
		size++;
	}

	int start = size > K ? (size % K) : size;
	int count = size > K ? K : size;

	for(int i = 0; i < K; i++) {
		cout<<array[(start + i) % K]<<endl;
	}

}


int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章