由买买提看人间百态

topics

全部话题 - 话题: void
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
d*********8
发帖数: 2192
1
man dlopen
Glibc extensions: dladdr() and dlvsym()
Glibc adds two functions not described by POSIX, with prototypes
#define _GNU_SOURCE
#include
int dladdr(void *addr, Dl_info *info);
void *dlvsym(void *handle, char *symbol, char *version);
The function dladdr() takes a function pointer and tries to resolve
name and file where it is located. Information is stored in the Dl_info
strucâ€
ture:
typedef struct {
... 阅读全帖
A**u
发帖数: 2458
2
来自主题: Programming版 - 请教c++ non-vitura interface
在看c++ effective,
有点不明白 NVI(non virtual interface idiom)
书上的例子
class B{
public:
~B();
void run(){do();}
private:
virtual void do() const = 0;
}
class D:public B{
private:
vitural void do(){....}
}
我有些问题
D 不能 访问 B的 private 成员, 所以 D 必须重新定义 private 中的 do 函数,
而且 private的成员, 也不会发生动态绑定,那为什么还需要 virtual呢
void do(){} 在 B 中, void do(){....} 在B中不也一样吗
r****t
发帖数: 10904
3
来自主题: Programming版 - C++ online Test 又一题 (转载)
【 以下文字转载自 JobHunting 讨论区 】
发信人: huameng (huameng), 信区: JobHunting
标 题: C++ online Test 又一题
发信站: BBS 未名空间站 (Wed Mar 2 19:21:15 2011, 美东)
Which one of the following function declarations do you use for a function
that takes a variable number of parameters and uses all of them?
A.
void function (first, others[]);
B.
void function(argc, *argv[]);
C.
void function(...);
D.
void function(int first, ...);
E.
void[] function();
觉得b,d 都行啊~
A**u
发帖数: 2458
4
来自主题: Programming版 - 请教pthread producer-consumer问题
照书编了一个,
结果总是不对,请大牛看看问题出在哪里呢
#include
#include
#include
pthread_mutex_t region_mutex = PTHREAD_MUTEX_INITIALIZER;
sem_t items;
int b; /* buffer size = 1; */
void add_buffer(int i){
b = i;
}
int get_buffer(){
return b ;
}
void *producer(void*)
{
int i = 0;
std::cout <<"I'm a producer" << std::endl;
while (1) {
pthread_mutex_lock(®ion_mutex);
add_buffer(i);
pthread_mutex_unlock(®ion_mutex);
sem_post(&ite... 阅读全帖
d****n
发帖数: 1637
5
来自主题: Programming版 - 用c怎么实现generic stack (转载)
generic C 有两类,一类是 void *, 但是有转换消耗。
另一类是 C marco,但是难debug
对于你的要求,可变的size,当然要用C marco
///file gstack.c
#define _STACK_STRUCT(STACKNAME, DATATYPE) \
typedef struct STACKNAME { \
struct STACKNAME * next;\
DATATYPE value;\
}STACKNAME##_t;
#define __STACK_POP(STACKNAME, DATATYPE ) \
void pop_##STACKNAME( STACKNAME##_t *node ) \
{ \
;\
}
#define __STACK_PUSH(STACKNAME, DATATYPE ) \
void push_##STACKNAME( STACKNAME##_t *node ) \
{ \
... 阅读全帖
g***a
发帖数: 114
6
来自主题: Programming版 - C++class指针转换
thinking in C++ 的一道作业题,关于class的指针转换,想说明C中存在此类"hole",
而C++可以规避。可是自己编程发现程序结果与solution刚好相反,望指点。
原题如下
Create a class called bird that can fly( ) and a class rock that can’t.
Create a rock object, take its address, and assign that to a void*. Now take
the void*, assign it to a bird* (you’ll have to use a cast), and call fly(
) through that pointer. Is it clear why C’s permission to openly assign
via a void* (without a cast) is a “hole” in the language, which couldn’t
be propagated into C++?
solution:... 阅读全帖
y****e
发帖数: 23939
7
来自主题: Programming版 - Visual C++ 高手帮忙,一个Link Error
首先,这个程序在Linux下用g++编译通过,没有任何问题。但是在Visual C++下编译时
,编译通过,但是出现下面链接错误:
error LNK2019: unresolved external symbol _gsl_vector_ptr referenced in
function "void __cdecl refalidf(struct gsl_vector const *,void *,struct gsl_
vector *)" (?refalidf@@YAXPBUgsl_vector@@PAXPAU1@@Z)
有这样两个函数:
1. static double refalifn(const gsl_vector * v, void *params)
2. static void refalidf(const gsl_vector * v, void *params, gsl_vector *
df)
问题出在第二个函数上。如果只有第一个函数,程序在VC++下也能编译链接通过。可是
这两个函数实在没有太大差别啊?难道是第二个函数有两个gsl_vector *指针?
谢... 阅读全帖
k****e
发帖数: 126
8
来自主题: Programming版 - 问一个volatile和memcpy一起用的问题
C99里面对function call是这样规定的,6.5.2.2 Function calls:
If the expression that denotes the called function has a type that does
include a prototype, the arguments are implicitly converted, as if by
assignment, to the types of the corresponding parameters, taking the type of
each parameter to be the unqualified version of its declared type.
对于assignment是这样规定的,6.5.16.1 Simple assignment:
one operand is a pointer to an object or incomplete type and the other is a
pointer to a qualified or unqualified ver... 阅读全帖
c*****n
发帖数: 75
9
来自主题: Programming版 - 问一个简单C++问题
这个问题的确考了一些平时自己想当然而没有深究的东西。
1) int i = i;
这个相当于
int i; //define and allocate storage on stack.
i = i; // assignment operator.
所以 i 是 uninitialized. 选 A)
2) void main()
研究了一下。 对main()的prototype, C/C++ 不同的年代的标准有不同的要求
C-90: 应该return int, 但允许compiler自由加入对其他return type的support.
也就是说, void main(), float main()都可能被你的compiler 支持。
C-99: 同 C-90
C++: 只允许 int main();
gcc 选择了支持 void main().
g++ 没得选, 不能支持 void main().
由以下结果也可看出结论:
gcc -std=c99 // ok;
gcc -pedantic -std=c99 // warning: return type of ‘main’ is... 阅读全帖
s*w
发帖数: 729
10
又琢磨了两天,看了不少相关东西,终于搞定了,觉得自己对这个多线程的理解加强了
很多。思考比单纯的看人说原理更刻骨铭心。
这个问题我设计的用一个 producer 多个 consumer 的架构,上个版本的是两头用
condition_variable, 一个通知 producer 有空间了开始干活生产,另一个通知
consumer 有库存了开始消费。参见上篇里面的 wait 和 notify_all,notify_one 语
句。 这个思路对于单 producer 单 consumer 没问题,可是不适用于 多 consumer.
因为所有的 consumer 可能同时睡觉(没空存),同时醒来(有库存),结果只有一个
能抢占mutex(拿到库存),其他的只好继续睡觉(while 循环 wait). 如果无限制的
生产下去,每个睡觉的都有机会醒来干活。可是在有限生产的情况下,producer 干完
活了后,总有睡觉的 consumer 无人唤醒导致死锁。解决的办法就是用 non-block
的 try_lock, lock 不上就返回 false,这样有机会检查是否需要(还在生产或是有
... 阅读全帖
N********n
发帖数: 8363
11
来自主题: Programming版 - 看了一下C#的async await
Imagine you wanna run 3 async operations in a method like below:
void foo () {
// run async_op1, async_op2 & async_op3
}
W/ callbacks you'd have to break it down into several callbacks:
void foo () { run_async (op1, op1_callback);}
void op1_callback () {run_async (op2, op2_callback);}
void op2_callback () {run_async (op3, op2_callback);}
void op3_callback () {//finish the rest;}
Now you are forced to break the logic down into spaghetti callback
methods. If one async_op requires o... 阅读全帖
d****l
发帖数: 51
12
来自主题: Programming版 - 请教一个关于std::function的问题
根据编译的错误信息,processVector()的第二个变量被定义为std::function >,而在调用时传入的是std::_Bind_helper const std::_Placeholder<1>&>::type),编译器无法确定这二者是同一类型,因此报
错。你可以定义一个std::function变量然后把bind赋值给它(implicit
cast)之后在调用processVector时传入这个变量,或者直接static_cast (the
following code uses static_cast and works fine on my machine with gcc-4.8.0):
...
void testint()
{
...
processVector(arr, static_cast>(std::bind(&Foo::
print, this, _1)));
}
...
另外,二楼建议的写... 阅读全帖
c***s
发帖数: 15
13
来自主题: Programming版 - 请教一个C++的设计问题
//样本代码如下,把具体decoder方法在不同type上实现就行
#include
enum decoder_type
{
type_A = 0,
type_B = 1,
type_C = 2,
no_decoder_available = 3
};
template
class decoder_algorithm
{
};
template<>
struct decoder_algorithm
{
static bool is_compatible () { return false; }
static void decode() { std::cout << "decoder A" << std::endl; }
};
template<>
struct decoder_algorithm
{
static bool is_compatible () { return false; }
static void decode() { std::cout << "decoder B" ... 阅读全帖
m********e
发帖数: 177
14
我明白你说的通过时间计算pore volume的意思了。
其实是我一开始把概念搞混了,我最开始提到的pore volume其实想说的是column里的
void volume。一般我见到的是,column pack好了之后,知道长度和内径,先称一下没
有水的时候的质量,然后灌水至饱和,再称一下,两次质量之差除以水的密度就是void
volume;也有的是知道沙子的总质量(我们一般用沙子,所以一般不会碰到体积膨胀
或者dual porosity的问题),根据其密度,一般认为是2.65,也能换算出void volume
。我没自己做过试验,所以想知道这样测量换算出来的void volume和porosity到底有
多可靠。
后面pore volume的计算应该可以按时间来,因为一般都是设定好了流量,然后自动采
样的。但问题是,前面的void volume不准的话,必然会导致pore volume这个
dimensionless time的误差。不知道allah对此有什么看法。谢谢。
b****r
发帖数: 2555
15
☆─────────────────────────────────────☆
voidness (先有没有) 于 (Tue Aug 23 19:41:26 2011, 美东) 提到:
大拇指只有人有,其他动物没有。
你试试不用大拇指看看能干啥?除了刨地啥也干不了。
而大拇指跟其他手指的分离就像画龙点睛一样,手就
啥都能干了,也能创造工具了。
所以也才会有竖起大拇指说:OK!
是谁把拇指分出来的呢?不告诉你。
☆─────────────────────────────────────☆
Ranma (ranma 1/2) 于 (Tue Aug 23 19:45:11 2011, 美东) 提到:
黑猩猩表示淡定

☆─────────────────────────────────────☆
Ranma (ranma 1/2) 于 (Tue Aug 23 19:47:06 2011, 美东) 提到:
大猩猩表示压力很大
☆─────────────────────────────────────☆
voidness (先有没有) 于 (Tue Au... 阅读全帖
f**o
发帖数: 12685
16
来自主题: Military版 - 简单编程问题求教。
c#
using System.IO;
using System.IO.Ports;
using System.Threading;
namespace RS232RVR
{
public partial class Form1 : Form
{
private delegate void SetTextDeleg(string data);
public Form1()
{
InitializeComponent();
SettingRS232();
}
public void SettingRS232 ()
{
try
{
SerialPort mySerialPort = new SerialPort("COM6");
mySerialPort.BaudRate = 9600;
m... 阅读全帖
c*******a
发帖数: 1879
17
这种弱智儿童也能够进谷歌
看来谷歌是世风日下 将是i现在的IBM和诺基亚的后果
【 以下文字转载自 JobHunting 讨论区 】
发信人: zengqinghan (Zzz), 信区: JobHunting
标 题: Re: 面试结束,晒3个 Java面试题,请大家讨论。
发信站: BBS 未名空间站 (Sat Nov 18 15:24:05 2017, 美东)
第三提我也见到过,不过是C++的,其实很简单,就是a执行完解锁b,b执行完解锁c...
开始就a解锁就行了。Java的不懂。
参考代码:
#include
#include
#include
using namespace std;
class Solution
{
mutex mutexa, mutexb, mutexc;
int N = 0;
void threada()
{
for (int i = 0; i < N; ++i)
{
mutexa.lock();
... 阅读全帖
c*******a
发帖数: 1879
18
【 以下文字转载自 JobHunting 讨论区 】
发信人: justicezyx (just), 信区: JobHunting
标 题: Re: 大家看看一个自称狗家码农写的代码 (转载)
发信站: BBS 未名空间站 (Sun Nov 19 17:50:19 2017, 美东)
几个细节,跟google c++代码要求差别比较大。
这个代码我是看不懂在做什么。
#include
#include
#include
using namespace std;
// using declarations on namespace is banned in google c++ code
class Solution
{ // this is not the style used in google, if you write c++ in google, I
feel very difficult to believe that you can resist the muscle memory and
uses a differ... 阅读全帖
c****t
发帖数: 1262
19
来自主题: TheStrait版 - 我打算投奔民主自由了
我打算前往民主自由的台湾地区
每天在自由广场上为自由民主奋斗
void main(X)
{
if ( H(X)=="民主" )
call A(X)
else if ( H(X)=="自由")
call B(X)
else if ( (H(X)!="民主" && (H(X)!="自由")) then
call C(X)
}
char *H(char *X)
{ if (X==”共产“)
return ”民主“;
else if (X=="国民“)
return "自由”;
else if (X=="民进“)
return "fault";
}
void A(X)
{
call_yell(‘打到’,X,'党’);
}
void B(X)
{
call_yell(X,'党’,‘买台’);
}
void C(X)
{
call_yell('支持‘,X,'党’);
}

发帖数: 1
20
来自主题: Automobile版 - Genuine, OEM or Aftermarket Parts(ZT)
Genuine, OEM or Aftermarket Parts

When a part fails on your car and needs to be replaced, what type of
replacement part offers the best value and quality? Knowing the different
types of parts available, and when to select the proper type, can save you
hundreds if not thousands of dollars.
Car Manufacturers Don’t Manufacture Cars
Contrary to popular belief, the car you bought was not made entirely by the
car company you bought it from. It was essentially just put together by the
car company fro... 阅读全帖
c*****f
发帖数: 176
21
还是找不到如何 void shipping label a
In the Details column of the payment, click Details.
Go to the bottom of the Transaction Details page, and click Void Label.
Click Void Shipment.
根本没有 click void label选择
M*M
发帖数: 2573
22
Best Buy Rewards Zone Members - (6) Bottles of Wine and $40 in Rewards
Certificates - $41.95 Total
This partner deal from Best Buy's Reward Zone is back.
Long story short: You sign up to the wine club and place your first order
for $41.95 total. After a few weeks you are credited $40 in reward zone
dollars. So you're basically getting (6) bottles of wine for $2 and your
money back in Best Buy credit.
I did this last time this was offered and received my credits. You just have
to make sure you ca... 阅读全帖
p****t
发帖数: 5162
23
最近版上这么热闹,想就最近碰到的一件事情,求个建议。第一次遇到丢东西的事情没
有经验,处理得不好。说出来,大家给提提意见。
6月25号,ebay GC 100 九折,我买了两单,一单20张,第二单50张。7月4-5号的时候
寄到了。正好那个周末是长假,我们出去了。UPS就把两个包裹送到rental office签收
了。7月8号周一,我LP去取包裹,在office门外碰到一个朋友,说了两句。office的女
管理员看到了, 就把包裹送出来(这个有点异常,平时不这样的)。我LP拿到手上,
觉得不对,发现其中 一个信封的一边裂开了,一看只有二十张卡(packing slip是50
张)。然后马上打开另外一个信封,25张(packing slip是20张)。就跟女管理员说东
西丢了,管理员也说是不对,就帮助LP打了ebay的电话,也不确定是跟ebay CS还是
gift card mall汇报了一下。
接着,我LP打电话给我,我就去ebay开了case,把情况说明了,并建议卖家(
giftcardmall) 立即void掉所有的卡。一天后收到回复,只是简短的说“*** is
iworking... 阅读全帖
v*****t
发帖数: 127
24
来自主题: JobHunting版 - 讨论:关于一个design 问题
然后在贴一个decorator的做一个比较:
class Fur{
public:
virtual void describeself()=0;
};
class Table:public Fur{
public:
void describeself(){cout<<"I'm a table";}
};
class Material:public Fur{
public:
virtual void describeself()=0;
protected:
Fur *fur;
string material_name;
};
class Wood: public Material{
public:
Wood(Fur *f){fur = f; material_name = "wood";}
void describeself(){
fur->describeself();
cout<<" with "< }
};
class Steel: publi
K*****n
发帖数: 65
25
来自主题: JobHunting版 - 问个简单的GooG题目
For Problem 2, copied from Visual Studio
;memmove - Copy source buffer to destination buffer
;
;Purpose:
; memmove() copies a source memory buffer to a destination memory
buffer.
; This routine recognize overlapping buffers to avoid propogation.
; For cases where propogation is not a problem, memcpy() can be used.
;
; Algorithm:
;
; void * memmove(void * dst, void * src, size_t count)
; {
; void * ret = dst;
;
; if (dst <= src || dst >=
p**********s
发帖数: 115
26
来自主题: JobHunting版 - 回馈本版,贴ms onsite面经
我来解typedef那题:
#include
using namespace std;
void staticFun(int num) {
cout << "static function" << endl;
}
class foo{
public:
void classFun(int num) {
cout << "function in a class" << endl;
}
};
typedef void (*myStaticFun) (int);
typedef void (foo::*myClassFun) (int);
int main()
{
myStaticFun testStaticFun = staticFun;
testStaticFun(6);
foo foo1;
myClassFun testClassFun = &foo::classFun;
(foo1.*testClassFun)(6);
return 0;
}
r****o
发帖数: 1950
27
来自主题: JobHunting版 - 问一个精华区里的题目
刚才试了一下,两个const按存在不存在取4种组合,函数都能正确工作,除了一些
warning以外。
const char *function1(void)
{
const char *s = "12345";
return s;
}
const char *function2(void)
{
char *s = "12345";
return s;
}
char *function1(void)
{
const char *s = "12345";
return s;
}
char *function1(void)
{
char *s = "12345";
return s;
}
是不是这4个函数等价?
h****b
发帖数: 157
28
来自主题: JobHunting版 - exception specification 的问题
class Foo {
public:
void virtual abc() throw(int, double, long);
};
Referring to the sample code above, if a derived class, Bar, overrides
the method abc, which one of the following is an acceptable declaration?
void abc() throw(double, int, long);
void abc();
void abc() throw(string);
选哪个?谢了
c****s
发帖数: 241
29
来自主题: JobHunting版 - bloomberg相关的面试题
以前一直说要总结一下自己的面试题,总是忘了,现在补上。
面试我的是替bloomberg找人的consultant公司,这些题目主要是c,c++的知识题。下
面主要是我跟那个HR的聊天记录。有的答得不好,也可能有错误,仅供大家参考:
11:56 AM recruiter: // What happens?
int main()
{
return main();
}
11:58 AM // What prints?
void main()
{
static int var = 5;
printf("%d ",var--);
if(var) main();
}
11:59 AM // What prints?
void main()
{
char *p;
printf("%d %d ",sizeof(*p), sizeof(p));
}
12:00 PM // What prints?
#define a 10
void main()
{
#define a 50
printf("%d",a);
}
// What prints?
void main ()
{
x***y
发帖数: 633
30
来自主题: JobHunting版 - C++ Q36: derivation specification (B8_9)
This statement is wrong, 1. the derived class have all the members of the
base class even if the inheritence is private 2. in a class, the same
function can only appear once, and accesibility won't affect this. As A's :
void f(int) goes into B, you can not have another void f(int) in B.
You can verify yourself in the programme to see that in B void f(int) is
till virtual by creating a class C publicly inheriting from B, and use B* to
point to new C. You can see that void f(int) is virtual.
b********e
发帖数: 693
31
来自主题: JobHunting版 - C++ Q36: derivation specification (B8_9)
B is wrong, please see following test case
A *a = new B(). when call a->f(1), it print out B.
class C;
class A {
virtual void f(int) {cout << "A ";};
public:
friend class C;
};
struct B : public A {
public:
void f(long){};
void f(int){cout << "B f";};
};
class C{
public:
void test(){
A *a = new B();
a->f(1);
}
};
K******g
发帖数: 1870
32
来自主题: JobHunting版 - 请教一个C++的问题
class Person;
class Command
{
Person* object;
void (Person::*method)();
public:
Command( Person* obj = 0, void (Person::*meth)() = 0 )
{
object = obj;
method = meth;
}
void execute()
{
(object->*method)();
}
};
我有两个问题:
1)void (Person::*method)(); 是什么意思?
2) 如果class person里没有public member fuction,会怎么样呢?
w**********y
发帖数: 1691
33
来自主题: JobHunting版 - [C++问题]请教关于几种size of class
////////////////////////////
class A{ }; A a;
那么 sizeof(a) is 1;
class X{
void doNothing(){}
char a;
};
sizeof(x) 咋还是1,不是 1+1 =2 呢?
////////////////////////////
class Y{
virtual void doNothing(){}
};
sizeof(y) is 4..这个应该是指向virtual table的地址大小吧?
class Y{
virtual void doNothing(){}
virtual void doNothing2(){}
char a;
};
为啥是8不是4+1=5啊? 而且再添加一个member: char b..还是8..
如果换成char* b就成了12...
如果换成char a[2],结果是8
如果换成char a[10],结果是16
非常不理解..
c****o
发帖数: 41
34
来自主题: JobHunting版 - 出个题
My 2 cents. 多个reader可以同时读;但假如有多个reader读时来了一个writer,则之
后的
reader不能再读,直到writer结束。
读文件:
FileLock.Readlock();
//read ...
FileLock.ReadUnlock();
写文件:
FileLock.WriteLock();
//write ...
FileLock.WriteUnlock();
class FileLock 如下:
class FileLock
{
private:
Lock m_ReadCounterLock;
Lock m_FileLock;
Lock m_writerWaitLock;
int m_ReaderCount;
public:
void ReadLock()
{
m_ReadCounterLock.lock();

// 如果有writer等,block
m_writerWaitLock.lock();
m_writerWaitLock.un... 阅读全帖
N*D
发帖数: 3641
35
来自主题: JobHunting版 - 出个题
非常接近了,不过Readlock里面对于writerWaitLock用法有些问题。lock总是为了lock
些什么东西,那个地方啥也没干,就是check一下,假设check当时没有waiting write
,check一结束就来了一个write呢?两个人就一块儿读写文件了。

My 2 cents. 多个reader可以同时读;但假如有多个reader读时来了一个writer,则之
后的
reader不能再读,直到writer结束。
读文件:
FileLock.Readlock();
//read ...
FileLock.ReadUnlock();
写文件:
FileLock.WriteLock();
//write ...
FileLock.WriteUnlock();
class FileLock 如下:
class FileLock
{
private:
Lock m_ReadCounterLock;
Lock m_FileLock;
Lock m_writerWaitLock;
int m_ReaderCount;
public:
void R... 阅读全帖
r*******n
发帖数: 3020
36
来自主题: JobHunting版 - List Flattening from book
我的递归解法,大家看看对吗?
Node flat_list_head[N]; // N is the largest level.
void flattenList(Node *head, int level)
{
if (head->next){
insert_node_front_flat_list_head(head, level);

// Go next
head = head->next;
flattenList(head, level);
}

if (head->child){
insert_node_front_flat_list_head(head, level+1);
// Go child
head = head->child;
flattenList(head, level+1);
}
}
void ... 阅读全帖
k*******t
发帖数: 202
37
来自主题: JobHunting版 - 请教一段volatile和多线程的代码
class SyncBuf {
public:
void Thread1();
void Thread2();
private:
typedef vector BufT;
volatile BufT buffer_;
Mutex mtx_; // controls access to buffer_
};
Inside a thread function, you simply use a LockingPtr to get
controlled access to the buffer_ member variable:
void SyncBuf::Thread1() {
LockingPtr lpBuf(buffer_, mtx_);
BufT::iterator i = lpBuf->begin();
for (; i != lpBuf->end(); ++i) {
... use *i ...
}
}
The code is very easy to wr... 阅读全帖
g***s
发帖数: 3811
38
来自主题: JobHunting版 - 问道amazon的面试题
public class FindSegments {
public static void main(String args[]) throws InterruptedException {
// List segs = Arrays.asList(new Integer[]{2, 3, 5, 7,
8, 10});
List segs = Arrays.asList(new Integer[]
{2,2,4,6,7,7,8,8,8,9,9,10,11,12,13,14,15,16,16,17,18,20,21,22,23,24,25,2
6,28,
29,30,31,33,34,37,38,39,40,41,45,47,47,49,54,56});
long startTime = System.currentTimeMillis();
findSegs(segs);
System.out.printf("Total running time : %d ms",... 阅读全帖
g***s
发帖数: 3811
39
来自主题: JobHunting版 - 问道amazon的面试题
public class FindSegments {
public static void main(String args[]) throws InterruptedException {
// List segs = Arrays.asList(new Integer[]{2, 3, 5, 7,
8, 10});
List segs = Arrays.asList(new Integer[]
{2,2,4,6,7,7,8,8,8,9,9,10,11,12,13,14,15,16,16,17,18,20,21,22,23,24,25,2
6,28,
29,30,31,33,34,37,38,39,40,41,45,47,47,49,54,56});
long startTime = System.currentTimeMillis();
findSegs(segs);
System.out.printf("Total running time : %d ms",... 阅读全帖
f*****y
发帖数: 444
40
I am preparing algorithm, wrote the following algorithm. Share with you. Not
sure whether it is a duplicate post or not.
//---- Breadth Fast Search algorithm ---
// circular queue
int head = 0;
int tail = 0;
#define Q_EMPTY (head==tail)
#define Q_FULL ((tail-head+1)%SIZE==0)
#define SIZE 10
struct Node* q[SIZE];
void put(struct Node* x)
{
if (x==NULL) return;
if (Q_FULL) return; //queue full
q[tail] = x;
tail = (tail+1) % SIZE;
return;
}
struct Node* get()
{
if (Q_EMPT... 阅读全帖
g**********y
发帖数: 14569
41
先把字符排序,然后对每个不同的字符(个数是len)来递归。相当于把len个字符插进前面的生成串里,总共有C(n+len, len)种方式。
这是java code
好象这也是G喜欢问的一道题,面试时写出来还是很吃力的。程序不难,但是那些递归关系和边界条件想清楚,很费脑力。我觉得谁要不用debugger, 一遍把它写对,差不多就可以把G拿下了。
public class RepetitionPermute {
public void permute(String a) {
char[] c = a.toCharArray();
Arrays.sort(c);
permute("", c, 0);
}

private void permute(String s, char[] c, int begin) {
if (begin == c.length) {
System.out.println(s);
return;
}
... 阅读全帖
g**********y
发帖数: 14569
42
先把字符排序,然后对每个不同的字符(个数是len)来递归。相当于把len个字符插进前面的生成串里,总共有C(n+len, len)种方式。
这是java code
好象这也是G喜欢问的一道题,面试时写出来还是很吃力的。程序不难,但是那些递归关系和边界条件想清楚,很费脑力。我觉得谁要不用debugger, 一遍把它写对,差不多就可以把G拿下了。
public class RepetitionPermute {
public void permute(String a) {
char[] c = a.toCharArray();
Arrays.sort(c);
permute("", c, 0);
}

private void permute(String s, char[] c, int begin) {
if (begin == c.length) {
System.out.println(s);
return;
}
... 阅读全帖
s*****y
发帖数: 897
43
来自主题: JobHunting版 - 问两道google面试题
No.2 刚写了一个,似乎是对的,只是测试了2个例子:
{9, 4, 14, 2, 8, 10, 16, 1, 3, 7};
//in place swap without using addition variable
void swap(Tree *a, Tree *b)
{
a->element = a->element ^ b->element;
b->element = a->element ^ b->element;
a->element = a->element ^ b->element;
}
void BstToHeap1( Tree *root)
{
if (!root)
return;
BstToHeap1(root->left);
BstToHeap1(root->right);
if (root->right)
swap(root, root->right);
}
void Heapify(Tree *root)
{
if (!root) return;
... 阅读全帖
y***m
发帖数: 7027
44
来自主题: JobHunting版 - 有简洁树代码么
这个简单java的,有更简洁明了完善高效结构化的么,thx!
class BinaryTree {
private int data;
private int minNode = Integer.MAX_VALUE;
private BinaryTree leftpoiter;
private BinaryTree rightpoiter;
public BinaryTree() {
}
public BinaryTree(int data) {
this.data = data;
leftpoiter = null;
rightpoiter = null;
}
public void getMinNode(int key) {
getMinNode(this, key);
}
public void getMinNode(BinaryTree root, int key) {
if (root != null) {
i... 阅读全帖
g**********y
发帖数: 14569
45
来自主题: JobHunting版 - 求一道 面世题 的解答思路
我写的慢程序:用HashMap实现的Trie. 换成char[], 对于N=7, 速度快一些,但是对N<
7, 速度更慢。可能因为解跟Hash key的位置有关。
public class WordRectangle {
private final static String DIR = "src/test/resources";

private Trie m_trie;
private String[] m_words;
public static void main(String[] args) {
WordRectangle w = new WordRectangle();
long t0 = System.currentTimeMillis();
w.find(6);
long t1 = System.currentTimeMillis();
System.out.println("Time = " + (t... 阅读全帖
c******e
发帖数: 545
46
打个比方说,地址在0x400000的函数当初是用__stdcall修饰的(比如win32平台上的
WINAPI宏,API函数默认修饰),那么声明指针的时候也要用相同修饰:
void (__stdcall*func)(int,int) = (void (__stdcall*)(int,int))0x400000;
func(10,100);
如果用系统默认__cdecl的话
void (*func)(int,int) = (void (*)(int,int))0x400000;
func(10,100);
生成的调用代码就完全不正确,原因见原帖。这种问题平时是见不到的,因为compiler
会核对原型,而且link的时候因为name mangling也通不过,但是因为指针强制赋值,
所以跳过了所有的检查,需要自己确定才行。
f****4
发帖数: 1359
47
O(m+n), O(k), O(lg m + lg n)解法见
http://www.ihas1337code.com/2011/01/find-k-th-smallest-element-
O(lgk)的解法只是2分查找需要查找的范围,而不是0~m或者0~n
文字解释可以看我的回答
http://www.mitbbs.com/article_t1/JobHunting/31911109_0_1.html
#include
bool try_find_kth(int a[], int lowa, int higha, int b[], int lowb, int highb
, int k)
{
while(lowa<=higha)
{
int mid=lowa+(higha-lowa)/2;
if(a[mid]b[k-mid-2]))
{
printf("find %dth %d\n",k,a[mid]);
return true;
... 阅读全帖
i*******h
发帖数: 216
48
来自主题: JobHunting版 - job handler Qs
写个简单的意思意思:
class JobHandler
{
public:
JobHandler(ResourceManager*);
~JobHandler();
void Receive(Request&); //receive request and insert into queue, atomic
void Run();

private:
queue m_queue;
ResourceManager* m_ResMgr;
}
//Thread1 function
//This is the main loop of processing thread
void JobHandler::Run()
{
while(true)
{
lock_queue();
if(m_queue.empty())
{
//better to optimize here so that we don't frequently lo... 阅读全帖
c**********e
发帖数: 2007
49
来自主题: 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
50
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();
}
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)