由买买提看人间百态

topics

全部话题 - 话题: stdio
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
s******n
发帖数: 3946
1
来自主题: JobHunting版 - leetcode上一题,求正解
写了几遍才对。。。
#include
#define exchangenum(a,b) {a^=b;b^=a;a^=b;}
void print(int* a, int count, int sum, int* out, int outindex)
{
if (sum==0) {
for (int i=0; i printf("\n");
return;
}
if (count==0) return;
out[outindex] = a[0];
print(a+1, count-1, sum-a[0], out, outindex+1);
print(a+1, count-1, sum, out, outindex);
}
int main(int argc, char** argv)
{
int a[5] = {1,3,4,2,6};
int sum = 7;
int out[5];
print(a, sizeof(a)/sizeof(a[0... 阅读全帖
s******n
发帖数: 3946
2
来自主题: JobHunting版 - leetcode上一题,求正解
如果有1,3,3,4,5,3重复,则需要特别处理,2个连续的3,分别处理3,3,4... 3,4... ,4.... 三种情况,应该没比我这程序更简短的了吧。:)
#include
void print(int* a, int count, int sum, int* out, int outindex)
{
if (sum==0) {
for (int i=0; i printf("\n");
return;
}
if (sum<0 || count==0) return;
int samea=1;
for (; samea for (int k=0; k<=samea; k++) {
print(a+samea, count-samea, sum-a[0]*(k), out, outindex+k);
out[outindex+k] = ... 阅读全帖
c*********n
发帖数: 182
3
来自主题: JobHunting版 - Unix高手来看看GS的UNIX题
• How many ways can you think of to remove a file called '-r'?
unlink/rm/...?
• How might one trap a SIGKILL from within shell, or C, Perl or
Python?
Perhaps there are nothing you can do with SIGKILL without patching the init.
..
• Can a Unix process become unkillable? If so, how?
Yes. uninterruptible sleep
• How would you find the disk space consumed by a directory without
using du?
don't know. find . | ls | awk | sort | awk | ... ?! &*^(&@#(*^@#
• W... 阅读全帖
s******n
发帖数: 3946
4
来自主题: JobHunting版 - 有个SB interviewer和我说++i比i++好
他的意思是假设是operator重载
++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
设编译无优化)
大家看有道理吗?
I did a real test on arm compiler turn off optimization:
the O0 code is exactly the same, except that post operator++()(int dummy)
has an extra dummy parameter which is required by c++ to identify the
difference of prefix and postfix.
2nd, even I change the Test& operator++() into Test operator++(), it still generates the same code.
printf("%d \n", 10+ (abc++).value);
sub r3, fp, #8
mov r0, ... 阅读全帖
s******n
发帖数: 3946
5
来自主题: JobHunting版 - 有个SB interviewer和我说++i比i++好
the O0 code is exactly the same, except that post operator++()(int dummy)
has an extra dummy parameter which is required by c++ to identify the
difference of prefix and postfix.
2nd, even I change the Test& operator++() into Test operator++(), it still generates the same code.
printf("%d \n", 10+ (abc++).value);
sub r3, fp, #8
mov r0, r3
mov r1, #0 --> the dummy parameter of postfix
bl _ZN4TestppEi
mov r3, r0
ldr r3, [r3, #0... 阅读全帖
h*****f
发帖数: 248
6
来自主题: JobHunting版 - C++ template
1. Compile time because template initialization is done during the compile
time and the initialization is based on the "known" template parameter(s)
during the compile time.
2. A way to prove:
#include
#include
template class Buffer {
char internal[max];
public:
void printSize() {
printf("buffer size=%lu\n", sizeof(internal));
}
};
int main() {
Buffer<512> my512;
my512.printSize();
size_t v;
std::cin >> v;
printf("my input=%lu\... 阅读全帖
s******n
发帖数: 3946
7
来自主题: JobHunting版 - Rotating an array in place
0 1 2 3 4 5 6 7 (N=8, k=3)
->
3 4 5 6 7 0 1 2
leetcode上那个要移动2N次,写了个只要移动N次的
#include
void rotate(int* a, int N, int k)
{
if (N<=1) return;
if (k>N) k=k%N;
if (k==0) return;
int moved = 0;
for (int i=0; moved int tmp = a[i];
int current = i;
do {
moved++;
int next = (current+k)%N;
if (next == i) {
a[current] = tmp;
break;
} else {
a[current] = a[next];
current = next;
}
} while (1);
}
}
int main()
{
int a[10] = {... 阅读全帖
d****n
发帖数: 1637
8
my solution
runningtime space O(1)
any suggestions or improvement?
///file mitbbs.c
#include
int arr[]={-5,2,-3,4,-8,-9,1,3,-10};
int sz=9;
void print_arr();
int main(){
print_arr();
int i,j,k,t;
for(i=0,k=0;i if(arr[i]<0 ){
t=arr[i];
for(j=i-1;j>=k ;j-- ){
arr[j+1]=arr[j];
... 阅读全帖
S******t
发帖数: 151
9
来自主题: JobHunting版 - 问个IQ 题
#include
#include
using namespace std;
char cross[3000][20];
int main()
{
int n,i,nc=0,sum=0,t[1000];
scanf("%d",&n);
for(i=0;i scanf("%d",&t[i]);
sort(t,t+n);
for(i=n-1;i>=3&&2*t[1] {
sprintf(cross[nc++],"%d %d\n",t[0],t[1]);
sprintf(cross[nc++],"%d\n",t[0]);
sprintf(cross[nc++],"%d %d\n",t[i-1],t[i]);
sprintf(cross[nc++],"%d\n",t[1]);
sum+=2*t[1]+t[0]+t[i];
}
for(;i>=2;i... 阅读全帖
c****p
发帖数: 6474
10
来自主题: JobHunting版 - 问个结构体的大小问题
#include
typedef struct structc_tag
{
char c;
double d;
int s;
} struct_t;
int main()
{
struct_t a;
printf("%x %x %x %d\n", &(a.c), &(a.d), &(a.s), sizeof(a));
return 0;
}
7f011690 7f011698 7f0116a0 24
S**I
发帖数: 15689
11
来自主题: JobHunting版 - 罗马数字转换成十进制
楼上的全是Java,俺来一个C的吧(各种exception懒得写了):
#include
int getDecimal(char);
int romanToDecimal(char *);
char getRoman(int);
char * decimalToRoman(int, char *);
int main(int argc, char* argv[]){
printf("Roman: %s\n", argv[1]);
int d = romanToDecimal(argv[1]);
printf("Decimal: %d\n", d);
char r[1000];
decimalToRoman(d, r);
printf("Roman: %s\n", r);
}
int getDecimal(char a){
switch(a){

case 'I':
return 1;
case 'V':
return 5;
case 'X':... 阅读全帖
e***n
发帖数: 42
12
第一遍扫描array用hashmap存array[i],
第二遍扫描找出hashmap(target - array[i])
但是如果遇到重复多次的数(如下例程 x=5)且x+x=target=10, C++ 的hashtable (
map) 就只存第一次出现的x 请问有什么办法解决?
#include
#include
#define N 12
using namespace std;
void find2SumTarget(int arr[N], int target){
map hashT;
int i;
map::iterator m;
for (i=0; i hashT.insert(make_pair(arr[i], i));
}
for (i=0; i m = hashT.find(target - arr[i]);
if ( m!=hashT.end() && i!=... 阅读全帖
e***n
发帖数: 42
13
搞定.用了multimap 和两个iterator.
#include
#include
#define N 13
using namespace std;
void find2SumTarget(int arr[N], int target){
int i;
multimap hashT;
multimap::iterator iter1, iter2;
typedef multimap::size_type sz_type;
for (i=0; i hashT.insert(make_pair(arr[i], i));
}
iter1 = hashT.begin();
while(iter1 != hashT.end()){
iter2 = hashT.find(target - arr[iter1->second]);
sz_type entries = has... 阅读全帖
l***h
发帖数: 392
14
来自主题: 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(... 阅读全帖
m***y
发帖数: 1
15
来自主题: JobHunting版 - 贡献一道G家的面试题
同意楼上peking2。从前往后,去A,从后往前,double B。
C代码如下:
#include
#define MAX_S_LENGTH 1024
void remove_a_double_b(char *s) {
int i, j, n, na, nb, nc; // nc is number of characters other than A
na = nb = 0;
j = 0;
for (i = 0; s[i] != '\0'; i++) {
if (s[i] == 'A')
na++;
else {
s[j] = s[i];
j++;
if (s[i] == 'B')
nb++;
}
}
n = i;
nc = j;
i = n - na + nb;
s[i] = '\0';
i--;
f... 阅读全帖
j********e
发帖数: 1192
16
来自主题: 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... 阅读全帖
j********e
发帖数: 1192
17
来自主题: JobHunting版 - 插入节点到complete binary tree的末尾
写了个使用O(1)memory, O(logN * logN) (N是tree的size)的程序。
类似于binary search的算法,测试代码也在下面,应该没有大bug。
先获得树的高度h,然后比较h和root->right子树的高度+1,如果相同,
说明树最后一个节点在root->right,否则最后一个节点在root->left的子树。
#include
#include
#include
#include
using namespace std;
class Node {
private:
Node *left;
Node *right;
int value;

public:
Node() {
left = right = NULL;
value = 0;
}

Node(int v) {
left = right = NULL;
value = v;
}

int Height(Node *node) {
int h... 阅读全帖
g*********e
发帖数: 14401
18
来自主题: JobHunting版 - 2 sigma 的这个recruiter真不靠谱
two sigma的code test都是用stdio,有点不习惯
j***y
发帖数: 1069
19
来自主题: JobHunting版 - 发个mathworks电面
好像大多数人对他家都不感冒,不过希望还是可以帮到某些同学。Application
Support Engineer,选的信号处理和C语言。找工作以来第一次碰到印度人面试,说不能
翻书查电脑没说不可以透题。问题如下
信号处理
1. Nyquist sampling theorem
2. aliasing
3. convolution
4. impulse response
5. FIR vs IIR
5. y(n) = x(n)*x(n-1),是不是causal,linear, time-invariant?why?
6. limit sin(x)/x as x->0 ? why?
7. z-transform, Fourier transform 关系
8. 1,2,3 (t=0),0,1,求Z transform,是否causal?
9. what is PSD?
10. PSD of a white noise signal
数学
11. rank of a matrix
12. nullspace of a matrix
13. singular matrix
14. eigen... 阅读全帖
j***y
发帖数: 1069
20
来自主题: JobHunting版 - 发个mathworks电面
好像大多数人对他家都不感冒,不过希望还是可以帮到某些同学。Application
Support Engineer,选的信号处理和C语言。找工作以来第一次碰到印度人面试,说不能
翻书查电脑没说不可以透题。一边考试一边记题,问题如下
信号处理
1. Nyquist sampling theorem
2. aliasing
3. convolution
4. impulse response
5. FIR vs IIR
5. y(n) = x(n)*x(n-1),是不是causal,linear, time-invariant?why?
6. limit sin(x)/x as x->0 ? why?
7. z-transform, Fourier transform 关系
8. 1,2,3 (t=0),0,1,求Z transform,是否causal?
9. what is PSD?
10. PSD of a white noise signal
数学
11. rank of a matrix
12. nullspace of a matrix
13. singular matrix
... 阅读全帖
S**I
发帖数: 15689
21
N久以前写的一个:
#include
int getDecimal(char);
int romanToDecimal(char *);
char getRoman(int);
char * digitToRoman(int, int, char *);
char * decimalToRoman(int, char *);
int main(int argc, char* argv[]){
printf("Roman: %s\n", argv[1]);
int d = romanToDecimal(argv[1]);
printf("Decimal: %d\n", d);
char r[1000];
decimalToRoman(d, r);
printf("Roman: %s\n", r);
}
int getDecimal(char a){
switch(a){

case 'I':
return 1;
case 'V':
return 5;
... 阅读全帖
l****c
发帖数: 838
22
来自主题: JobHunting版 - C的fscanf的问题 (转载)
You should read in the string and parse.
You don't know how long the first part string is or how long the number is.
So if you define:
char tempprice[10];
char ticker[10];
You have the risk of buffer overflow.
Here is my solution. I debug it as I wrote it, so it is not optimized.
it is pure C. You can get result with 2 lines of perl or python code
============================
#include
#include
#include
int main()
{
char *str = "GOOD|89.34";
char *ptoken, *... 阅读全帖
c********s
发帖数: 817
23
This is my implementation for these two functions, and a driver to run them.
cat string_reverse.c
#include
#include
#include
void swap(char* c1, char* c2);
// -----------------
void string_reverse1(char* string) {
if (string == NULL)
return;
char* head = string;
char* tail = head;
// find the tail
while (*tail) ++tail;
--tail;
// while loop to do the swap
while (head < tail)
swap(head++, tail--);
}
// -----------------
// the caller of this function is res... 阅读全帖
i****1
发帖数: 445
24
来自主题: JobHunting版 - 大家帮忙看看这个副作用问题
#include
int main(void)
{
int i = 0;
char a[3] = {0,0,0};
a[++i] = i++;
return 0;
}
运行完后各个值分别是多少?
i
a[0]
a[1]
a[2]
i 肯定是2。
a[++i] = i++这句:
先右边i++:i的值0作为表达式的值赋给左边,然后i自增1,i的值为1
再左边a[++i]:i自增1,i的值为2,i的值2作为表达式的值,所以应该是给a[2]赋值。
所以这句话的意思应该是a[2] = 0。
最后a[0, 1, 2]全部是0。
但是我做visual studio里试了,a[0] = a[2] = 0, a[1] = 1。
这是怎么回事?
h**6
发帖数: 4160
25
来自主题: JobHunting版 - 一道复杂的题,求解法
用力矩的算法,复杂度O(n^2)
#include
#include
#include
#include
using std::string;
using std::vector;
using stdext::hash_map;
void weightcenter(string str)
{
int n = str.size();
vector w(n);
for(int i = 0; i < n; i++) {
w[i] = str[i] - '0';
}
vector >weight(n, vector(n));
vector >torque(n, vector(n));
int maxlen=-1, maxi=-1, maxj=-1;
for(int len = 1; len <= n/2; len++) {
hash_map阅读全帖
b*****u
发帖数: 648
26
试着写了一个递归的
思路: 每个recursion的开始有一个factor,一个当前operand和一个之前乘/除号
首先把operand = operand 乘除 factor
然后,如果下一符号是加减,则返回 operand 加减下一recursion(factor=1, 乘)
如果下一符号是乘除,则返回下一recursion (factor=当前operand, 下一符号)
(on a second thought, 这个其实也就是实现了一个stack的功能,递归即stack)
#include "stdafx.h"
#include
#include
#include
using namespace std;
int stringCalculator(string input, int factor, char sign)
{
int len = input.length();
if (len == 0) return 0;
int i = 0;
while (i< len && inp... 阅读全帖
b*****u
发帖数: 648
27
试着写了一个递归的
思路: 每个recursion的开始有一个factor,一个当前operand和一个之前乘/除号
首先把operand = operand 乘除 factor
然后,如果下一符号是加减,则返回 operand 加减下一recursion(factor=1, 乘)
如果下一符号是乘除,则返回下一recursion (factor=当前operand, 下一符号)
(on a second thought, 这个其实也就是实现了一个stack的功能,递归即stack)
#include "stdafx.h"
#include
#include
#include
using namespace std;
int stringCalculator(string input, int factor, char sign)
{
int len = input.length();
if (len == 0) return 0;
int i = 0;
while (i< len && inp... 阅读全帖
s********l
发帖数: 998
28
来自主题: JobHunting版 - 问2道面试题
1.
一个c 文件里有
#include
在main() 里面 有scanf();
用make 生成a.out后
a.out里有没有scanf()的code
2.
scanf()如何知道是从那个device得到输入?
谢谢
d**********x
发帖数: 4083
29
来自主题: JobHunting版 - LinkedIn 面经
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(... 阅读全帖
d**********x
发帖数: 4083
30
来自主题: JobHunting版 - LinkedIn 面经
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(... 阅读全帖
e*******i
发帖数: 56
31
来自主题: JobHunting版 - hackerrank的xor题目
C code as following. Passed first test case. Could not figure out why failed
the others. Please help
//////////////////////////////
#include
#include
#include
#include
int main() {
int nCases;
scanf("%d\n", &nCases);

for(int i=0;i {
int N;
int Q;
scanf("%d %d\n",&N, &Q);

short int *A=malloc((N+1)*sizeof(short int));
for(int j=1;j<=N;++j) scanf("%d ", &A[j]);

s... 阅读全帖
e*******i
发帖数: 56
32
来自主题: JobHunting版 - hackerrank的xor题目
Fixed some pointer casting issue. Now it scored 42+ points but still failed
test case 1,2,3,4,5,10 due to TLE.
/////////////////////
#include
#include
#include
#include
int main() {
int nCases;
scanf("%d\n", &nCases);

for(int i=0;i {
int N;
int Q;
scanf("%d %d\n",&N, &Q);

short int *A=(short int *)malloc((N+1)*sizeof(short int));
for(int j=1;j<=N;++j) scanf("%hd ", &A[j]);
... 阅读全帖
d**********x
发帖数: 4083
33
来自主题: JobHunting版 - Haker Rank Median...
那题目貌似就没卡复杂度。。。枉我昨天下班后还捏着鼻子写了个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),... 阅读全帖
s*********s
发帖数: 318
34
来自主题: 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... 阅读全帖
g*********e
发帖数: 14401
35
来自主题: JobHunting版 - 请问一道概率题,谢谢了先
#include
#include
#include
using namespace std;
double getRand() {
double ran=rand()/(double)RAND_MAX;
return ran;
}
int main() {
double E=0;
double sum=0;
double A=getRand();
sum=A;
double B;
int count=1;
double total;
int times=100000;
for(int i=0;i count=1;
A=getRand();
sum=A;
while(B=getRand()){
sum+=B;
count++;
if(B>=A)
break;
else {
A=B;
}
}
E=sum/count;
... 阅读全帖
s********r
发帖数: 403
36
不知道下面这种满不满足你的需求
一般来讲,宏函数短小,主要还是靠精确的设计不靠debug
$ cat macro.c
#include
#define PRINT(str) \
do \
{ \
int i = 1; \
printf("%s %dn", (str), i); \
} \
while (0);
int main(void)
{
const char *sz_in = "Hello";
PRINT(sz_in);
return 0;
}
gcc -ggdb3 macro.c
(gdb) info MACRO PRINT
Defined at /usr/local/src_test/macro.c:3
#define PRINT(str) do { int i = 1; printf("%s %dn", (str), i); } while (0);
(gdb) si
0x0000000000400540 15 PRINT(sz_in);
(gdb) p i
$1 = 1
(gdb) p sz_in... 阅读全帖
h**6
发帖数: 4160
37
把楼主的代码翻译成c++了,输出为Hello。
#include
void mutate(const char *o)
{
o = "Goodbye";
}
int main ()
{
const char *o = "Hello";
mutate(o);
printf("%s\n", o);
return 0;
}
如果mutate函数定义为void mutate(const char *&o),则输出为Goodbye。
R*****i
发帖数: 2126
38
来自主题: JobHunting版 - 一道老题目, 求最快捷解法
这个题目建议用红黑树做可能最快捷有效。红黑树每一次插入,删除都是log(k),求平
均值非常简单,就是根部的那个节点。所以总的计算量是O(nlog(k))。
以下是c/c++代码(需要修改数组,以便一个一个输入)。
#include
#include
#include
#define INDENT_STEP 4
enum rbtree_node_color {RED, BLACK};
typedef struct rbtree_node_t {
int value;
struct rbtree_node_t* left;
struct rbtree_node_t* right;
struct rbtree_node_t* parent;
enum rbtree_node_color color;
} *rbtree_node;
typedef rbtree_node node;
typedef enum rbtree_node_color color;
typedef struct... 阅读全帖
l*********o
发帖数: 3091
39
来自主题: 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... 阅读全帖
p****o
发帖数: 46
40
oj 在{0, 1}测试的结果是{{1}, {0,1}, {1,0}}
但我自己运行打印出来是{{0,1}, {1,0}}
请大伙帮忙看看。
代码如下
#include "stdio.h"
#include
using namespace std;
class Solution {
public:
vector > permute(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector v(0);
doPermute(v, num);
return vv;
}

void doPermute(vector &v, vector &num) {
if(num.empty()) {
vv.push_back(v); ... 阅读全帖
p*u
发帖数: 136
41
来自主题: JobHunting版 - fb电面面经
#include
#include
#include
using namespace std;
#define INF 0x7fffffff
struct Log
{
int login_time;
int logout_time;
Log(int in, int out): login_time(in), logout_time(out)
{}
};
struct Node
{
int time;
int value;
Node(int t, int v): time(t), value(v)
{}
};
bool cmp(const Node &a, const Node &b)
{
if (a.time != b.time)
return a.time < b.time;
else
return a.value > b.value;
}
void output(int start, int end, int ... 阅读全帖
p*u
发帖数: 136
42
来自主题: JobHunting版 - fb电面面经
#include
#include
#include
using namespace std;
#define INF 0x7fffffff
struct Log
{
int login_time;
int logout_time;
Log(int in, int out): login_time(in), logout_time(out)
{}
};
struct Node
{
int time;
int value;
Node(int t, int v): time(t), value(v)
{}
};
bool cmp(const Node &a, const Node &b)
{
if (a.time != b.time)
return a.time < b.time;
else
return a.value > b.value;
}
void output(int start, int end, int ... 阅读全帖
m**n
发帖数: 384
43
下面代码,如果把所有*去掉,结果是对的,
但我需要用指针传递数据,所以都用了指针,编译后,
结果就是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");
}
m**n
发帖数: 384
44
为了比较,下面这段代码是正确的啊
#include
main()
{int *p;
*p=0;
(*p)++;
(*p)++;
printf("%d\n",*p>1? 1:*p);
}
编译后,结果是 1
J**9
发帖数: 835
45
来自主题: JobHunting版 - wordbreak in C?
Problem at
http://discuss.leetcode.com/questions/765/word-break
This does not seem to be a DP problem, just do it in a straightforward way.
Here's my implementation in C. Any issue?
Thanks.
//===========================================================
#include
#include
#include
#include
#include
/**
* Given a string s and a dictionary of words dict, determine if s can be
segmented into a space-separated sequence of one or more
* dictionary w... 阅读全帖
b******p
发帖数: 49
46
来自主题: JobHunting版 - leetcode上的Sort List那道题
我来贴个CPP的(注意:以下有乱七八糟的code……)
合并排序确实比较好用,我还在写第一遍leetcode,代码风格也比较乱,带了很多
debug code,还带了测试用例,试了9次才通过…
有一些多边界条件需要判断的。我的方法是加很多debug,或加很多assertion(我做别
的题里面经常用assertion……不知道是不是好习惯)
============================
#include
#include
using namespace std;
/*
Merge sort ?
*/
// Start: 22:40
// End: 23:45 用了一个小时
//Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
#define TOMMYDBG
class Solution {
public:
... 阅读全帖
C*N
发帖数: 1792
47
#include
#include
#include
using namespace std;
struct currentListStruct
{
double A;
double B;
std::vector values;
};
void getMaxNSum (std::vector & array1, std::vector & array2,
currentListStruct & cur, int N)
{
if (array1.empty () || array2.empty ()) {
return;
}
cur.A = array1.back();
cur.B = array2.back();
cur.values.push_back (cur.A + cur.B);

double a=array1.front()-1, b=array2.front()-1;
... 阅读全帖
l***p
发帖数: 358
48
来自主题: JobHunting版 - linkedin,rocketfuel, google面经若干
中间有0的情况,比如
10042319
#include
int main(int argc, char * argv[])
{
int num = atoi(argv[1]);
char s[] = "45002319";
printf("source: %s and num:%d", s, num);
size_t size;
while (num > 0 && (s && (size = strlen(s)) > 0))
{
if (size > 1)
{
if (s[0] > s[1])
{// 4[<4, 0]
size_t cz = 1;
char * n = s + 1;
... 阅读全帖
m**********g
发帖数: 153
49
来自主题: JobHunting版 - A家onsite, 求bless
这个验证通过:
#include
#include
#include
using namespace std;
void rotate(int a[], int k, int n) {
int i=0;
k = k%n;
for (; i+k if (n%k == 0) return;
rotate(&a[i], k - n%k, k);
}
int main()
{
int i;
int a[]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
rotate(a, 5, 12);
for(i=0; i printf( "%d ", a[i]);
}
printf("n");
return 0;
... 阅读全帖
c**y
发帖数: 172
50
来自主题: JobHunting版 - 请教一个c的概念题
写了个程序测了一些,没有发现你的说的问题,both (p < q) and (p > q) are
possible, depending on how the parameters are passed. The code below is
compiled with c++ 11.
#include
#include
#include
using namespace std;
void foo(int *p, int *q)
{
while (p cout << "p < q" < return;
}
cout << "p > q" << endl;
return;
}
int main() {
int *pa = new int(10);
int *pb = new int(20);
foo(pa, pb);
foo(pb, pa);
return 0;
}
======== output =... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)