A*******e 发帖数: 2419 | 1 这里说的很清楚。
http://jxq.me/2012/06/06/%E8%AF%91%E8%AF%A6%E8%A7%A3c%E5%8F%B3%
如果你实现了void foo(X&);,但是没有实现void foo(X&&);,那么和以前一样foo的参
数只能是左值。如果实现了void foo(X const &);,但是没有实现void foo(X&&);,仍
和以前一样,foo的参数既可以是左值也可以是右值。唯一能够区分左值和右值的办法
就是实现void foo(X&&);。最后,如果只实现了实现void foo(X&&);,但却没有实现
void foo(X&);和void foo(X const &);,那么foo的参数将只能是右值。
如果同时定义,是不是就有二义性了? |
|
a*****g 发帖数: 19398 | 2 /* http://www.ccodechamp.com/c-program-of-set-operations-in-maths/ */
#include
#include
#define MAX 30
void create(int set[]);
void print(int set[]);
void Union(int set1[],int set2[],int set3[]);
void intersection(int set1[],int set2[],int set4[]);
int member(int set[],int x);
int main()
{ int set1[MAX],set2[MAX],set3[MAX];
int x,op;
set1[0]=set2[0]=set3[0]=0;
printf("-------------------------------------------------------------\n");
printf("----------------made by C co... 阅读全帖 |
|
F*****e 发帖数: 331 | 3 问题是关于decorate pattern(装饰模式),就是给一个人一件一件穿衣服的程序,基
类是人,第一个子继承类是Finery(服饰),Finery下面有3个子继承类分别是:
Tshirt Sneaker Trousers。 程序中用到了装饰模式的思想,和一堆虚函数和覆写函数
,关于这些函数和覆写函数是怎么跑的我看不明白,贴上代码:请牛人观观!
class Program
{
static void Main(string[] args)
{
Person Alice = new Person("Alice");
Console.WriteLine("\n Alice's beautiful dress are:");
Sneakers sneaker = new Sneakers();
BigTrouser trouser = new BigTrouser();
TShirts tshirt = new TShirts()... 阅读全帖 |
|
g**********y 发帖数: 14569 | 4 原帖见:http://www.mitbbs.com/article_t/JobHunting/32015883.html
说实在,那个主函数是一个很糟糕的写法,没有错误,但是awfully done。
现代软件工程的核心就是:代码易读易改。要做到这点,你的写法就不能违反直觉。
sneaker.Decorate(Alice); <-------------------------
trouser.Decorate(sneaker); 就是这一段看不明白!!!!!
tshirt.Decorate(trouser); 虚函数+覆写函数 咋有递归的感觉
tshirt.Show(); <-------------------------
code里用了英文单词,但是表达的完全不是那个意思,那就是对后来维护的人一种折磨。
Decorator的核心就是一个operation, 经过一条链来处理。这条链是可以动态增加的。
从结果看,你希望达到的效果就是:
decoratedOb... 阅读全帖 |
|
S**I 发帖数: 15689 | 5 ☆─────────────────────────────────────☆
careerchange (Stupid) 于 (Sat Jun 4 20:25:58 2011, 美东) 提到:
void reversePrint(Node* head) {
if(head=NULL) {
cout << "Empty List. No node to print." << endl;
return;
}
if(head->next=NULL) {
cout << head->data << endl;
return;
} else {
Node* temp=head;
head=temp->next;
reversePrint(head);
cout << temp->data << endl;
return;
}
}
☆─────────────────────────────────────☆
speeddy (Wade) 于 (Sat Jun 4 20:30:33 ... 阅读全帖 |
|
j*******e 发帖数: 1058 | 6 我的解法,是一个普遍的k解法。在main里面把k改为2或者是面试官喜欢的k的值就ok。
希望大家指正。
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
// Start typing your Java solution below
// DO NO... 阅读全帖 |
|
j*******e 发帖数: 1058 | 7 我的解法,是一个普遍的k解法。在main里面把k改为2或者是面试官喜欢的k的值就ok。
希望大家指正。
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
// Start typing your Java solution below
// DO NO... 阅读全帖 |
|
r****i 发帖数: 528 | 8 Use cases:
No user request
User presses floor (Up/Down) buttons
User presses elevator buttons (emergency stop button, close/open button/
floor buttons)
User holds elevator door
Not working
Classes:
public enum ElevatorState
{
WAITING, RUNNING, INACTIVE
}
public enum Direction {UP, DOWN}
//to handle
class ElevatorGroup
{
private int id;
private List Elevators;
//to select an elevator in the list and add request to the elevator
request list
public Elevator pickUpElevator(Floo... 阅读全帖 |
|
d**********o 发帖数: 1321 | 9 最终版本的compiler测试结果
=================================================
Output of Building User Code
Explode the tar
c-.l
c-.y
scanType.h
makefile
symtab.h
symtab.cpp
emitCode.h
emitCode.cpp
20131214164956-huang-CS445-F13-A5.tar: POSIX tar archive (GNU)
Tests: directory
c-.l: lex description text
c-.y: lex description text
emitCode.cpp: ASCII C++ program text
emitCode.h: ... 阅读全帖 |
|
d**********o 发帖数: 1321 | 10 最终版本的compiler测试结果
=================================================
Output of Building User Code
Explode the tar
c-.l
c-.y
scanType.h
makefile
symtab.h
symtab.cpp
emitCode.h
emitCode.cpp
20131214164956-huang-CS445-F13-A5.tar: POSIX tar archive (GNU)
Tests: directory
c-.l: lex description text
c-.y: lex description text
emitCode.cpp: ASCII C++ program text
emitCode.h: ... 阅读全帖 |
|
c**t 发帖数: 2292 | 11 不是学计算机的试着写一个。请教授和其他学计算机指正。
Class cleanSickDogs{
DogSet ds = new DogSet();
public static void checkDogs(DogSet allDogs, int foundOtherSickDogAmt)
{
if (allDogs.getSickDogAmt()<1) throw new Exception("CDC Sucks!");
Dog myDog;
if(iAmNWWolf)
myDog = new Dog(true);
else
myDog = new Dog(false);
int dayCounter = 1;
while(true)
{
if(dayCounter>foundOtherSickD... 阅读全帖 |
|
o**2 发帖数: 168 | 12 Give each Predictor object its own thread, and apply producer/consumer
pattern twice: one is from user thread to predictor thread for dropping off
prediction input, and the other one is form predictor thread to user thread
for picking up computed prediction.
public class Predictor implements Runnable {
private String input, prediction;
private boolean stop;
public Predictor () {
new Thread (this).start ();
}
public synchronized boolean isIdle () {
return input... 阅读全帖 |
|
p*********s 发帖数: 1952 | 13 //刚学java, 下面代码出错,请问下错在哪儿里,怎么改?
//RefDemo04.java:65: error: cannot find symbol
// System.out.println(bk.getPerson().getName());
^
// symbol: method getName()
// location: class Person
// 1 error
class Person {
private String name;
private int age;
private Book book;
public Person(String n, int a) {
this.setName(n);
this.setAge(a);
}
public void setBook(Book b) {
book = b;
}
public vo... 阅读全帖 |
|
i**p 发帖数: 902 | 14 最近编译运行了《Essential Linux Device Drivers》第5章的例子,
Driver for the Parallel LED Board (led.c)。程序不复杂。其中有一个函数led_
attach(struct parport *port) 是由parport_register_driver(&led_driver)注册的。
请问,led_attach()是何时被调用的?或者说怎么引起系统调用这个函数?
----------------------------------------
I double checked my code again. Though I use the same name as the link http://www.spinics.net/lists/newbies/msg38087.html suggested, the
led_attach() is not called.
code is here.
#include
#include
#include 阅读全帖 |
|
d****p 发帖数: 685 | 15 I doubt the memcpy implementation is wrong.
Dereferencing a void*?
I think the following is an OK implementation:
void* memcpy(void* dest, const void* src, size_t size)
{
assert(dest != 0 && src != 0);
char* dest2 = (char*)dest;
char* src2 = (char*)src;
while (size-- > 0)
*dest2++ = *src2++;
return dest;
}
Actually the following is also OK:
void* memcpy(void* dest, const void* src, size_t size)
{
assert(dest != 0 && src != 0);
|
|
g***i 发帖数: 4272 | 16 #include
#include
typedef void (*callback_fn_t)(void*);
typedef struct cb_st{
void *user_data;
callback_fn_t user_fn;
}callback_t;
void do_callback(const callback_t *cb){
((callback_fn_t) cb)(cb->user_data);//这里会出错
}
void my_cb(void *data){
printf("Hello, %s!\n", (char*)data);
}
int main(int argc, const char * argv[])
{
callback_t cb;
cb.user_data = (void *)"Stephen";
cb.user_fn = &my_cb;
do_callback(&cb);
return 0;
}
请问出错的原因是什么?
是不是不能把cb ... 阅读全帖 |
|
k*******3 发帖数: 1909 | 17 下面程序copy来自C++ essential 4.7节,
Triangular_iterator begin() const
{ return Triangular_iterator( _beg_pos ); }
请问
return Triangular_iterator( _beg_pos ); 这句话的语法是怎么样的?
Triangular_iterator是自定义的一个class吧,class后面直接加(_beg_pos)什么意思
呢?
如果是返回一个Triangular_iterator的object, 不需要定义成
Triangular_iterator obj(_beg_pos);
return obj;吗?
谢谢!
================================Full Class definition==================
class Triangular_iterator
{
public:
Triangular_iterator( int index ) : _index( index-1 ){} //*... 阅读全帖 |
|
h*******3 发帖数: 3775 | 18 有一组数据,其中的fied有surname, department,payRate,等等
要求是按照surname 从A 到Z 排列这组数据。排列的方法是straight insertion
我现在的方法已经可以sort surname, department,但是如何能把他改成templates呢?
这样就能用来sort 任何数据。
我的目前sort的方法是这样的:
struct emp
{
char surname[15];
char given[15];
char depart[20];
double payRate;
char eyeColor[10];
};
void sort(struct emp person[], int nums, compare cmp, copy cpy)
{
void *key;
int j, i, flag;
struct emp temp;
for (j = 1; j < nums; j ++)
{
i = j - 1;
(*cpy)(key, (person + j));
te... 阅读全帖 |
|
o**2 发帖数: 168 | 19 上周 cplus2009 同学有个问题(http://www.mitbbs.com/article_t/Programming/31253813.html),涉及到GUI程序中线程之间转换的问题。就是controls都要在UIthread(Swing或SWT)里操作,而后台的logic要在非UIthread里操作。
我当时说了FMP可以轻易淘汰掉这种thread-base的编程技术,于是趁这个长周末,把对
UIthread的支持做进了FMP。想要试用的,可以在此download 2.0 jar:https://
github.com/fastmessenger/RI-in-Java/blob/master/fmp-2.0-bin.jar?raw=true
这里我借用 cplus2009 同学的问题作为范例:他的问题是有一个界面,上面有一个
BROWSE按钮,按了后弹出一个文件选择对话框。用户选好文件后,将文件名显示在
BROWSE按钮前面的text里。这里文件选择用来代表后台的耗时操作。
用FMP的,可以这样设计:一个Frontend class用来access UI controls... 阅读全帖 |
|
i**p 发帖数: 902 | 20 I double checked my code again. Though I use the same name as the link http://www.spinics.net/lists/newbies/msg38087.html suggested, the
led_attach() is not called.
code is here.
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "led"
static dev_t dev_number; /* Allotted device number */
static struct class *led_class; /* Class to which ... 阅读全帖 |
|
h****i 发帖数: 254 | 21 不是你编个有效率名词就可以拿来吹牛误导人的。
美国临床实验的论文发表的是说78%建立起人工神经反射成功,但是结论“The impact
of this surgery on improvement in quality of life still needs to be
determined”
他的实验数据是,看最后三列
有反射 voiding(手术前) voiding(手术12月后)
1 No No Yes
2 Yes No Yes
3Yes No No
4 Yes No Yes
5 Yes No Yes
6 No No Yes
7 Yes No No
8 Yes Yes Yes
9 Yes Yes Yes
三个病人有反射,voiding从no转为yes
两个病人无反射,voiding也从no转为yes
一个病人有反射,voiding保持在no不变
两个病人有反射,voiding保持在yes不变 |
|
e********a 发帖数: 262 | 22 The Introduction of Shear Modification to Gurson Model
Sun, 2012-09-23 23:41 - Liang Xue
research
The Introduction of Shear Modification to Gurson Model
I have been asked questions about how the shear modification was introduced
to the Gurson model. I was also asked to compare my modification (see Xue L.
, Constitutive modeling of void shearing effect in ductile fracture of
porous materials, Engineering Fracture Mechanics, 2008, 75(11):3343-3366) to
John Hutchinson's modification (Nahshon, K., H... 阅读全帖 |
|
o**s 发帖数: 65 | 23 the testcase is correct, b is the right answer.
But for this one: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.
I have different opinion.
class A {
void f(int){
cout << "A::f()" << endl;
}
public:
void gg(){
f(10);
}
};
class B : public A {
public:
void f(long);
void f(int){
cout << "B::f() |
|
t*****j 发帖数: 1105 | 24 [9.4] 内联函数如何在安全和速度上取得折衷?
在 C 中,你可以通过在结构中设置一个 void* 来得到“封装的结构”,在这种情况下
,指向实际数据的 void* 指针对于结构的用户来说是未知的。因此结构的用户不知
道如何解释void*指针所指内容,但是存取函数可以将 void* 转换成适当的隐含类
型。这样给出了封装的一种形式。
不幸的是这样做丧失了类型安全,并且即使仅仅是访问结构体中的一个很不重要的字段
也必须进行函数调用。(如果你允许直接存取结构的域,那么任何人都能直接存取该结
构体了,因为他们必须了解如何解释 void* 指针所指内容;这样将使改变底层数据结
构变的困难)。
~~~~~~~~~~~~~~~~~~~~这上面讲的我似懂非懂....哪位能帮我讲
讲清楚些。c里为啥要用void*啊?丧失类型安全这个我是明白的。 |
|
h*****g 发帖数: 944 | 25 Q1)
what are the variable types in the statement below?
int * x, y;
a) both x and y are of type int *
b) x is of type int* and y is of type int
c) x is of type int and y is of type int*
d) both x and y are of type int
我选的是b
Q2)
which of the following is a correct declaration for a pointer to a function
that returns a double and takes no arguments?
a) double (*ptr_function)(void);
b) double (ptr_function)(void);
c) double ptr_function(void);
d) double *ptr_function(void);
Q3)
Which of the followi... 阅读全帖 |
|
c*****e 发帖数: 74 | 26 题目:一个board上,每个cell是一个字母,从一个cell出发,可以到它相邻的8个cell
。这样可以在board上walk。但是一个walk上一个cell不能出现2次。
要求:找出所有的可以由walk得到到的不同的单词。编程中可以自己假定一些已经实现
的方法。
以前面试被问到过,时间浪费在一些细节(怎么解决8个相邻cell的问题,怎么检查是
否一个cell重复出现)上了,当时也没有充分利用try,没答好,主要还是对递归、Try
的接口不熟。今天好好想了想该怎么写,下面是我的代码,花了不少时间,觉得收获不
少。好好写写这道题应该对面试很有帮助的。
有问题帮忙指出一下。
---------------------------------
class CTryNode;
class CTry
{
public:
CTryNode* Advance(CTryNode* pNode, char ch);
CTryNode* Back(CTryNode* pNode);
string GetValue(pTryNode* pNode);
bool IsCurr... 阅读全帖 |
|
f*******n 发帖数: 8 | 27 来自主题: JobHunting版 - 问一算法题 我贴一下O(n)算法的实现吧,大家自己琢磨
#include "stdio.h"
//轮换
void Cycle(int Data[],int Lenth,int Start)
{
int Cur_index,Temp1,Temp2;
Cur_index=(Start*2)%(Lenth+1);
Temp1=Data[Cur_index-1];
Data[Cur_index-1]=Data[Start-1];
while(Cur_index!=Start)
{
&... 阅读全帖 |
|
v***a 发帖数: 365 | 28 来自主题: JobHunting版 - 问个算法题 第一次写 c 程序,不保证正确,请自己 debug
程序假设单词是 a to z 组成
用的 bst counting, 然后 导出来 qsort
#include
#include
struct _node;
typedef struct _node {
int cc;
char c;
struct _node * n[26];
struct _node * fa;
} node;
void addToTree(node * root, node * r, const char * p1, const char * p2) {
int t;
int i;
if (p1 == p2) {
if (r->cc == 0) root->cc++;
r->cc++;
return;
}
t = (*p1) - (int)'a';
if (r->n[t] == NULL) {
r->n[t] = (node *... 阅读全帖 |
|
S**I 发帖数: 15689 | 29 ☆─────────────────────────────────────☆
recursive (递归) 于 (Mon Apr 11 10:56:49 2011, 美东) 提到:
大半夜收到HR的thank you note。不用管什么NDA了
本人ECE fresh PhD,背景是电路/EDA,跟G业务基本没什么关系
同学内部推荐的,很简单的一次电面就给了onsite
题都不难,但是自己没把握好机会,出了一些小bug。
总的感觉,出错就是硬伤,宁可从最简单的算法写起,也不能出错。
电面:
1,Skip list, http://en.wikipedia.org/wiki/Skip_list
写code实现struct skip_list * find(struct skip_list *head, int value)
2,sorted array with repeated elements
for given element, find out its range.
e.g. A A B B B B B C C D D E F G, given B, the out... 阅读全帖 |
|
S**I 发帖数: 15689 | 30 ☆─────────────────────────────────────☆
libei (Bei) 于 (Wed Jan 11 15:43:39 2012, 美东) 提到:
面试官是Google+组的,
一上来她说看到我简历上的一篇测试自动化的文章,读了一遍,感觉"very
informative",让后让我介绍一下相关经验。让我小高兴了一下。
第一题是coding,做的还算顺利,后来她评价说所有的cases都覆盖到了。可能算是过
关吧。
第二题我想复杂了,然后在她提示下才解决。自我感觉很不好。其实sort一下就差不多
了,不过我往复杂的树结构想去了。虽然树结构确实能解决这个问题,不过当时我解释
得很不清楚。反正很不爽。
最后瞎聊时间,她说我提到的测试自动化实践和Google内部的基本完全一样blahblah。
。。,不过我觉得这点也算不上加分吧,是个人进google一段时间后都能学会。就怕她
觉得我想问题太复杂,直接negative。
大家有啥建议想法??
☆─────────────────────────────────────☆
peking2 (myfac... 阅读全帖 |
|
e********r 发帖数: 2352 | 31 在本地的考试中心预约的上机考试。考试有三个部分,1.数学题,2.给出一种新的编程
语言的语法,回答相应问题,3. Programming Test 要求速度和准确性both important
Programming test 答得不好,肯定不行了,把面试题发上来让大家做做。做了3个半小
时,快累死了。
数学题基本上就是脑筋急转弯,举几个例子
1. You have three kinds of magazines, all but two are Times, all but two are
Science, all but two are Nature. How many magazines in total do you have?
3 books
2. Only one of the answers is true
A. All of the below are true
B. All answers are true
C. One of the above is true
D. All of the above are true
E. None of the above ar... 阅读全帖 |
|
d********w 发帖数: 363 | 32 Given a binary tree made up out of the following structures:
typedef struct _tnode {
int key;
struct _tnode *left;
struct _tnode *right;
void *datum;
} TreeNode;
Write a C routine with the following prototype that returns all TreeNodes of
a tree to free
store:
void TreeFree(TreeNode *root,void (*datafreer)(void *));
where root is the root node of a tree, and datafreer is a pointer to a
function returning
void and taking a void * that will return the object pointed to by a
TreeNode.datum
pointer ... 阅读全帖 |
|
C***y 发帖数: 2546 | 33 假设你有个rw lock class
class rwlock{
pubic:
void getReadLock();
void releaseReadLock();
void getWriteLock();
void releaseWriteLock();
};
然后你可以在你的hash table的class里面加个 rwlock的data member
class htable{
private:
rwlock m_lock;
};
在find方法里可以用r lock,insert 方法里面用w lock
void htable::find(...)
{
m_lock.getReadLock();
.
.
.
m_lock.releaseReadLock();
}
void htable::insert(...)
{
m_lock.getWriteLock();
.
.
.
m_lock.releaseWriteLock();
}
get 和release可以用一个auto class包装起来,这样你就不用手动调用release了 |
|
h****n 发帖数: 1093 | 34 写了一个,不过废了挺长时间,不知道要写详细到什么程度
enum STATE{MAINTENANCE,STAND,UP,DOWN};
public class Elevator
{
private:
STATE state;
int currentFloor;
priority_queue, greater> UpFloorReqList;
priority_queue DownFloorReqList;
public:
Elevator(STATE st, int floor)
{
state = st;
floor = currentFloor;
}
STATE GetState()
{
return state;
}
void SetState(STATE newst)
{
state = newst;
}
int GetCurrentFloor()
{
return currentFloor;
}
void SetCurrentFloor(int floor)
{
currentFloor = floor;
}
void Ad... 阅读全帖 |
|
h***i 发帖数: 1970 | 35 class RWMutex {
public:
RWMutex();
~RWMutex();
void read_lock();
void write_lock();
void unlock();
private:
int count_; // >0 multiple reads, <0 writer, = 0 free
int waiting_writers_;
pthread_mutex_t mutex_;
pthread_cond_t readers_;
pthread_cond_t writers_;
};
RWMutex::RWMutex() {
count_ = 0;
waiting_writers_ = 0;
pthread_mutex_init(&mutex_, NULL);
pthread_cond_init(&readers_, NULL);
pthread_cond_init(&writers_, NULL);
}
RWMutex::~RWMutex() {
pthre... 阅读全帖 |
|
d**********x 发帖数: 4083 | 36 H2O那个我写了个C的。可以改吧改吧加上对各种原子的计数。
编译的时候别忘了 -lpthread !
#include
#include
#include
#include
#include
#include
sem_t availH;
sem_t availO;
sem_t usedH;
sem_t usedO;
void* H(void*) {
sem_post(&availH);
sem_wait(&usedH);
fprintf(stderr, "H consumed.\n");
pthread_detach(pthread_self());
return NULL;
}
void* O(void*) {
sem_post(&availO);
sem_wait(&usedO);
fprintf(stderr, "O consumed.\n");
pthread_detach(... 阅读全帖 |
|
l**h 发帖数: 893 | 37 实现两个函数: H() and O(), 这两个函数会被多线程调用。当一个线程调用H或O时
,如果当前已经有至少两个线程call H和一个线程call O。那么让两个call H和一个
call O的线程返回(产生一个水分子),其他的都block。
写了个Java版本的,大家指教:
import java.util.*;
import java.util.concurrent.*;
public class ThreadH2OSema {
private final Semaphore hCalled = new Semaphore(0);
private final Semaphore hUsed = new Semaphore(0);
private final Semaphore oCalled = new Semaphore(0);
private final Semaphore oUsed = new Semaphore(0);
public void H() {
try {
h... 阅读全帖 |
|
d**********x 发帖数: 4083 | 38 H2O那个我写了个C的。可以改吧改吧加上对各种原子的计数。
编译的时候别忘了 -lpthread !
#include
#include
#include
#include
#include
#include
sem_t availH;
sem_t availO;
sem_t usedH;
sem_t usedO;
void* H(void*) {
sem_post(&availH);
sem_wait(&usedH);
fprintf(stderr, "H consumed.\n");
pthread_detach(pthread_self());
return NULL;
}
void* O(void*) {
sem_post(&availO);
sem_wait(&usedO);
fprintf(stderr, "O consumed.\n");
pthread_detach(... 阅读全帖 |
|
l**h 发帖数: 893 | 39 实现两个函数: H() and O(), 这两个函数会被多线程调用。当一个线程调用H或O时
,如果当前已经有至少两个线程call H和一个线程call O。那么让两个call H和一个
call O的线程返回(产生一个水分子),其他的都block。
写了个Java版本的,大家指教:
import java.util.*;
import java.util.concurrent.*;
public class ThreadH2OSema {
private final Semaphore hCalled = new Semaphore(0);
private final Semaphore hUsed = new Semaphore(0);
private final Semaphore oCalled = new Semaphore(0);
private final Semaphore oUsed = new Semaphore(0);
public void H() {
try {
h... 阅读全帖 |
|
h**u 发帖数: 144 | 40 用了两个blocking queue, 还请大侠指教!
import java.util.concurrent.LinkedBlockingQueue;
public class H2O {
LinkedBlockingQueue hQueue = new LinkedBlockingQueue();
LinkedBlockingQueue oQueue = new LinkedBlockingQueue();
Object o = new Object();
public void h() throws InterruptedException {
hQueue.put(Thread.currentThread());
synchronized (o){
System.out.println(Thread.currentThread().getName() + ".h,
notify");
o.notify()... 阅读全帖 |
|
h**u 发帖数: 144 | 41 用了两个blocking queue, 还请大侠指教!
import java.util.concurrent.LinkedBlockingQueue;
public class H2O {
LinkedBlockingQueue hQueue = new LinkedBlockingQueue();
LinkedBlockingQueue oQueue = new LinkedBlockingQueue();
Object o = new Object();
public void h() throws InterruptedException {
hQueue.put(Thread.currentThread());
synchronized (o){
System.out.println(Thread.currentThread().getName() + ".h,
notify");
o.notify()... 阅读全帖 |
|
d**********x 发帖数: 4083 | 42 那题目貌似就没卡复杂度。。。枉我昨天下班后还捏着鼻子写了个treap。。。结果证
明是输出格式错了,只过了两三个case的可以考虑检查一下输出格式
思路大致就是用order statistics tree。每次查询都是O(logn)。手写最大问题在于没
法定制容器的行为。。。
好吧,然后想了一下,如果用两个heap加一个map的话也是可以的,因为只是查询
median,没有要求随机查询kth元素,所以还是写复杂了
附上代码,treap是个随机平衡树,可以拿去用。
#include
#include
#include
#include
using namespace std;
template
class Treap {
public:
class Node {
public:
Node(const T& value)
: value_(value),
left_(NULL),
right_(NULL),... 阅读全帖 |
|
p*****p 发帖数: 379 | 43 来自主题: JobHunting版 - 电梯设计题 贴个我以前写的,仅供参考,肯定不是标准解
包括控制系统和电梯厢两个类
class ElevatorSystem
{
public:
struct SystemSetup{
int totalFloors;
int totalElevators;
};
ElevatorSystem(SystemSetup &);
~ElevatorSystem(void);
void requestUp(int floor);
void requestDown(int floor);
private:
int totalFloors;
int totalElevators;
Elevator **elevators;
enum RequestType {NONE, UP, DOWN};
RequestType *requests;
void resetRequest(int floor);
};
class Elevator
{
public:
Elevator(int... 阅读全帖 |
|
q******0 发帖数: 15 | 44 Will this resolve your problem? A_ex() is the your new interface provided to
library user.
class Base
{
virtual void A(); //virtual
void A_ex(); // non-virtual
}
void Base::A()
{
//do nothing
}
void Base::A_ex()
{
A();
}
class Derived
{
virtual void A();
}
void Derived::A()
{
//do something specific to Derived
} |
|
n*****g 发帖数: 178 | 45 题目是CC150第五版16.5题,题目是这样的:
Suppose we have the following code:
public class Foo {
public Foo() { . . . }
public void first() { ... }
public void second() { ... }
public void thirdQ { ... }
The same instance of Foo will be passed to three different threads. ThreadA
will call first, threads will call second, and threadC will call third.
Design a mechanism to ensure that first is called before second and second
is called before third.
这要换成C语言来问的话,是不是等同于这样问:一个foo函数,确保foo先被first调用
,然后再被second和third调用(f... 阅读全帖 |
|
b******7 发帖数: 92 | 46 #define BLOCK_SIZE 1024
#define ALIGN(size,unit) (((size)+(unit-1))/(unit)*(unit))
struct ChunkHead{
ChunkHead * next;
};
#define CHUNK_SIZE ALIGN(sizeof(ChunkHead) + BLOCK_SIZE, sizeof(int))
ChunkHead * firstChunk;
void initChunk(void * addr, size_t size)
{
firstChunk = NULL;
Chunk * pre = NULL;
for(int i = 0; i < size/CHUNK_SIZE; i++)
{
ChunkHead * cur = (ChunkHead *)(addr + i* CHUNK_SIZE);
if( pre != NULL)
... 阅读全帖 |
|
s*********s 发帖数: 318 | 47 来自主题: JobHunting版 - 问个面试题 http://tiny/1fpew40ab/stacques5347howt
好像也没有明确答案。
我save了一份答案,号称O(N),不知道对不对?
#include "stdio.h"
//轮换
void Cycle(int Data[], int Length, int Start) {
int Cur_index, Temp1, Temp2;
Cur_index = (Start * 2) % (Length + 1);
Temp1 = Data[Cur_index - 1];
Data[Cur_index - 1] = Data[Start - 1];
while (Cur_index != Start) {
Temp2 = Data[(Cur_index * 2) % (Length + 1) - 1];
Data[(Cur_index * 2) % (Length + 1) - 1] = Temp1;
Temp1 = Temp2;
Cur_index = (Cur_in... 阅读全帖 |
|
h**o 发帖数: 548 | 48 int test_parking_lots(){
ParkingLots p(3,40,50);//floor, row, column
Car car1 = Car();
Motor motor1 = Motor();
Bus bus1 = Bus();
int car1_id, motor1_id, bus1_id;
bool res;
res = p.park(car1, car1_id);
res = p.park(motor1, motor1_id);
res = p.park(bus1, bus1_id);
res = p.unpark(car1, car1_id);
return 0;
}
typedef enum VehicleType_e{
MOTOR_VEHICLE_TYPE = 0,
CAR_VEHICLE_TYPE,
BUS_VEHICLE_TYPE,
MAX_VEHICLE_TYPE_SIZE
} VehicleType;
typedef e... 阅读全帖 |
|
Y********f 发帖数: 410 | 49 贴个我写的吧,C++,有点繁琐,write优先。
class rwlock
{
public:
rwlock();
void rdlock();
void wrlock();
void unlock();
private:
pthread_mutex_t mutex;
pthread_cond_t condRead;
pthread_cond_t condWrite;
int readerCount;
int writeWaitCount;
bool writeLock;
};
rwlock::rwlock() : readerCount(0), writeLock(false), writeWaitCount(0)
{
pthread_mutex_init(&mutex, NULL); //forget NULL
pthread_cond_init(&condRead, NULL);
... 阅读全帖 |
|
Q*****a 发帖数: 33 | 50 #include "stdafx.h"
#include
#include
#include
namespace {
class IStopWatch {
public:
virtual void Start() = 0;
virtual double SecondsFromStart() = 0;
};
class StopWatch: public IStopWatch {
private:
time_t start_;
public:
StopWatch(bool start) {
if (start) {
Start();
}
}
virtual void Start() {
time(&start_);
}
virtual double... 阅读全帖 |
|