07 2013 档案

摘要:You've been given a list of words to study and memorize. Being a diligent student of language and the arts, you've decided to not study them at all and instead make up pointless games based on them. One game you've come up with is to see how you can concatenate the words to generate the 阅读全文
posted @ 2013-07-31 04:32 pgu2 阅读(224) 评论(0) 推荐(0)
摘要:solution:const int N = 8;int position[N]; // Check if a position is safebool isSafe(int queen_number, int row_position){ // Check each queen before this one for(int i=0; i<queen_number; i++) { // Get another queen's row_position int other_row_pos = ... 阅读全文
posted @ 2013-07-25 10:21 pgu2 阅读(191) 评论(0) 推荐(0)
摘要:A double-square number is an integerXwhich can be expressed as the sum of two perfect squares. For example, 10 is a double-square because 10 = 32+ 12. Your task in this problem is, given X, determine the number of ways in which it can be written as the sum of two squares.For example, 10 can only be 阅读全文
posted @ 2013-07-25 08:20 pgu2 阅读(413) 评论(0) 推荐(0)
摘要:Design a stack that supports push, pop, and retrieving the minimum element in constant time. Can you do this?Solution:struct Node{ int min; int val; Node( int val) {this->min = val; this->val = val;}};class stackNode : public stack{public: void push(Node& sN); void pop(); int getMin(); };v 阅读全文
posted @ 2013-07-25 07:30 pgu2 阅读(332) 评论(0) 推荐(0)
摘要:Given a function which generates a random integer in the range 1 to 7, write a function which generates a random integer in the range 1 to 10 uniformly.solution:int rand10() { int row, col, idx; do { row = rand7(); col = rand7(); idx = col + (row-1)*7; } while (idx > 40... 阅读全文
posted @ 2013-07-25 01:39 pgu2 阅读(274) 评论(0) 推荐(0)
摘要:For example, given that the pattern ="abc", replace"abcdeffdfegabcabc"with"XdeffdfegX". Note that multiple occurrences ofabc's that are contiguous will be replaced with only one'X'. Solution:string& repalce(string& s, string ch) // the only problems 阅读全文
posted @ 2013-07-25 01:34 pgu2 阅读(206) 评论(0) 推荐(0)
摘要:Print all edge nodes of a complete binary tree anti-clockwise. That is all the left most nodes starting at root, then the leaves left to right and finally all the rightmost nodes. In other words, print the boundary of the tree.Variant: Print the same for a tree that is not complete.Solution:#include 阅读全文
posted @ 2013-07-18 21:54 pgu2 阅读(314) 评论(0) 推荐(0)
摘要:Given the sequenceS1= {a,b,c,d,...,x,y,z,aa,ab,ac.... } and given that this sequence corresponds (term for term) to the sequenceS2= {0,1,2,3,....}. Write code to convert an element ofS2to the corresponding element ofS1.solution:// ExcelSheetRowNumbers.cpp : Defines the entry point for the console ap 阅读全文
posted @ 2013-07-16 22:27 pgu2 阅读(188) 评论(0) 推荐(0)
摘要:Design an algorithm and write code to serialize and deserialize a binary tree.Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.Solution:// TreeSerialAndDeserial.cpp : Defines the entry p 阅读全文
posted @ 2013-07-13 04:58 pgu2 阅读(371) 评论(0) 推荐(0)
摘要:Given a singly linked list, find if there exist a loop.Solution:#include "stdafx.h"#include using namespace std;class Node{public: void addNode(Node* node); void loopStart(); void loopEnd(); void setVal(int val) {this->val = val;} int getVal() {return this->val;} Node(int val) {this- 阅读全文
posted @ 2013-07-08 21:20 pgu2 阅读(292) 评论(0) 推荐(0)
摘要:ProblemOn our planet, Jamcode IX, three Great Events occurred. They happened 26000, 11000 and 6000 slarboseconds ago. In 4000 slarboseconds, the amount of time since all of those events will be multiples of 5000 slarboseconds, the largest possible amount... and the apocalypse will come.Luckily for y 阅读全文
posted @ 2013-07-02 01:01 pgu2 阅读(218) 评论(0) 推荐(0)