由买买提看人间百态

topics

全部话题 - 话题: printf
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
l***h
发帖数: 392
1
来自主题: JobHunting版 - 昨天的F家店面
这个题目主要是靠第二点和第三点。
#include
#include
#define BLOCK 40
size_t readblock(FILE *fp, char* buffer){
return fread(buffer,1,BLOCK,fp);
};
size_t readline(FILE *fp, char *buf){
long pos = ftell(fp); // record the currently position
int offset = 0;
int num;
while( num = readblock(fp, buf+offset)){
int i;
for( i=0; i char c = buf[offset];
if(... 阅读全帖
j********e
发帖数: 1192
2
来自主题: JobHunting版 - google scramble string O(n) 解法
我没有说字符集相同就能scramble,字符集相同是必要条件,而不充分。
我的做法是,先找到一个位置i在s1,j在s2使得分割后的4个字符串两两
有相同的字符集(i=j 或者i=N-j)。然后接着对这2对字符串继续做同样
的事情连寻找分割位置。这样递归下去,如果有某对字符串无法找到分割
位置,则表示失败。否则,最后分隔最小的字符串只有一个字符。就可以
判断scramble成功。
问题是,如果有多个分割位置,是否任选一个都可以?如果必须测试每个可能的分割位置,那复杂度就不好说了。
下面是我的简单实现代码,通过了leetcode的online judge (包括judge large)。这段代码中会处理所有可能的分割位置。如果只选第一个可能的分割位置,有一个测试失败了。
#include
#include
#include
#include
using namespace std;
#define BITMAP_LEN 256
bool compare_char_set(const char *s1, con... 阅读全帖
d**e
发帖数: 6098
3
来自主题: JobHunting版 - [合集] 放弃A,G...从头再来
☆─────────────────────────────────────☆
printf (helloworld) 于 (Tue May 8 22:20:25 2012, 美东) 提到:
下午接到G家猎头的电话,给offer了。听着猎头的祝贺,心里酸酸的。
自己骑驴找马,投A,G完全为了配合LD。可是LD前两天刚刚接到拒信,他在CA的
jobhunting已经全军覆没了。加上早就把他拒掉的M,我们在西雅图和硅谷团聚的美好
计划彻底泡汤。LD手头比较接近offer的职位都在中部,而我人在纽约。不管接受A,G
任何一个offer,或者继续现在的工作,我们之间的距离只能更遥远...
四年了,不想再两地了...如果一定有人要牺牲一下的话,那还是我来吧
回到家,打开邮箱,A,G家的offer静静地躺在那里。她们和网上传说的一样华丽。点
开,一页页的看过去,给自己几分钟的时间,傻乎乎地陶醉在那些数字里。然后呢,还
是删掉吧...祈祷几个月以后,这个幸运邮箱会把我带到LD那里
周一就要给A,G打电话了,还没想好说什么。陪LD一起找工作的这段时间里,一直看版
上的面经,受益匪浅。现在的... 阅读全帖
J**9
发帖数: 835
4
来自主题: JobHunting版 - An interview question
int hkNumRangeRandMy(int l, int u)
{
return l + rand() % (u-l+1);
}
void hkNumRandom2Out(int a[], int size)
{
if (!a || size<=1)
return;
while(size>1)
{
int idx1 = hkNumRangeRandMy(0, size-1);
int idx2 = idx1;
do {
idx2 = hkNumRangeRandMy(0, size-1);
}
while (idx2 == idx1);
//printf("idx1=%d idx2=%d\n", idx1, idx2);
int diff = 0;
int i;
diff = abs(a[idx1]-a[idx2]);
if(idx1>idx2)
... 阅读全帖
l*********o
发帖数: 3091
5
来自主题: JobHunting版 - 问下LeetCode上的题目:count and say
#include
#include
int convert(char* src, char* dst)
{
*dst = 0;
int pos;
char str[64];
while(*src)
{
if(*src < '0' || *src > '9') return 1;
pos = 1;
while(*src==*(src+pos))pos++;
sprintf(str, "%d", pos);
strcat(dst, str);
dst += strlen(str);
sprintf(str, "%c", *src);
strcat(dst, str);
dst += strlen(str);
src += pos;
}
return 0;
}
int main()
{
int n = 29;
int... 阅读全帖
m**n
发帖数: 384
6
下面代码,如果把所有*去掉,结果是对的,
但我需要用指针传递数据,所以都用了指针,编译后,
结果就是Segmentation fault
帮看看为啥啊
#include
#define MAX 10
main()
{
int *row;
int *column;
int *total;
*row=1;
*column=1;
*total=0;
while(*total {
if(*column==*row)
{ printf("%4d",((*column)*(*row)));
*column=1;
(*row)++;
(*total)++;
if(*total }
else
{printf("%4d",((*column)*(*row)));(*column)++;(*total)++;}
}
printf("F\n");
}
x*******9
发帖数: 138
7
来自主题: JobHunting版 - 问一道G家热题
做了一下
>> 给定一个数字数组 ,其中每个元素是从末端数小于原数组中该元素的个数。求原数
组。
Time complexity: O(n * logn) // quick-sort + traverse
Memo complexity: O(n)
>> 令s[i]为a[i+1..n-1]中比a[i]大的数的数量。求最大的s[i]。要求O(nlogn)
Time complexity: O(n * logn) // Manipulate the Binary Indexed Tree
Memo compleixty: O(n)
#include
#include
#include
#include
#include
#include
using namespace std;
#define print(x) cout << x << endl
#define input(x) cin >> x
const int SIZE = 1024;
// @brief Bin... 阅读全帖
t********r
发帖数: 157
8
来自主题: Working版 - EB1 or EB2?
This is wrong:
try (eb1)
{
sleep(99999999);
} catch (exception e)
{
eb2;
printf(“妈的,晚了”);
}
Solution:
try (eb1 && (eb2 + NIW))
{
sleep(1111111);
} catch (exception e)
{
if (eb1.success() && eb2.fail())
{
printf(“绿了”);
} else if (eb1.fail() && eb2.success())
{
printf(“绿了”);
} else
{
printf(“妈的,踩在屎上了”);
goto home;
}
}
l*****8
发帖数: 16949
9
下面这个hello world的20天能学会吗?
[
uuid(2573F8F4-CFEE-101A-9A9F-00AA00342820)
]
library LHello
{
// bring in the master library
importlib("actimp.tlb");
importlib("actexp.tlb");
// bring in my interfaces
#include "pshlo.idl"
[
uuid(2573F8F5-CFEE-101A-9A9F-00AA00342820)
]
cotype THello
{
interface IHello;
interface IPersistFile;
};
};
[
exe,
uuid(2573F890-CFEE-101A-9A9F-00AA00342820)
]
module CHelloLib
{
// some code related header files
importheader();
importheader();
importheader(阅读全帖
f******6
发帖数: 67
10
来自主题: WaterWorld版 - 借人气问个c语言的问题,求救
#include
#include
int main(int argc, char *argv[])
{
int value, base;
int remainder, place=79;
char buffer[80];
line_1 //buffer[79]=(char)(0);//不加这一行就有乱码
char *table ="0123456789ABCDEF";
value = atoi(argv[1]);
base = atoi(argv[2]);
printf("%d %dn", value, base);
do {
remainder = value%base;
printf("%cn", table[remainder]);
buffer[place] = (char)(table[remainder]);
printf("%cn", buffer[place]);... 阅读全帖
w**********l
发帖数: 8501
11
来自主题: CS版 - 问一个很简单的编程问题
用C语言。
调用几个void function. 其中一两个void functions里面如果加个printf语句,就能
run下去直到结束;如果删掉其中一个printf,就出现segmentation fault.
printf里面什么也没有,就是个test, printf("test\n");
到底什么原因?怎么解决?
谢谢
l******n
发帖数: 1683
12
来自主题: Linux版 - ./test input and ./test < input
可以这样,
//test
cat $1 > tmp
cat tmp | awk '{if (NR==1) {printf "%s",$2} else {printf ", %s",$2}}END{pri
ntf "\n"}'
cat tmp | awk '{if (NR==1) {printf "%s",$3} else {printf ", %s",$3}}END{pri
ntf "\n"}'
rm -f tmp
r*******y
发帖数: 1081
13
来自主题: Linux版 - ./test input and ./test < input
I tried this:
//input
a A 10
b B 20
//test
cat $1 > tmp
cat tmp | awk '{if (NR==1) {printf "%s",$2} else {printf ", %s",$2}}END{pri
ntf "\n"}'
cat tmp | awk '{if (NR==1) {printf "%s",$3} else {printf ", %s",$3}}END{pri
ntf "\n"}'
rm -f tmp
then ./test < input just give
A, B10, 20
not
A, B
10, 20
r*******y
发帖数: 1081
14
来自主题: Linux版 - ./test input and ./test < input
I find a way from your code:
cat $1 >tmp
cat tmp | awk '{if (NR==1) {printf "%s",$2} else {printf ", %s",$2}}'
cat tmp | awk '{if (NR==1) {printf "\n%s",$3} else {printf ", %s",$3}}'
rm -f mytmp
but here I have another issue for this input:
a "A" 10
b "B" 20
The output is
"A", "B"
10, 20
I want the output to be
A, B
10, 20
That is, I want to remove the double quote around A and B. How can I do this
in awk?
Thanks a lot
~

"\
r*******y
发帖数: 1081
15
来自主题: Linux版 - ./test input and ./test < input
I got it
cat $1 >tmp
cat tmp | awk '{if (NR==1) {printf "%s",$2} else {printf ", %s",$2}}'
cat tmp | awk '{if (NR==1) {gsub("\"", "", $2);printf "\n%s",$3} else {gsub
("\"", "", $2);printf ", %s",$3}}'
rm -f tmp
Thanks a lot
k********r
发帖数: 18
16
来自主题: Programming版 - 问个socket编程中select()的问题。
1。请问用select()的话是不是就block在select()这个函数?
2. 按我的理解, 一般可以把select()放在一个while(1)loop里这样就可以一直监听了。
即:每有一个事件就执行一下然后返回到select()停在那里。 但请看这段程序:
。。。
FD_ZERO(&readfds);
FD_SET(STDIN, &readfds);
while(1){
printf("before select\n");
select(STDIN+1, &readfds, NULL, NULL, NULL);
printf("after select\n");
if (FD_ISSET(STDIN, &readfds))
printf("A key was pressed!\n");
}
printf("Timed out.\n");
......
我一按键,程序就不停地执行:
after select
A key was pressed!
before select
a
d*********1
发帖数: 25
17
来自主题: Programming版 - a C++ interview question..........
what are the results of the folloing output?
it has been tested: 1st--> c; 2nd --> c++
anybody knows why?

float x=0.7;
if(x<0.7)
printf(" c");
else
printf("c++");
cout< x=0.8;
if(x<0.8)
printf(" c");
else
printf("c++");
cout<
s***e
发帖数: 122
18
为了实现多态,C++里增加了virtual这么一个关键字来修饰类的成员函数。
如果一个成员函数是virtual的,那么当你通过一个对象调用它的时候,无论你用的指
针是基类还是子类,都会调到这个对象的实际类型所实现的这个函数。
你可以跑一下下面这个程序看看virtual和非virtual的区别,然后你就知道shadow为什
么和virtual这么相关了。
#include
class A {
public :
void f() { printf("A::f()\n"); };
virtual void g() { printf ("A::g()\n"); }
};

class B : public A {
public:
void f() { printf("B::f()\n"); }
virtual void g() { printf("B::g()\n"); }
};

void main() {
B b;
A* a = &b;
a->f();
a->g(
h**o
发帖数: 548
19
来自主题: Programming版 - func调用结束时出错
请问 这会是 什么 错?
func1 call func2, func2 中的printf("end of func2\n")打出来了。然后没执行
printf("after func2\n");却说程序FATAL: Received Segment Violation...dying:
func1(){
...
result = func2();
printf("after func2\n");
...
}
int func2(){
...
printf("end of func2\n");
return result;
}
这是 syslog打出的 错误 信息:
myProcess Parent: recv_fatal_signal on thread #1 pid 3377
myprocess[3371]: [ID 704344 local4.notice] Child process 3377 byebye: 0 0 1
myprocess[3371]: [ID 123356 local4.notice] myProcess P
b***y
发帖数: 2799
20
☆─────────────────────────────────────☆
GuoChan007 (猪肉刀客) 于 (Sat Mar 31 18:39:56 2007) 提到:
第一种写法:
int main()
{
for(int i=0;i<5;i++)
printf("%d\n",i);
for(int i=0;i<5;i++)
printf("%d\n",i);
}
第二种写法:
int main()
{
for(int i=0;i<5;i++)
printf("%d\n",i);
for(i=0;i<5;i++)
printf("%d\n",i);
}
区别在于第二个循环里面的i需不需要声明?我一直认为标准C++里应该写成第一种写法。
在linux里,我用g++和intel cc编译,都应该是第一种形式,第二种会报错。
但在windows下,我用vc6和intel cc编译,第一种写法却会报错,第二种写法可以通过。
我听说vc6对标准C++支持不好,但intel cc应该没问题啊。
有点糊涂
y****e
发帖数: 23939
21
来自主题: Programming版 - A helloworld OpenMP question?
I want to start 3 threads, but I always get only one.
#include
#include
int main(int argc, char *argv[]){
printf("Hello from master thread.\n");
int num = 3;
omp_set_num_threads(num);
#pragma omp parallel num_threads(3)
{

printf("Team member %d reporting from team of %d\n",
omp_get_thread_num(),omp_get_num_threads() );
printf("Max number of threads for this system is: %d\n",
omp_get_max_threads() );
printf("Number of process
n********y
发帖数: 48
22
来自主题: Programming版 - multithread: how to lock a thread
程序的基本结构如下所示,
class priceserv{
public:
priceserv(){
}
virtual ~priceserv(){
}
int run()
{
// connect to the price server
// waiting for the price events, and call the corresponding
callback function.
}
//callback function
bool on_quote(char *data)
{
//printf data
}
bool on_trade(char *data)
{
//printf data
}
bool on_depth(c... 阅读全帖
d****n
发帖数: 1637
23
来自主题: Programming版 - char s[]和char *ps的不同
char *p="abc"; 在代码段,immutable.
any write operation will cause segfault.
char a[]="abc"; stack, read/write okay
#include
#include
int main(){
char *p="abc";
printf("%p\n",p );
*(p+1)='w'; //segfault
free(p); //segfault or
glibc fault
p =(char *) malloc (sizeof(char)*4) ; //okay, but previous "abc" lost
printf("%p\n",p );

char a[]="def";
printf("%p\t%s\n",a,a); ... 阅读全帖
d****n
发帖数: 1637
24
C面试题
~~~~~~~~~~~
1.使用 #define 定义一个值为一年的秒数的常量,不考虑润年。
~~~~~~~~~~~
2.使用 #define 定义一个返回两个数中较小的一个的宏。
~~~~~~~~~~~~
3.将变量a定义成如下类型:
1. 有符号整数
2. 双精度浮点数
3. 指向一个有符号整数的指针
4. 一个十个成员的有符号整数数组
5. 一个函数指针,指向的函数返回类型为有符号整数,有一个有符号整数类型的参数
~~~~~~~~~~~~
4.C语言中的static的用处是?
~~~~~~~~~~~~
5. 写出下面函数被调用时的输出。
void foo(void)
{
unsigned int a = 6;
int b = -20;
(a+b > 6) ? puts("> 6") :
puts(" < = 6");
}
~~~~~~~~~~~~
6.写出下面程序的输出
#include
#include

typedef struct
{
char flag;
int value;
}SampleSt... 阅读全帖
l***t
发帖数: 10
25
let me try:
~~~~~~~~~~~
1.使用 #define 定义一个值为一年的秒数的常量,不考虑润年。
#define sec_per_year (60*60*24*365)UL
~~~~~~~~~~~
2.使用 #define 定义一个返回两个数中较小的一个的宏。
#define MIN(a,b) ((a)<=(b)?(a):(b))
~~~~~~~~~~~~
3.将变量a定义成如下类型:
1. 有符号整数
int a;
2. 双精度浮点数
double a;
3. 指向一个有符号整数的指针
int *a;
4. 一个十个成员的有符号整数数组
int a[10];
5. 一个函数指针,指向的函数返回类型为有符号整数,有一个有符号整数类型的参数
int (*a)(int);
~~~~~~~~~~~~
4.C语言中的static的用处是?
~~~~~~~~~~~~
5. 写出下面函数被调用时的输出。
void foo(void)
{
unsigned int a = 6;
int b = -20;
(a+b > 6) ? puts("> 6") :
puts... 阅读全帖
d****n
发帖数: 1637
26
来自主题: Programming版 - 如何设计一个停车场。
My implementation using min heap
//file name parkinglots.c
#include
#include
int *lots; //parking lots
#define LOTSMAX 16 //lots size
void Heapify( int i, int mx);
void car_enter();
void car_leave(int i);
void print_lots();
int main(){
int i;
lots=(int *)calloc( (LOTSMAX), sizeof(int));
printf("car enter\n");
for(i=0;i if(lots[0]!=1){
car_enter();
print_lots();
}
}
printf("car leave\n");
for(i=LOTSM... 阅读全帖
d****n
发帖数: 1637
27
来自主题: Programming版 - 如何设计一个停车场。
My implementation using min heap
//file name parkinglots.c
#include
#include
int *lots; //parking lots
#define LOTSMAX 16 //lots size
void Heapify( int i, int mx);
void car_enter();
void car_leave(int i);
void print_lots();
int main(){
int i;
lots=(int *)calloc( (LOTSMAX), sizeof(int));
printf("car enter\n");
for(i=0;i if(lots[0]!=1){
car_enter();
print_lots();
}
}
printf("car leave\n");
for(i=LOTSM... 阅读全帖
F********g
发帖数: 475
28
/* add chars */
char test_str[32] = "Mr John Smith ";
void string_add_char(char *str)
{
unsigned int indx = 0;
char string_m[32];
//char *string_m;
while(*(str+indx) != '\0')
{
if(*(str+indx) != ' ')
{
indx++;
}
else
{
indx++;
if(*(str+indx) == ' ')
{
break;
}
else
{
strcpy(string_m,str+indx);
printf("%s\r\n",string_m);
indx += 2;
strcpy(str+indx,string_m);
*(str+indx-3*sizeof(char))='%';
... 阅读全帖
a9
发帖数: 21638
29
来自主题: Programming版 - Oracle v Google on Java
我觉得api就是function call啊。像printf,你怎么实现printf是你的知识产权,但
printf
我也可以实现,我也叫printf,不违法。

问律
i*****o
发帖数: 1714
30
来自主题: Programming版 - Oracle v Google on Java
可惜他没设计过整个java API.
估计他自己还停留在printf的层次,就像前面有人提到printf不可以被copyright一样
,他真心认为printf不可以被copyright。其实我也同意printf不可以被copyright的。
d****n
发帖数: 1637
31
来自主题: Programming版 - 请教一道题 (转载)
又拓展了一下,给出parent link和buffed level computation
#include
#include
int *buff=NULL;
int buff_size=1024;
int buff_used=0;
int level(int query ){
while( buff[buff_used-1] {
if ( buff_size <=buff_used ){
buff_size<=1;
buff=(int *)realloc( buff,sizeof(int)*buff_size );
}
buff_used++;
buff[buff_used]=(buff_used+1 )*(buff_used+1+1)/2;
}
... 阅读全帖
S*A
发帖数: 7142
32
我的机器没有Wei 的好,
但是相对结果还是可以看出来的。
原来的测试程序是严格顺序向后找, 虽然看上去
跳了 40 byte, 但是仍然小于 64 byte 的cache line
size. 但是从访问内存的次序来看是严格增加的。
这个是 cache 最爽的状态。
我改变了输入的次序,变成随机定票段,从输入数组
里面进来,其他的不变。构造输入数组的时间是刨去的。
程序里面 USE_RAND = 1 vs 0
仅仅变的是订票的次序,结果呢?
速度差了整整 4 倍。从 28M 暴减少到7M.当然实际订票
更接近与随机分布。谁排好了一个接一个定。
这就是为什么我会担心 IO 是瓶颈的问题。外部进来的数据,
全部都是 cache miss。这些纯计算的模拟还不能很好的代表真实
的负载情况。
程序如下:
比较快的机器建议增加 ITER 大小,使得总共运行时间超过几十秒。
#include
#include
#include
#define SEGMENTS 10000000
#define INPUTS 1000... 阅读全帖
r*****8
发帖数: 2560
33
来自主题: Programming版 - C 语言,初学者,简单问题
问题1. 为什么 sizeof(test))总是显示8?
问题2. *test 的大小从12变到36字母没问题吗?
#include
#include
void main(void)
{
char *test = "abcdefghijk";
printf("Size of test is %li \n", sizeof(test));
// 结果显示:8,应该是12啊,怎么会是8?
printf("The string test is %s \n", test);
// 结果打印出 "abcdefghijk"
test = "abcdefghijk abcdefghijk abcdefghijk";
printf("Size of test is %li \n", sizeof(test));
// 结果显示还是8,应该是36啊,怎么会是8?
printf("The stri... 阅读全帖
s******e
发帖数: 2181
34
谢谢解惑,我的问题是这样的,这是一个被我简化后的在matlab环境下运行的C程序。
我想测试我输入的数据是否被正确读入。一个途径是通过把输入参数赋给输出的地址,
来得到结果,看来是正确的。另一个是通过printf把输入数据直接打出来,可是这个打
出来的数字始终为0,无论采用printf("input=%dn", data2[0]); 还是printf("input=
%dn", *data2);都一样。真是见鬼了,那data2这个地址上存的数据到底是什么
#include "mex.h"
#include "gpu/mxGPUArray.h"
#include "cuda.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, mxArray const *prhs[])
{
double *data1; *data2;
int m,n;
m=mxGetM(prhs[0]);
n=mxGetN(prhs[0]);
plhs[0]=mxCreateDoubl... 阅读全帖
w****n
发帖数: 31
35
来自主题: Computation版 - 老大们再帮一把吧
re-run the code with the folloiwng modify but sitll not working.
#include
int main()
{
long double a;
a = 1.0/3.0;
printf("%40.40Lf\n",a);
printf("float=%d\n", sizeof(float));
printf("double=%d\n", sizeof(double));
printf("long double=%d\n", sizeof(long double));
return 0;
}
mary015-01dhcp57:/home/wilson/program/PRECISION # icc -long_double long.c
mary015-01dhcp57:/home/wilson/program/PRECISION # ./a.out
0.333333333333333314829616256
D****y
发帖数: 462
36
家庭医生完全不用机器人,一个麦可风加一片8086就行了,内制三行代码
printf ("You are fine. Go home. n");
printf ("Sleep more. n");
printf ("Drink more water. n");
q******u
发帖数: 46
37
来自主题: JobHunting版 - 算法:按照字典序求第k个排列数
可以算出第一位是floor((k-1)/(n-1)!)+1,剩余的递归就好了。非递归的程序如下:
void print_comb_kth(int n, int k){
int fac = 1;
int i, j;
int *buf;
for (i=2; i<=n; i++) fac*=i;
if (k>fac || k<1){
printf("Index out of range!\n");
return;
}

if (n==1){
printf("1\n");
return;
}
buf = (int*) malloc(sizeof(int)*n);

for (i=1; i<=n; i++) buf[i-1] = i;
for (i=1; i fac /= (n+1-i);
printf("%d", buf[(k-1)/fac]);
j = (k-1)/fac;
while (j buf[j] = buf[j+1];
r**u
发帖数: 1567
38
来自主题: JobHunting版 - this question is nice
你可以build a tree。然后postfix traverse tree,第一次走到这个节点printf
,当左右子树都遍历过了,就printf<\x>。当然,leaf节点要printf number。
a
/ \
b e
/ \ 6
c d
1 4

..
n**h
发帖数: 154
39
来自主题: JobHunting版 - 螺旋打印matrix
前两天正好练了一下手
void serial_print_matrix(int A[N][N])
{
int i;
int begin, end;
int top;
for (begin = 0; begin < N / 2; begin++) {
end = N - begin - 1;
//top
for (i = begin; i < end; i++) {
printf("%2d ", A[begin][i]);
}
//right
for (i = begin; i < end; i++) {
printf("%2d ", A[i][end]);
}
//bottom
for (i = begin; i < end; i++) {
printf("%2d ", A[end][end - i + begin]);
}
//left
for (i = begin
P*******b
发帖数: 1001
40
来自主题: JobHunting版 - why this can compile?
#include
int main()
{
int a=65;
switch(a)
{
case 'A':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
slfsjsdlfjsdldfs:
printf("NONE\n");
}
return 0;
}
e******d
发帖数: 310
41
来自主题: JobHunting版 - Two C++ questions from Bloomberg on-site
Some code used to verify the above discussions. If you find the code is not correct, please revise it. Thank you.
=========================================================
#include "stdio.h"
#include
class MyException
{
public:
MyException(){
printf("Constructor MyException() is called. \n");
}
~MyException(){
printf("Destructor ~MyException() is called. \n");
}
private:

};
class A{
public:
A()
{
printf("A() is called \n");
}
~A
c****s
发帖数: 241
42
来自主题: 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 ()
{
UD
发帖数: 182
43
来自主题: JobHunting版 - one amazon interview problem
below code seems to work, and should be O(n) time and O(1) space, if you disagree, please list your argument.
The idea behind is the use and resue of sum(i,j), that's why the O(1) space.
sum(i,j)=sum of all the 1s between i and j.
sub-seq(i,j) has equal 1s and 0s when sum(i,j)*2=j-i+1, we start by setting i=0,j=A.length-1, then searching from both ends.
1. if sum(i,j)*2==j-i+1, then found it
2. else if sum(i,j)*2>j-i+1, we have more 1s then 0s, then we check A[i] and A[j]:
a. if A[i]==A[j], t... 阅读全帖
i******s
发帖数: 301
44
来自主题: JobHunting版 - 问两道google题
你的解法应该是对的,基于这个写了如下C++代码
/*
* Author: Shengzhe Yao
* Date: 08 Nov 2010
*/
#include
#include
#include
#include
using namespace std;
// T(n) = \sum_{i=1}^{n-1} (T(i) * H(i+1))
int findAllStr(const set &dict, char *str,
int end, int *lookup_table) {
if (lookup_table[end] > 0)
return lookup_table[end];
int total = 0;
for (int i=1; i < end; ++i) {
char *substr = (str+i+1);

if (dict.find(substr) != dict.end()) {
char tmp =... 阅读全帖
c********v
发帖数: 21
45
来自主题: JobHunting版 - Arista Networks面经2
2 hours
ssh programming:
1. Write a generic [template ] stack class.
1.1 write the interface, [push,pop...]
1.2 implement push
1.3 how to use it for array of stacks.
2. what's the output of the following program
2.1
int main() {
char * s="12345";
printf("%d\n",s);
}
what is the output?
what is the value in memory of the byte that is pointed by s?
2.2
int get() {
static int n = 0;
return ++n;
}
int printf( char const * fmt, ... ) {
// breakpoint
...
}
int main() {
printf ( "%d %d\n", ge... 阅读全帖
c***2
发帖数: 838
46
来自主题: JobHunting版 - map numbers to strings
Ok. I play it further to get a utility program.
==================================================
/* telwords.c

Given a telephone number (or any number),
print all possible words based on
digits-letters mapping from telephone key pads.
*/

#include
#include
#include
static char *digitsmaps[]={
"0",
"1",
"ABC",
"DEF",
"GHI",
"JKL",
"MNO",
"PQRS",
"TUV",
"WXYZ",
"#",
NULL
};

void doPrintTelephoneWo... 阅读全帖
w*******n
发帖数: 773
47
来自主题: JobHunting版 - c++ 问题
main()
{
char *c1 = "abc";
char c2[] = "abc";
char *c3 = ( char* )malloc(3);
c3 = "abc";
printf("%d %d %s\n",&c1,c1,c1);
printf("%d %d %s\n",&c2,c2,c2);
printf("%d %d %s\n",&c3,c3,c3);
getchar();
}
运行结果
2293628 4199056 abc
2293624 2293624 abc
2293620 4199056 abc
看运行结果
为什么c2所指的地址,
和c2 自己的地址是一样,
我运行了,也是这样的。
f*******n
发帖数: 8
48
来自主题: 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)
     {
   &... 阅读全帖
o****d
发帖数: 2835
49
来自主题: JobHunting版 - 问个面试题
C程序
思路如上面的朋友所说
=======
#include
#include
void setMatrix (int ** matrix, int x, int y, int n, int start)
{
int i,j;
if(n==0)return;
else if(n==1) matrix[x][y]=start;
else{
for(j=y;j matrix[x][j]=start++;
for(i=x;i matrix[i][y+n-1]=start++;
for(j=y+n-1;j>y;j--)
matrix[x+n-1][j]=start++;
for... 阅读全帖
f****4
发帖数: 1359
50
来自主题: JobHunting版 - 问Thomson Reuters两道算法题
抛个没有count的;一次写对还是有难度的
grass的count的办法能简化代码
#include
int k = 0;
int pts[k+1]; //pts[0] for color 1; pts[k-1] for color k; pts[k] is the
current index in balls
void printArray(int *arr, int length)
{
for(int i = 0; i < length; i++)
printf("%d,",arr[i]);
printf("\n");
}
void KquickSortPartition(int *arr, int length)
{
if (arr == NULL || length == 0 || k <= 0) return;
printArray(arr, length);
for (int i = 0; i < k+1; i++)
pts[i] = 0;
int ball;
int color;
while (pts[k... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)