由买买提看人间百态

topics

全部话题 - 话题: endl
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
h**6
发帖数: 4160
1
来自主题: JobHunting版 - 面经: bloomberg 电面
赞考虑周到
一般来说,-x = ~x+1
只有最小负整数是个例外,-x = x,加负号后仍然是其本身。
short a = -32768;
short b = -a;
cout< cout< a和b都是-32768
i**********e
发帖数: 1145
2
来自主题: JobHunting版 - FaceBook面经--第三(最后)部分
我以前贴过,但不知道为什么找不回那个帖子了。
#include
#include
#include
using namespace std;
const int TAB_SPACE = 4;
void outputJSon(istream &in, int indentLevel) {
bool firstChar = true;
bool inBracket = false;
while (in) {
char c = in.get();
if (firstChar) {
cout << endl << setw(indentLevel) << c;
firstChar = false;
}
else {
cout << c;
}
if (c == '{') {
outputJSon(in, indentLevel+TAB_SPACE);
c = in.get();
assert(c == '... 阅读全帖
c**********e
发帖数: 2007
3
来自主题: JobHunting版 - C++ Q42: (C22)
extern void print(int *ia, int sz); // { cout << "extern" << endl; }
void print(int *array, int size); // { cout << "space" << endl; }
Does the sample code compile?
a) Yes, it does.
b) No, there are 2 functions named print.
c) No, the function name print cannot be extern.
d) No, there are 2 functions named print that have the same signature.
m******e
发帖数: 353
4
来自主题: JobHunting版 - one amazon interview problem
#include
#include
void countOnesAndZeros(int &numZeros, int &numOnes, const std::vector &
sequence) {
numOnes = 0;
numZeros = 0;
for(size_t i = 0; i sequence[i] == 1 ? ++ numOnes : ++ numZeros;
}
}
int trimLeft(const std::vector &sequence, int start, int numToTrim, int
untilSeenSymbol) {
int trimCnt = 0;
while(start + trimCnt >= 0 && start + trimCnt < (int) sequence.size() &&
numToTrim > 0) {
if(sequenc... 阅读全帖
L*******e
发帖数: 114
5
来自主题: JobHunting版 - 砸了面试,发面题
骑驴找驴,砸了面试,潜心修行,明春再战。
//1. what is the output of the following program?
class A{
public:
struct HEADER{
int a_;
double b_;
unsigned int c_;
unsigned char d_;
static int SIZE = 100;
}header;
private:
double k;
};
int main()
{
A a;
cout << "sizeof A: " << sizeof(a) << endl;
cout << "sizeof structure: " << sizeof(a.HEADER) << endl;
}
//2. What is the output of the following program?
void print(int a, int b)
{
cout<<"value a: " << a
x****k
发帖数: 2932
6
来自主题: JobHunting版 - Apple的一些C++概念题
A general template:
template struct X
{ void f() { cout << "Primary template" << endl; } };
Partial specialized template(only for template class and struct, not for
funtion)
template struct X
{ void f() { cout << "Partial specialization 1" << endl;
} };
l********n
发帖数: 54
7
来自主题: JobHunting版 - 问个c++题
有一个类
class A
{
};
要使得下面的code能够编译
int main(void)
{
A a;
cout< return 1;
}
并且cout< 问需要在这个类里添加什么functions,如何实现。
============
应该是要对<<进行operator overloading
ostream& operator<< (const ostream& out, const A& a);
不过不知道如何实现。
PS: 建议用用这个工具,这样大家可以讨论code起来比较方便。
http://www.ideone.com/kNSiq
w****g
发帖数: 206
8
/thinking in C++/第三章最后一个程序,硬是看不懂, 请高手指点一下:

//: C03:FunctionTable.cpp
// Using an array of pointers to functions
#include
using namespace std;
// A macro to define dummy functions:
#define DF(N) void N() { \
cout << "function " #N " called..." << endl; }
DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g);
//以上是和前一句, 宏定义一起的么? 怎么解释?
void (*func_table[])() = { a, b, c, d, e, f, g };
//以上function定义怎么理解,a,b,...g,是arguments还是statments?
int main() {
... 阅读全帖
l*******o
发帖数: 791
9
来自主题: JobHunting版 - c++ 程序一问
楼上说的对。
问题出现在你的test函数,test函数接受的是一个以值传递的指针。比如说一个经典的
swap(int a, int b)的例子,必须写成swap(int * a, int * b)或者引用才能改变外
界传入函数参数的值。同理如果你想改变一个指针的值,你需要用对一个指向这个指针
的指针进行操作,
所以test必须写成
void test (char * * p)
{
*p = new char[10];
memset((*p), 'A', 10);
}
然后main函数写成
int main(int argc, char *argv[])
{
char *k;

test(&k);
cout << k << endl;
}
不过一般倾向于使用引用即reference这样你就不会混淆,reference版本如下
void test( char * & p )
{
p = new char[10];
memset(p, 'A', 10);
int main(int argc, char *argv[])
{... 阅读全帖
r*****n
发帖数: 20
10
来自主题: JobHunting版 - Google的面经
恩 make sense
题意理解有误
这个题有比O(n^2)好的算法么?
写了一下O(n^2) 测了一下349901词的一个dichttp://www.math.sjsu.edu/~foster/dictionary.txt 要跑5-6分钟
return:
stekelenburg,hohohohohoho
代码如下
int myfunc(string a, string b){
return a.size()>b.size();
}
void foo(vector arr){

if(!arr.size())
return;
//step 1. sort w.r.t. lentgh of each word O(nlogn)
sort(arr.begin(), arr.end(), myfunc);

//step 2. compute signature for each word
vector sig;
for(long i=0; i阅读全帖
b*****e
发帖数: 474
11
很不错了。细节可以再推敲一下,算法本身(基于DFS的递归)没问题,C++
的掌握也不错。当个 developer 够了哈。
我的版本,给你参考一下吧:
#include
#include // i like arrays
#include // for atoi()
using namespace std;
void print_parens(int n) {
static vector s; // i just use static vars to save parameter
static numLeft = 0; // passing
static numRight = 0;
if (n<0) return;
if (n==0) {
for( int i=0; i // save the extra recursion when there are n '('s already -
// so ... 阅读全帖
i**********e
发帖数: 1145
12
来自主题: JobHunting版 - fb 面经
For number 1, the problem of proving the statement sounds MORE interesting..
. hmm..
My solution using character pointer manipulation.
#include
using namespace std;
void printCharCounts(const char *in, char out[]) {
int count = 1;
char *pOut = out;
const char *pIn = in;
while (*pIn) {
if (*pIn == *(pIn+1)) {
count++;
} else {
*pOut++ = '0' + count;
*pOut++ = *pIn;
count = 1;
}
pIn++;
}
*pOut = '\0';
}
int main() {
char str[100] = "... 阅读全帖
i**********e
发帖数: 1145
13
来自主题: JobHunting版 - 问道 facebook 面试题
不是大侠,贴一贴我的代码,代码如果不好看 请多多包涵
恐怕只能 handle 以上的 sample test case。
基本思路就是递归,如果有更复杂的情况可以再慢慢改进。
#include
#include
#include
using namespace std;
const int TAB_SPACE = 4;
void outputJSon(istream &in, int indentLevel) {
bool firstChar = true;
bool inBracket = false;
while (in) {
char c = in.get();
if (firstChar) {
cout << endl << setw(indentLevel) << c;
firstChar = false;
}
else {
cout << c;
}
if (c == '{') {
outputJSon(in... 阅读全帖
z*******0
发帖数: 30
14
来自主题: JobHunting版 - A公司的面经
int a;
int b;
int fibo1(int n)
{
if (n == 0) return 0;
if (n == 1 || n == 2) return 1;
a = 1; b = 1;
return fiboR(3, n);
};
int fiboR(int x, int n)
{
int c = a + b;
a = b;
b = c;
if (x == n)
{
cout < return b;
}
int v = fiboR(x+1, n);
cout < return v;
};
l*****a
发帖数: 14598
15
来自主题: JobHunting版 - A公司的面经
Fibonacci(0,1,k);
void Fibonacci(int a,int b,int k)
{
cout< if(k>1) Fibonacci(b,a+b,k-1);
return;
}
void Fibonacci(int a,int b,int k)
{
if(k>1) Fibonacci(b,a+b,k-1);
cout< return;
}
why need static variables?
c******t
发帖数: 391
16
来自主题: JobHunting版 - 一道M$算法题。
How about this one? Is this O(NlogN) solution?
#include
#include
using namespace std;
void finddup(int arr[], int length){
hash_map hm;
cout< for(int i=0; i hm[arr[i]]++;
}
hash_map::iterator it;
int dup=0;
for(int i=0; i it=hm.find(arr[i]);
if(it->second>1){
dup=it->first;
cout< }
}
cout< }
void main(){... 阅读全帖
g*********s
发帖数: 1782
17
来自主题: JobHunting版 - amazon电面为啥要念程序?
so:
cout << "hello, world!" << endl;
"cout left shift double quote hello comma space world exclamatory mark
double quote left shift endl semi-comma change a line"
j***y
发帖数: 2074
18
来自主题: JobHunting版 - 大家windows下面用什么写C程序的?

我用你的coding panel调试remove duplicate的程序:
---
#include
//#include
#include
using namespace std;
const int REMOVE = -1000;
int remove_duplicate(int a[],int n){
int i = 0, j = 0;
//unordered_map hash;
map hash;
for(i = 0; i < n; ++i){
if(hash.find(a[i]) != hash.end()) {
a[i] = REMOVE;
} else {
hash[a[i]] = 1;
}
}
j = 0;
i = 0;
while (i < n){
while (a[i] == REMOVE){
i++;
}
a[j] = a[i];
i... 阅读全帖
j***y
发帖数: 2074
19
来自主题: JobHunting版 - 请教几个电面题

谢谢啊。这两天我也在学习hashtable的方法,刚好看到C++里面有个map可以用。
调试了一下你的程序,稍作调整后的code如下:
---
#include
//#include
#include
using namespace std;
const int REMOVE = -1000;
int remove_duplicate(int a[],int n){
int i = 0, j = 0;
//unordered_map hash;
map hash;
for(i = 0; i < n; ++i){
if(hash.find(a[i]) != hash.end()) {
a[i] = REMOVE;
} else {
hash[a[i]] = 1;
}
}
j = 0;
i = 0;
while (i < n){
while (a[i] == REMOVE){
i++;... 阅读全帖
l*********y
发帖数: 142
20
来自主题: JobHunting版 - 生物男的Google面经节略版
#include
#include
using namespace std;
int square[1000000];
int best = 4;
int result[4];
void RecurSearch(int count, int target, int start)
{
count ++;
if (count > best) return;
for (int i = start; i >= 1; i --) {
int value = target - square[i];
if (value < 0) continue;
if (square[i] < (target / (best - count))) continue;
result[count - 1] = square[i];
if (value == 0) {
if (count <= best) {
best =... 阅读全帖
y*******i
发帖数: 100
21
来自主题: JobHunting版 - c++ primer求助
读入一系列 string 对象,直到同一
个单词连续出现两次,或者所有的单词都已读完,才结束读取
string preword,currword;
while(cin>>currword){
if(preword==currword){
cout<<"the word is: "< break;
}//end if
else preword=currword;
}//end while
if(currword.empty()||(currword!=preword))
cout<<"there are no adjacent words are the same"< 有重复单词测试成功,但是如果输入一系列没重复单词的单词,总是打印不出"there
are no adjacent words are the same",为什么呢?找了半天找不出逻辑错误。
谢谢各位。
d*******l
发帖数: 338
22
来自主题: JobHunting版 - 问个面试题
#include
using namespace std;
int f[110];
int p[50];
int cur = 1;
int ans;
void solve(int n, int m, int s)
{
if(m == 0) {
/* for(int i = 0; i < cur; i++)
cout << p[i]+1 << " ";
cout << endl;
*/ ans += n-1-p[cur-1];
return ;
}
for(int i = s; i < n; i++) {
if(!f[i]) {
p[cur++] = i;
for(int j = cur-1; j >= 0; j--)
if(2*i-p[j] < n)
f[2*i-p[j]]++;
solve(n, m... 阅读全帖
d**e
发帖数: 6098
23
你发现有什么错?感觉没什么错误.
但如果说以整个code来看,其实后面两个return是多余的,可删去,另外print的message
有bug,head是空就说是empty list,但因为用了递归,head->next为null时,不代表它是
empty list
手痒写了个,但没验证...
void reversePrint(Node * head){
if(!head){
cout << "the end ..." << endl; // print some message here if needed...
return;
}
reversePrint(head->next);
cout << head->data << endl;
}
s*****y
发帖数: 897
24
http://zebozhuang.blog.163.com/blog/static/17147980420112710523
在标准库算法中,next_permutation应用在数列操作上比较广泛.这个函数可以计算一组
数据的全排列.但是怎么用,原理如何,我做了简单的剖析.
首先查看stl中相关信息.
函数原型:
template
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last
);
template
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last,
BinaryPredicate _Comp
);
两个重载函数,第... 阅读全帖
s*****y
发帖数: 897
25
http://zebozhuang.blog.163.com/blog/static/17147980420112710523
在标准库算法中,next_permutation应用在数列操作上比较广泛.这个函数可以计算一组
数据的全排列.但是怎么用,原理如何,我做了简单的剖析.
首先查看stl中相关信息.
函数原型:
template
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last
);
template
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last,
BinaryPredicate _Comp
);
两个重载函数,第... 阅读全帖
i**********e
发帖数: 1145
26
来自主题: JobHunting版 - 新鲜onsite面经
我写的 boggle 游戏算法,DFS + trie.
一秒以内给出所有 5x5 的答案。
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct Trie {
bool end;
Trie *children[26];
Trie() {
end = false;
memset(children, NULL, sizeof(children));
}
void insert(const char *word) {
const char *s = word;
Trie *p = this;
while (*s) {
int j = *s-'A';
assert(0 <= j && j < 26);
if (!p->childre... 阅读全帖
s*****y
发帖数: 897
27
来自主题: JobHunting版 - 问个编程题
#include
#include
#include
#include
using namespace std;
vector FindSet(int target, const vector &input)
{
vector rtn;
int dp[10][100]; //dp table to save the result
bool used[10][100];
for (int i = 0; i <= target; i++) {
dp[0][i] = 0;
if (i <= 1) {
dp[1][i] = i;
used[1][i] = true;
} else {
dp[1][i] = INT_MAX;
used[1][i] = false;
}
}
for ... 阅读全帖
e***s
发帖数: 799
28
来自主题: JobHunting版 - 问个编程题
#include
#include
#include
#include
#include
using namespace std;
vector FindSet(unsigned int sum, vector &coinset)
{
vector rtn;
int *Min = new int[sum + 1];
vector *Combination = new vector[sum + 1];
Min[0] = 0;
unsigned int i;
unsigned int j;
for(i = 1; i <= sum; i++)
{
Min[i] = INT_MAX;
}
for(i = 1; i <= sum; i++)
{
for(j = 0; j < co... 阅读全帖
b******c
发帖数: 70
29
来自主题: JobHunting版 - G题讨论
#include
#include
using namespace std;
int main() {
//int a[8] = {101,2,3,104,5,103,9,102};
int a[9] = {100,3,200,1,2,4,3,6,7};

// Assume at least one number
int L = sizeof(a)/sizeof(a[0]);
cout << "total: " << L << endl;

map ht;
int max = 1;
int start = a[0], end = a[0];
// Scan and store number of consecutive numbers
for (int i = 0; i < L; ++i)
{
if (ht.find(a[i]) != ht.end())
continue;

int c1 = 0;
if (ht.find(a[i]+1)... 阅读全帖
g*****k
发帖数: 623
30
来自主题: JobHunting版 - G题讨论
似乎不对,
举个例子吧,假设有2个区段,[m..i], [i+2..j]
并且下一步处理 i+1和j-1
处理i+1将使得ht[m]=ht[j]=j-m+1
但是处理j-1的时候,假设ht[j-2]=t(some old value)
之后ht[j]=j-m+1+t是错误的,因为hast table ht[i]无法区分是以i开始,还是以i结
束。

#include
#include
using namespace std;
int main() {
//int a[8] = {101,2,3,104,5,103,9,102};
int a[9] = {100,3,200,1,2,4,3,6,7};

// Assume at least one number
int L = sizeof(a)/sizeof(a[0]);
cout << "total: " << L << endl;

map ht;
int max = 1;
int start = a[0], end = a[0];
//... 阅读全帖
b******c
发帖数: 70
31
来自主题: JobHunting版 - G题讨论
#include
#include
using namespace std;
int main() {
//int a[8] = {101,2,3,104,5,103,9,102};
int a[9] = {100,3,200,1,2,4,3,6,7};

// Assume at least one number
int L = sizeof(a)/sizeof(a[0]);
cout << "total: " << L << endl;

map ht;
int max = 1;
int start = a[0], end = a[0];
// Scan and store number of consecutive numbers
for (int i = 0; i < L; ++i)
{
if (ht.find(a[i]) != ht.end())
continue;

int c1 = 0;
if (ht.find(a[i]+1)... 阅读全帖
g*****k
发帖数: 623
32
来自主题: JobHunting版 - G题讨论
似乎不对,
举个例子吧,假设有2个区段,[m..i], [i+2..j]
并且下一步处理 i+1和j-1
处理i+1将使得ht[m]=ht[j]=j-m+1
但是处理j-1的时候,假设ht[j-2]=t(some old value)
之后ht[j]=j-m+1+t是错误的,因为hast table ht[i]无法区分是以i开始,还是以i结
束。

#include
#include
using namespace std;
int main() {
//int a[8] = {101,2,3,104,5,103,9,102};
int a[9] = {100,3,200,1,2,4,3,6,7};

// Assume at least one number
int L = sizeof(a)/sizeof(a[0]);
cout << "total: " << L << endl;

map ht;
int max = 1;
int start = a[0], end = a[0];
//... 阅读全帖
z**********n
发帖数: 3
33
来自主题: JobHunting版 - facebook的一道题
#include
#include
using namespace std;
class Matrix
{
public:
Matrix(int r, int c)
{
pData = new int[r * c];
_rowNum = r;
_colNum = c;
}
// TODO pData is a pointer, so deep copy struct is needed
// TODO write copy structor and assign operateo
//Matrix(Matrix& m)
//{
//}
//Matrix& operator = (Matrix& m)
//{
//}
void SetNum(int r, int c, int num)
{
pData[r * _colNum + c] = num;
}
const i... 阅读全帖
u**r
发帖数: 663
34
来自主题: JobHunting版 - 问个amazon面试题
我从中间开始交换,假设输入数组存储为a[1...n,n+1...2n]
从a[n],a[n+1]开始,每次把a[n]和a[n+1]交换到正确位置去,直到a[n]=n,a[n+1]=n+1
为止,然后处理a[n-1],a[n+2],知道最外头,这样每次交换保证两个数位于正确位置
,所以最多交换次数就是n.
大家看看有没有问题?
void main()
{
int n = 20,k,t,tt;
int* c = new int [2*n];
for (int i=0;i<2*n;i++)
{
c[i] = l(i+1,n);
cout< }
cout< int idx1 = n, idx2=n+1;
for(idx2=n+1;idx2<=2*n;idx2++,idx1--)
{
while (c[idx2-1] != idx2)
{
t = c[idx2-1];
c[idx... 阅读全帖
c**********e
发帖数: 2007
35
来自主题: JobHunting版 - amazon 2nd phone interview
Quick Selection:
选target个最小的。最终目的是,使a[0], …, a[target-1]为最小的。因为是递归调用子函数,子函数对数组的一部分a[low], …, a[high]操作。
#include
using namespace std;
#define swap(x,y) { int z=x; x=y; y=z; }
int low_n(int a[], int low, int high, int target) {
int pivot; int mid=(low+high)/2;
if(a[low]<=a[mid])
{
if(a[high]<=a[low]) pivot=a[low];
else if(a[high]<=a[mid]) pivot=a[high];
else pivot=a[mid];
} else
{
if(a[high]<=a[mid]) pivot=a[mid];
else if(a[high]<=a[low]) pivot=a[high];
... 阅读全帖
S**I
发帖数: 15689
36
来自主题: JobHunting版 - C++ Q83: 这个const_cast什么意思?
The result is undefined behavior. When a variable is declared const, the
compiler can store the variable anywhere it wants (may be read-only memory
location), when you cast away the constness of this variable and make
assignment, the compiler is forced to create a variable on the stack. So in
your example, a and const_cast(a) could be two different variables and
have different memory location.
If you run the following code, you can see the difference:
const int a = 2;
int b = &cons... 阅读全帖
l*********y
发帖数: 142
37
来自主题: JobHunting版 - 这个facebook puzzle样题怎么做?
顺手写了一个,46min整。也没用状态压缩,待会看一下gloomyturkey的code。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 9;
const int maxk = 6;
int N, K;
struct State {
State() {
for (in... 阅读全帖
c**********e
发帖数: 2007
38
来自主题: JobHunting版 - C++ Q96: function inheritance
Faint. I made such a low level mistake. Then what is the output of the
following code?
#include
using namespace std;
class A {
public:
bool equals(A a) { return data==a.data; }
void setData(int input) { data=input; }
private:
int data;
};
class B: public A {};
void main() {
A a;
a.setData(7);
B b;
b.setData(7);
cout << a.equals(b) << endl;
cout << b.equals(a) << endl;
}
C***y
发帖数: 2546
39
来自主题: JobHunting版 - 问个C++ virtual function的问题
有没有办法强制基类中定义的pure virtual function在所有的继承类中实现
For example:
class A
{
public:
virtual void func() = 0;
};
class B: public A
{
public:
void func() { std::cout<<"In B"< };
class C: public B
{
public:
void func() { std::cout<<"In C"< };
class C中也必须实现 func,否则报错
C***y
发帖数: 2546
40
来自主题: JobHunting版 - 问个C++ virtual function的问题
有没有办法强制基类中定义的pure virtual function在所有的继承类中实现
For example:
class A
{
public:
virtual void func() = 0;
};
class B: public A
{
public:
void func() { std::cout<<"In B"< };
class C: public B
{
public:
void func() { std::cout<<"In C"< };
class C中也必须实现 func,否则报错
c**********e
发帖数: 2007
41
来自主题: JobHunting版 - C++ Q97: reference and virtual
What is the output of the following code? Why?
#include
using namespace std;
class student {
public:
virtual void sleep() { cerr << "student sleep" << endl; }
};
class grad: public student {
public:
virtual void sleep() { cerr << "grad sleep" << endl; }
};
void exam(student& stu) {
stu.sleep();
}
void main() {
grad g;
exam(g);
}
c**********e
发帖数: 2007
42
What is the output of the following code? Why?
#include
using namespace std;
class base {
public:
void pay() { cout << "Base::pay" << endl; }
virtual void eat() { pay(); }
};
class derived: public base {
public:
void pay() { cout << "Derived::pay" << endl; }
};
void main() {
base* p = new derived;
p->eat();
}
x***i
发帖数: 64
43
来自主题: JobHunting版 - 请教一道面试题,关于tree的
贴一下我的code吧
string pre2post(string pre)
{
string temp, tmp_pos;
int i = 0;
stack ops;
while (i < pre.size())
{
temp = pre.substr(i,1);
if (pre[i]=='-' || pre[i]=='+' ||
pre[i]=='*' || pre[i]=='/')
{
ops.push(temp);
}
else {
while (!ops.empty() &&
!((tmp_pos=ops.top()).size()==1 &&
(tmp_pos[0]=='-' || tmp_pos[0]=='+' ||
tmp_pos[0]=='... 阅读全帖
r******n
发帖数: 170
44
来自主题: JobHunting版 - 请教一道面试题,关于tree的
贴个tree的版本,不过空string对应空tree的时候,似乎空指针处理的有些ugly,求更
简洁版本。
struct BNode
{
char c;
BNode* left;
BNode* right;
BNode():c(0){}; //avoid uninitialized value for c
BNode(char _c):c(_c),left(NULL),right(NULL){};
};
void pre2bst(string pre, size_t& start, size_t len, BNode* &p)
{
if(start {
p=new BNode(pre[start]);
size_t curr=start;
start++;
if(pre[curr]=='+'||pre[curr]=='-'||pre[curr]=='*'||pre[curr]=='/')
{
pre2bst(pre,s... 阅读全帖
w****x
发帖数: 2483
45
来自主题: JobHunting版 - 上个题大家给评评 (G家的)
先对起点排序,排好了用下面的函数
void PrintMerge2(int a[], int n)
{
assert(a && n > 0 && n%2 == 0);
int nBeg = a[0];
int nEnd = a[1];
for (int i = 2; i < n; i += 2)
{
if (a[i] > nEnd)
{
cout< nBeg = a[i];
nEnd = a[i+1];
}
else
{
if (a[i+1] > nEnd)
... 阅读全帖
w****x
发帖数: 2483
46
来自主题: JobHunting版 - 上个题大家给评评 (G家的)
先对起点排序,排好了用下面的函数
void PrintMerge2(int a[], int n)
{
assert(a && n > 0 && n%2 == 0);
int nBeg = a[0];
int nEnd = a[1];
for (int i = 2; i < n; i += 2)
{
if (a[i] > nEnd)
{
cout< nBeg = a[i];
nEnd = a[i+1];
}
else
{
if (a[i+1] > nEnd)
... 阅读全帖
q****x
发帖数: 7404
47
来自主题: JobHunting版 - An example of strategy pattern
/*
Is template method in parallel with strategy to abstract different algorithms? If so, how to modify the following example to template method?
*/
#include
using namespace std;
class Robot;
class Search {
public:
virtual void apply(Robot*) = 0;
};
class Linear: public Search {
public:
void apply(Robot*) { cout << "linear" << endl;}
};
class Spiral: public Search {
public:
void apply(Robot*) { cout << "spiral" << endl;}
};
class Robot {
public:
void go() { m_search->ap... 阅读全帖
A**u
发帖数: 2458
48
来自主题: JobHunting版 - 算法题求教
这个题目我刚好练过
这里有介绍
http://www.geeksforgeeks.org/archives/14469
用backtracking.
关键是,这个树的有些node, 可以不用访问
比如 你要找9.
假设从1开始那么,到达(1,2,4)node.
(1,2,4)的和 加上 {5,7,8}的最小值 > 9.
所以不用考虑 包含1,2,4的node了,这样可以可以排序不少点
这是我的算法
#include
#include
#include
using namespace std;
void re(int depth, int sum, vector selected,int w[], int N, int target){
if ( depth == N )
{
if ( sum == target )
{
for(vector::iterator it = selected.begin(); it != selecte... 阅读全帖
f*******t
发帖数: 7549
49
来自主题: JobHunting版 - 不用暴力,这道题有没有优化解
写了很长的代码,感觉不太对,如果一个数组里面有重复的数,应该会输出重复的组合
,但程序实际运行结果貌似是没有,不知道为什么。
#include
#include
#include
#include
#include
#define N 5
using namespace std;
struct combination {
int indices[N];
int sum;
combination();
bool operator<(const combination& c) const;
void operator=(const combination& c);
bool operator() (const combination& c) const;
bool operator== (const combination& c) const;
};
combination::combination()
{
sum = 0;
f... 阅读全帖
w*******s
发帖数: 96
50
If the string is not the same length, my code will crash because of array
index out of boundary. Can you help find the bug?
//Refer Algorithm 4th, page 722
void exch(string &a, string &b)
{
string c = a;
a = b;
b = c;
}
int compare(int a, int b)
{
if (a if (a==b) return 0;
if (a>b) return 1;
}
void ThreeWayQsort(vector &v, int low, int high, int d)
{
if (high<=low) return;
//3 way partition to handle duplicate
int lt = low;
int gt = h... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)