n*****3 发帖数: 1584 | 1 你说的对, 这不是大数据。
因为在modeling training stage, 用 R 跑prototype system,
R itself 不 方便 并行。 下一阶段 会上 spark/hadoop。
继续 抛砖引玉一下。 下面的 CPP file 跑得还行。 用 sourceRCPP call
就可。 看看有什么可以改进的,
再次 谢谢大家 all
#include
using namespace Rcpp;
using namespace std;
// Below is a the balancing C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp
// function (or via the Source button on the editor toolbar)
// For more on using Rcpp click the Help button on the editor toolbar
// [... 阅读全帖 |
|
O******5 发帖数: 483 | 2 #include
int main(int argc, char * argv[]){
char sb;
std::cin >> sb;
switch (sb) {
case 'x':
std::cout << "xiaxianyue is a sb!\n";
break;
case 'l':
std::cout << "laocat is a sb!\n";
break;
case 'm':
std::cout << "mifepristone is a sb!\n";
break;
default:
std::cout << "You are a sb!\n";
break;
}
return 0;
} |
|
S******D 发帖数: 8437 | 3 #include
int main(int argc, char * argv[]){
std::cout << "xiaxianyue: I love CHAO_CHUI! I always have one more than
last time!\n\n";
std::cout << "How many CHAO_CHUI did xiaxianyue have?\n\n";
std::cout << "Please enter a number: \n\n";
int sex;
std::cin >> sex;
int times = 1;
int CHAO_CHUI = 0;
while (times <= sex) {
CHAO_CHUI = CHAO_CHUI + times;
++times;
}
std::cout << "xiaxianyue: I had " << CHAO_CHUI << " CHAO_CHUI!" <<"\n";
return 0;
} |
|
w********p 发帖数: 948 | 4 问题继续, 包子继续
class Base
{
public:
Base(){ cout<<"Constructor: Base"<
~Base(){ cout<<"Destructor : Base"<
};
class Derived: public Base
{
public:
Derived(){ cout<<"Constructor: Derived"<
~Derived(){ cout<<"Destructor : Derived"<
};
int main()
{
Derived Var1;
Base Var2(Var1);
Derived Var3(Var1);
return 0;
}
output:
Constructor: Base
Constructor: Derived
Destructor : Derived
Destructor : Base
Destructor : Base
Destructor : Derived
Destructor : Base
请解释一下 |
|
n*******s 发帖数: 482 | 5 code :
class Base{
public:
Base(){cout << "Base()" << endl;}
Base(const Base&){cout << "Base(const Base&)" << endl;}
~Base(){cout << "~Base()" << endl;}
};
Base func(Base b){return b;}
void test1(){
Base a;
func(a);
}
void test2(){
Base a;
Base b = func(a);
}
int main(){
test1();
cout<<"----------"<
test2();
return 0;
}
// from the output,
test1() will
1. first call Base's default constructor for "Base a"
2. call Base's copy constructor since parameter is passed by value
3. call Base |
|
c*****o 发帖数: 178 | 6 第3题可不可以用global variable呢?
int* input;
static void Foo(int);
int _tmain(int argc, _TCHAR* argv[])
{
int size = 100;
try{
Foo(size);
::free(input);
cout<<"free memory in Main function"<
}
catch(exception &e){
cout<
}
return 0;
}
void Foo(int size){
try{
input = (int*)(::malloc(size));
cout<<"allocate " <
}
catch(exception &e){
cout< |
|
P*******b 发帖数: 1001 | 7 f2()为啥比f1()多调用一次copy ctor
#include
struct C
{
C() { std::cout << "Ctor" << std::endl;}
C(const C&) { std::cout << "A copy was made.\n"; }
};
C f1(C c)
{
C first;
return first;
}
C f2(C c)
{
C first;
return c;
}
int main()
{
C first;
std::cout << "-----------" << std::endl;
C second = f1(first);
std::cout << "-----------" << std::endl;
f2(second);
} |
|
c**********e 发帖数: 2007 | 8 #include
using namespace std;
class A {
public:
A() { cout<<"A_constructor "; f(); }
virtual void f() { cout<<"A_f "; }
};
class B: public A {
public:
B() { cout<<"B_constructor "; f(); }
virtual void f() { cout<<"B_f"; }
};
int main()
{ B b; }
What is the output?
a) A_constructor A_f B_constructor A_f
b) A_constructor A_f B_constructor B_f
c) Cannot call f() in A's constructor
d) None of above. |
|
i**********e 发帖数: 1145 | 9 这是我的代码,虽然没有那么漂亮简洁,但是比较容易懂。
我可以尝试把代码缩短,但是缩短之后就很难理解了。
void post_order_traversal_iterative(BinaryTree* root) {
bool down = true;
BinaryTree *prev;
stack s;
s.push(root);
while (!s.empty()) {
BinaryTree *curr = s.top();
if (down) {
if (curr->left) {
s.push(curr->left);
} else {
if (curr->right) {
s.push(curr->right);
} else {
down = false;
cout << curr->data << " ";
s.pop();
prev = cu... 阅读全帖 |
|
B********n 发帖数: 384 | 10 是否先用最大的无所谓吧,感觉用简单的循环就可以实现了.
void PrintCoins(int n)
{
int i, j, k, l;
for (i = 0; i <= n/5; i++)
for (j = 0; j <= (n-5*i)/3; j++)
{
k = (n-5*i-3*j)/2;
if (k*2 == n-5*i-3*j)
{
for (l = 0; l < i; l++)
cout << "5 ";
for (l = 0; l < j; l++)
cout << "3 ";
for (l = 0; l < k; l++)
cout << "2 ";
cout << endl;
... 阅读全帖 |
|
E**h 发帖数: 6 | 11 void Print_Fibonacci_Reverse(uint32_t n)
{
static uint32_t n0 = 0;
static uint32_t n1 = 1;
if (0 == n)
{
cout << n0 << endl;
return;
}
if (1 == n)
{
cout << n1 << endl;
cout << n0 << endl;
return;
}
uint32_t result = n1 + n0;
n0 = n1;
n1 = result;
Print_Fibonacci_Reverse(n - 1);
result = n1 - n0;
n1 = n0;
n0 = result;
cout << result << endl;
return;
} |
|
b*****n 发帖数: 482 | 12 How to check the top 2 objects? It seem we can only look at the top
object.
Another question is we have to check N-1 chars from the top of the
stack, where N is the length of the str2. If there are multiple
characters in str2 that are the same as its last char (e.g. accccbc),
then even if str1 is the same as string 2, we have to do extra check 4
times whenever we meet a 'c' in str1.
My idea is to have a companion array recording the maximum matched index
of the char sequence in str1. If there is... 阅读全帖 |
|
t******r 发帖数: 209 | 13 我贴个非递归的解法
// 从外向内螺旋打印1-n
void SpiralPrint(int n){
// 计算二维数组的维数k
int k = ceil(sqrt((float)n));
// 动态分配二维数组
int **a = new int*[k];
for(int m = 0; m < k; m++){
a[m] = new int[k];
}
// 初始化数组元素值为-1
for(int x = 0; x < k; ++x){
for(int y = 0; y < k; ++y) {
a[x][y] = -1;
}
}
// 起始数字
int num = 1;
int flag = 1;
// 起始位置
int i = 0;
int j = 0 ;
// 填数
while(true){
// 所有数都填完了么?
... 阅读全帖 |
|
h*****g 发帖数: 312 | 14 zz:
原文:
http://blog.csdn.net/njnu_mjn/archive/2010/04/04/5449098.aspx
八皇后问题(C++) 收藏
1 、问题描述: 在一个8*8 的棋盘上放置8 个皇后,不允许任何两个皇后在棋盘的同
一行、同一列和同一对角线上。
2 、关键字: 递归、上溯
3 、技巧:
1 )、
经观察发现,对8 x 8 的二维数组上的某点a[i][j](0<=i,j<=7)
其主对角线(即左上至右下)上的每个点的i-j+7 的值(范围在(0,14) )均相等;
其从对角线(即右上至左下)上的每个点的i+j 的值(范围在(0,14) )均相等;
且每个主对角线之间的i-j+7 的值均不同,每个从对角线之间的i-j+7 的值亦不同;
如a[3][4]:
主:3-4+7=6
从:3+4=7
因此可设两个数组b[15],c[15] 分别表示主、从对角线是否安全
(为1 表示有皇后,不安全;为0 表示安全)
2 )、
每行有且仅有一个皇后:
每i 个皇后放在每i 行(0<=i<=7)
void eightQueens( int line );
4 、源码(... 阅读全帖 |
|
e***s 发帖数: 799 | 15 来自主题: 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... 阅读全帖 |
|
u**r 发帖数: 663 | 16 我从中间开始交换,假设输入数组存储为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... 阅读全帖 |
|
r******n 发帖数: 170 | 17 贴个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*******r 发帖数: 2953 | 18 程序和运行结果如下,不考虑瘸子的情况。
#include
using namespace std;
class human
{
public:
human();
human(bool, bool);
~human(){};
bool getEyeState();
bool getMouthState();
char *getHealthDesc();
private:
bool isBlind;
bool isDeaf;
};
human::human()
{
isBlind = 0;
isDeaf = 0;
}
human::human(bool blind, bool deaf)
{
isBlind = blind;
isDeaf = deaf;
}
bool human::getEyeState()
{
return isBlind;
}
bool human::getMouthState()
{
return isDeaf;
}
char *hu... 阅读全帖 |
|
w*******s 发帖数: 96 | 19 有没有对Careercup的C/C++的Solution感兴趣的同学啊?准备做几个样题和我的实现放
上来大家讨论讨论:先来一个拍拍
////////////////////////////////////////////////////////////////////////////
////////
// Problem 3.6: (第4版)
//
// Analysis and points:
// 1. You are asking to implement one sort algorithm with input
parameter is a stack.
// 2. If we allow to use extra space, we can use another stack to
help. Each time, we
// insert the top element into the proper postion. The stack is
used each other as
// buffer ... 阅读全帖 |
|
c*****e 发帖数: 737 | 20 1 #include
2
3 class A
4 {
5 public:
6 A(int v_) : v(v_) {};
7 virtual void foo() {std::cout << "A:" << v << "\n";}
8 int v;
9 };
10
11 class B : public A
12 {
13 public:
14 B(int w_, int z_):w(w_), A(z_){};
15 virtual void foo() {std::cout << "B:" << w << "\n";}
16 int w;
17 };
18
19 class C : public A
20 {
21 public:
22 C(int k): A(k){};
23 v... 阅读全帖 |
|
i**********e 发帖数: 1145 | 21 我刚在linux上运行了,没有这方面的问题,不过我的程序是C++ 不是Java。
我测试了你的算法,返回结果都算满意,而且运行越多次,返回的结果越接近理想的 p
值。
你的算法不单简洁,而且复杂度是最优的,递归是 average 两次就返回答案。
expected # calls = 1*0.5 + 2*0.5^2 + 3*0.5^3 + ...
= Summation( k * 0.5^k ), k = 1 .. n
= 2 - (n+2)/2^n
When n = infinity,
expected # calls = 2
贴下code方便大家做测试:
#include
#include
#include
#include
using namespace std;
bool rand2() {
return (rand() % 2 == 0);
}
unsigned long long totalCalls = 0;
bool Prob2(double p, bool expected = tru... 阅读全帖 |
|
i**********e 发帖数: 1145 | 22 我刚在linux上运行了,没有这方面的问题,不过我的程序是C++ 不是Java。
我测试了你的算法,返回结果都算满意,而且运行越多次,返回的结果越接近理想的 p
值。
你的算法不单简洁,而且复杂度是最优的,递归是 average 两次就返回答案。
expected # calls = 1*0.5 + 2*0.5^2 + 3*0.5^3 + ...
= Summation( k * 0.5^k ), k = 1 .. n
= 2 - (n+2)/2^n
When n = infinity,
expected # calls = 2
贴下code方便大家做测试:
#include
#include
#include
#include
using namespace std;
bool rand2() {
return (rand() % 2 == 0);
}
unsigned long long totalCalls = 0;
bool Prob2(double p, bool expected = tru... 阅读全帖 |
|
s******n 发帖数: 3946 | 23 螺旋打印:
int array[N][N];
for (int i=0; i
for (int j=i; j
for (int j=i; j
for (int j=N-i-1; j>0; --j) cout<
for (int j=N-i-1; j>0; --j) cout<
} |
|
s******n 发帖数: 3946 | 24 螺旋打印:
int array[N][N];
for (int i=0; i
for (int j=i; j
for (int j=i; j
for (int j=N-i-1; j>0; --j) cout<
for (int j=N-i-1; j>0; --j) cout<
} |
|
s********y 发帖数: 40 | 25 写了个C++版本的螺旋打印。。
void printCircular(int** data,int rows, int cols)
{
int minRow = 0;
int minCol = 0;
int maxRow = rows-1;
int maxCol = cols-1;
while(minRow <= maxRow && minCol <=maxCol)
{
for(int j = minCol; j <= maxCol; j++)
std::cout<
minRow++;
for(int i = minRow; i <= maxRow; i++)
std::cout<
maxCol--;
for(int j = maxCol; j >= minCol; j--)
std::cout<阅读全帖 |
|
a***y 发帖数: 50 | 26 俺写了一个, 没有测试过只有1, 2, 或者0 的情况...
请高手指正...
思路就是维护了三个指针p0, p1, p2, 分别指向了数组index最小的0, 1, 2值所在位置,
然后遍历的过程中,不断swap(值和指针同时swap), 复杂度为O(n), 常数空间.
请高手们多指正了!
#include
#include
using namespace std;
class MITbbSolver
{
public:
MITbbSolver() {}
virtual ~MITbbSolver() {}
public:
void swap_pv(int* &p1, int* &p2) {
int tmp = *p1;
*p1 = *p2;
*p2 = tmp;
int* p = p1;
p1 = p2;
p2 = p;
return;
}
void sort_012_array(int* ... 阅读全帖 |
|
a**U 发帖数: 115 | 27 来源于面试题目,下面的code可以编译运行成功。关键是我觉得不应该编译运行成功,
请看下面我的comments。请高手指点。
#include
using namespace std;
class A
{
public:
virtual void test(){ cout <<"test A"<
};
class B : public A
{
public:
virtual void test(){ cout <<"test B"<
};
class C : public A
{
int value;
public:
C(){value = 1; }
virtual void test(){ cout <<"test C"<
void accessValue() { cout << "value=" << value << endl; }
void setValue(int value){ this->value... 阅读全帖 |
|
B*******1 发帖数: 2454 | 28 似乎可以吧,刚测试了一下,没有发现不对啊。
测了这几个例子
72 cout << "-----------------Test 1--------------------" << endl;
73 int test1[] = {1, -2, 4, -2, 6, 7};
74 getSubArray(test1, sizeof(test1)/sizeof(int), 7);
75
76 cout << "-----------------Test 2--------------------" << endl;
77 int test2[] = {1,1,1,1,-1};
78 getSubArray(test2, sizeof(test2)/sizeof(int), 3);
79
80
81 cout << "-----------------Test 3--------------------" << endl;
82 int test3[] = {1,1,1,1,0,-1,0,-1,1};
83 getSubAr... 阅读全帖 |
|
i*********7 发帖数: 348 | 29 int* findmaxsumarray(int *array, int length, int maxsum){
int *sum = new int[length + 1];
sum[0] = 0;
for(int i = 1; i <= length; i++)
sum[i] = sum[i - 1] + array[i - 1];
int *max = new int[length + 1];
int *min = new int[length + 1];
for(int i = 0;i<=length;i++)
cout<
cout<
max[0] = 0;
for(int i = 1; i < length + 1;i++)
{
max[i] = std::max(max[i - 1], sum[i]);
}
min[length] = sum[length];
for(int i = ... 阅读全帖 |
|
g*******n 发帖数: 214 | 30 是那道打印括号的题目:addValidParenthesis是一个另一个函数,返回的结果是正确
的,因为我不用指针的话(如下面代码)是可以答应出来的。
vector Parenthesis::generateParenthesis(int n){
vector list;
addValidParenthesis(n-1, n, &list, "(");
/*cout<
for(int i=0;i
cout<
}
cout<
return list;
}
但是如果改为vector* 返回类型,并且上面代码只修改为return &list。在外
面用
vector* result = Parenthesis::generateParenthesis(3);
用 cout << (*result)[0] << endl;是打印不出来东西
请... 阅读全帖 |
|
l*******b 发帖数: 2586 | 31 嗯。。我是按这个写的,感觉比翻转容易写错,改了3,4遍
void reconstruct(string &s) {
/* reconstruct minimal lexical seq with DIDI */
int n = s.size();
int m = 1; // minimal current availabe number
int i = 0;
while(i < n) {
if(s[i] == 'I') {
cout << m++ << " "; // output
i++;
}
else {
int j;
for(j = i; j < n && s[j] != 'I'; ++j)
;
int t = m + j -... 阅读全帖 |
|
Y********f 发帖数: 410 | 32 学习了,今天花时间仔细看了一下morris traversal,同时练习了一下,测试了一个
case。
Node *getNext(Node* &bst)
{
Node *cur = bst;
while(cur)
{
if (cur->lch == NULL)
{
bst = cur->rch;
return cur;
}
Node *prev = cur->lch;
while(prev->rch && prev->rch != cur)
prev = prev->rch;
if (!prev->rch)
{
prev->rch = cur;
cur = cur->lch;
}
else
{
prev->rch = NULL;
bst ... 阅读全帖 |
|
b********s 发帖数: 1676 | 33 没看懂,还是多谢code了。测试了一下,貌似second max不对。。。不管k换成3还是4
,都给第一个72,第二个是69,第三个是70.
int main()
{
int a[10]={39,31,34,53,24,70,42,69,72,44};
int *p;
int size = 0;
p = findPath(a,0,10,size);
cout<<"max="<
cout<<"second max="<
cout<<"p[3]="<
cout<<"p[4]="<
getchar();
return 1;
} |
|
d**********x 发帖数: 4083 | 34 实际上是O(logn)的空间,但是既然长度为2^32的数列可以借助一个int来作为辅助空间
,这点overhead基本可以忽略不计了。欢迎找bug:
#include
#include
#include
using namespace std;
#include
static int get_index(int i) {
if (i == 0) {
return 1;
}
return (1 << i) + 1;
}
void rearrange(vector& vec) {
int bitmap = 0; //"O(1)" space for length < (1 << 32)
int k = vec.size() / 2;
int n = vec.size();
for (int i = 0; get_index(i) < k; ++i) {
int cur = get_index(i);
... 阅读全帖 |
|
o******3 发帖数: 91 | 35 决定把题目跟我写的很挫的代码贴上来,有需要的同学可以来看。。。
代码可以过sample test
但是目前还不能过所有的Test
题目:
There are K pegs. Each peg can hold discs in decreasing order of radius when
looked from bottom to top of the peg. There are N discs which have radius 1
to N; Given the initial configuration of the pegs and the final
configuration of the pegs, output the moves required to transform from the
initial to final configuration. You are required to do the transformations
in minimal number of moves.
A move consists of picking the topm... 阅读全帖 |
|
o******3 发帖数: 91 | 36 决定把题目跟我写的很挫的代码贴上来,有需要的同学可以来看。。。
代码可以过sample test
但是目前还不能过所有的Test
题目:
There are K pegs. Each peg can hold discs in decreasing order of radius when
looked from bottom to top of the peg. There are N discs which have radius 1
to N; Given the initial configuration of the pegs and the final
configuration of the pegs, output the moves required to transform from the
initial to final configuration. You are required to do the transformations
in minimal number of moves.
A move consists of picking the topm... 阅读全帖 |
|
s******d 发帖数: 424 | 37 也要面A,不是Apple,攒人品
#define N 1000
// 多少个重复的都可以
#define DU 7
int _tmain(int argc, _TCHAR* argv[])
{
vector v;
int i,j;
for(i = 0; i<=N; i++)
v.push_back(i);
for(i = 0; i<=DU; i++)
v.push_back(i);
random_shuffle(v.begin(), v.end());
copy(v.begin(), v.end(), ostream_iterator(std::cout, " "));
cout<
for(i=0;i
{
while(v[i] != i && v[v[i]] != v[i])
swap(v[i], v[v[i]]);
}
copy(v.begin()+N+... 阅读全帖 |
|
s*w 发帖数: 729 | 38 codeeval.com 上的一个题
给n个东西,每个东西有重量和价值,要求在重量小于给定值的时候,尽量价值大
在重量是浮点数的情况下,怎么做 bottomup 的 dp table?
我现在的解是 recursion, 而且没 memorization, 因为不知道怎么存这个 table.
帖下代码,请指教一下
#include
#include
#include
#include
#include
#include
using namespace std;
class Solution {
int weightLimit;
int n;
vector weights;
vector costs;
public:
Solution(const string &line) {
istringstream iss(line);
iss >> weightLimit;
int in... 阅读全帖 |
|
s********u 发帖数: 1109 | 39 今天在做波兰表达式的时候想到用一下stringstream。
试了一下,发现>>与<<的行为非常诡异,代码和输出如下:
string str = "abc xyz";
stringstream stream(str);
cout << stream.str() <
stream >> str;
cout << str <
stream << str;
cout << stream.str() <
stream >> str;
cout << str <
就是将一个字符串吐出来,再塞回去,这时候stream里的字符串是一样的。
但是再吐出来,就不是这个abc字符串了,而是他后面那个,也就是说abc是塞到流的最
后了。但是stream.str()却一致。太诡异了。 |
|
p****o 发帖数: 46 | 40 you can check output position for insert (tellp) and input position for
extraction (tellg).
string str = "abc xyz";
stringstream stream(str);
// tellp() returns 0; tellg returns 0;
cout << stream.str() <
stream >> str;
// extract: input moves from 0 to 3 (tellg() ==3), which is the
whitespace position
cout << str <
stream << str;
// insert: since no insertion so far, output position is still 0,
so it ... 阅读全帖 |
|
f**********t 发帖数: 1001 | 41 来自主题: JobHunting版 - f家店面题 int FlogCrossRiver(string river) {
if (river.empty()) {
return 0;
}
vector>> vp(river.size());
vp[0].emplace_back(1, 1);
int res = INT_MAX;
for (size_t i = 0; i < vp.size(); ++i) {
if (river[i] == '0') {
continue;
}
for (auto pr : vp[i]) {
if (i + pr.first >= vp.size()) {
res = min(pr.second, res);
} else if (river[i + pr.first] == '1') {
vp[i + pr.first].emplace_back(pr.first, pr.second + 1);
}
if... 阅读全帖 |
|
f**********t 发帖数: 1001 | 42 来自主题: JobHunting版 - f家店面题 int FlogCrossRiver(string river) {
if (river.empty()) {
return 0;
}
vector>> vp(river.size());
vp[0].emplace_back(1, 1);
int res = INT_MAX;
for (size_t i = 0; i < vp.size(); ++i) {
if (river[i] == '0') {
continue;
}
for (auto pr : vp[i]) {
if (i + pr.first >= vp.size()) {
res = min(pr.second, res);
} else if (river[i + pr.first] == '1') {
vp[i + pr.first].emplace_back(pr.first, pr.second + 1);
}
if... 阅读全帖 |
|
e*******i 发帖数: 56 | 43 来自主题: JobHunting版 - 请教一道题 Let N be the height of the complete binary tree that is stored in A[1....2^N
-1]
print()
{
while(true)
{
k=1<<(N-2);
cout<
if(2*k+1==2<
cout<
k=k+1;
}
} |
|
e*******i 发帖数: 56 | 44 来自主题: JobHunting版 - 请教一道题 Sorry. there was a bug. this should work
Let N be the height of the complete binary tree that is stored in A[1....2^N
-1]
print()
{
k=1<<(N-2);
while(true)
{
cout<
if(2*k+1==2<
cout<
k=k+1;
}
} |
|
h****g 发帖数: 105 | 45 来自主题: JobHunting版 - 电面题一个 int m=matrix.size();
int n=matrix[0].size();
int i=0;
while(i
int j=0;
int a=i;
do{
cout<
} while(a>=0&&j
i++;
cout<
}
int j=1;
while(j
int i=m-1;
int b=j;
do{
cout<
} while(i>=0&&b
j++;
cout<
} |
|
h****g 发帖数: 105 | 46 来自主题: JobHunting版 - 电面题一个 int m=matrix.size();
int n=matrix[0].size();
int i=0;
while(i
int j=0;
int a=i;
do{
cout<
} while(a>=0&&j
i++;
cout<
}
int j=1;
while(j
int i=m-1;
int b=j;
do{
cout<
} while(i>=0&&b
j++;
cout<
} |
|
l****s 发帖数: 75 | 47 nothing happens.
a and b are not initialized.
try this:
#include
class foo
{
private:
int a;
int *b;
public:
void print()
{
std::cout << a << std::endl;
std::cout << &a << std::endl;
std::cout << b << std::endl;
//std::cout << *b << std::endl;
}
};
void doSth()
{
foo f;
f.print();
}
void main()
{
doSth();
std::cin.get();
} |
|
c**********e 发帖数: 58 | 48 a is not initialized to 0, nor b is initialized to NULL. Try this
class foo
{
public:
// foo() {cout << a << " " << b << endl;};
int a;
int *b;
};
void doSth()
{
foo f;
cout << f.a << " " << f.b << endl;
}
int main() {
doSth();
// int a;
// cout << a << endl;
// int *b;
// cout << b << endl;
return 0;
} |
|
w*****d 发帖数: 105 | 49 经过实测,上面的代码有点问题:pthread里如果没有线程wait,条件变量signal是没
有任何效果的。
下面的代码是测试通过的C++代码:
CMutex m;
CCondition hc, oc;
set hq, oq;
void * H(void * arg) {
bool w = true;
m.lock();
cout<<"generate H in "<
hq.insert(pthread_self());
if(hq.size() > 1 && !oq.empty()){
hc.signal();
w = (hq.size() > 2);
if(w)
hc.signal();
oc.signal();
}
if(w)
hc.wait(m);
hq.erase(pthread_self());
cout<<"consume H in ... 阅读全帖 |
|
w****a 发帖数: 710 | 50 C++11的
condition_variable gOReady, gHReady;
int atomcountH = 2, atomcountO = 1;
void O() {
unique_lock l(gLock);
gHReady.wait(l, []{return atomcountO > 0;});
cout << "O" << endl;
if(--atomcountO == 0 && atomcountH == 0) {
atomcountH = 2;
atomcountO = 1;
cout << "water" << endl;
}
gOReady.notify_all();
}
void H() {
unique_lock l(gLock);
gOReady.wait(l, []{return atomcountH > 0;});
cout << "H" << endl;
if(--atomcou... 阅读全帖 |
|