由买买提看人间百态

topics

全部话题 - 话题: stdio
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
e******d
发帖数: 310
1
来自主题: 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**y
发帖数: 172
2
来自主题: JobHunting版 - 电话面试排列组合题
Below is my code. However, (101,2)^2 looks the simplest.
#include
#include
#include
using namespace std;
int countRect(int x, int y) {
if ((x == 0) || (y == 0)) {
return 0;
} else if ((x == 1) && (y > 1)) {
return y;
} else if ((x > 1) && (y == 1)) {
return x;
} else {
return (x * y);
}
}
int countAllRect(int n, int m) {
if ((n <= 0) || (m <= 0)) {
return 0;
} else if ((n == 1) && (m > 1)) {
t******e
发帖数: 1293
3
来自主题: JobHunting版 - array of pointers to functions
c
zz from http://www.java2s.com/Code/C/Function/Arrayoffunctionpointer.htm
#include
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int (*p[4]) (int x, int y);
int main(void)
{
int result;
int i, j, op;
p[0] = sum; /* address of sum() */
p[1] = subtract; /* address of subtract() */
p[2] = mul; /* address of mul() */
p[3] = div; /* address of div() */
printf("Enter two numbers: ");
scanf("%d %d", &i, &j);

printf("0: Ad
t****a
发帖数: 1212
4
来自主题: JobHunting版 - 一道面试题——取珠宝
O(n^2) DP solution here:
#ifdef HAVE_CONFIG_H
#include
#endif
#include
#include
int a[] = {4,6,1,7,2,10,8,5,3,9};
const int n = 10;
int sum[n][n];
int best[n][n];
int game(int a[], int start, int stop) {
if (start > stop) {
return 0;
} else if (best[start][stop]!=-1) {
return best[start][stop];
} else {
int s1 = sum[start+1][stop] - game(a, start+1, stop) + a[start];
int s2 = sum[start][stop-1] - game(a, start, stop-1) +
UD
发帖数: 182
5
来自主题: 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... 阅读全帖
t****a
发帖数: 1212
6
来自主题: JobHunting版 - 问道题,看不太懂
han6 come first
qu you wu, zhou lang gu :)
here goes DP solution, as han6 said, O(m^3)
#ifdef HAVE_CONFIG_H
#include
#endif
#include
#include
int cost[100][100];
int best_cut(int position[], int start, int stop) {
// printf("%d\t%d\n", start, stop);
if (cost[start][stop]!=-1) {
return(cost[start][stop]);
} else {
int cost0 = position[stop] - position[start];
if (start+1 == stop) {
cost[start][stop] = cost0;
d**e
发帖数: 6098
7
来自主题: JobHunting版 - C的argc问题
我用的是ubuntu, gcc 4.2.4
------------------------
#include
int main(int argc, char * argv[])
{
printf("argc = %d\n", argc);
return 0;
}
------------------------
比如我传入一个参数,输出是 2,正确
但如果我传入的是"*"符号,结果是 13 ... 这是为什么呢?
似乎只要有"*",argc至少就是13了。
谢谢
$ ./test 12 * 3
argc = 15
$ ./test *
argc = 13
$./test 12
argc = 2
c***2
发帖数: 838
8
Here's the full file. You may compile and run: any case I missed?
==================================================================
/* wild.c
*/
#include
#include
#include
#define STR_SIZE 256
//===========================================================
int matchndots(const char *text, const char *dstr, int len)
{
while(len&&*text&&*dstr&&(*text==*dstr || *dstr=='.')){
text++;
dstr++;
len--;
}

if(!len)
return 1;
... 阅读全帖
o*****e
发帖数: 99
9
来自主题: JobHunting版 - 攒人品 报BB面经
Sigh...,
Why are so many misleading information here.
You guys really should try it out before post here.
char* c;
is not a "declare" in this case, it is a definition.
since the stack will allocate 4bytes(8bytes for 64bit machine) for it.
And you don't have to initialize this pointer if you decide to give it a
value in foo().
#include
#include
void foo(char** a){
static char A[20] = "hello world";
*a = A;
}
int main(){
char* c;
foo(&c);
printf("%s",c);
... 阅读全帖
b********n
发帖数: 609
10
来自主题: JobHunting版 - 面经兼求祝福
he must misread the output since stdio is shared among child processes...
8 seems right to me.
c***2
发帖数: 838
11
来自主题: JobHunting版 - 请问这样写程序错了吗?
I also use VC compiler, but only in cmd, not in dde.
Did you include the headers?
stdio/string
i******s
发帖数: 301
12
来自主题: 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***2
发帖数: 838
13
来自主题: 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******p
发帖数: 166
14
来自主题: JobHunting版 - 经典面试题
http://en.wikipedia.org/wiki/Multiplication_algorithm
an implementation of the "lattice mul":
#include
#include
void box(const char* v1, const char* v2)
{
size_t len1 = strlen(v1);
size_t len2 = strlen(v2);
size_t lres = len1 + len2;
char res[lres+1];
size_t i1, i2;
for (i1 = 0; i1 < lres; i1 ++)
res[i1] = 0;
res[lres] = '\0';
for (i1 = 0; i1 < len1; i1 ++)
for (i2 = 0; i2 < len2; i2 ++)
{
int int1 = (int) (v1[len1-1-i1... 阅读全帖
J******8
发帖数: 132
15
来自主题: JobHunting版 - Facebook phone screen
The question is not hard. But I missed two key points.
The details below.
==========================================================
/*
example
char *words[] = {"foo", "bar", "baz", NULL};
setup(words);
1) First step:
isMember("foo") -> true
isMember("garply") -> false
2) Second step:
isMember("f.o") -> true
isMember("..") -> false
*/
#include
#include
#include
char *words[] = {"foo", "bar", "baz", "caz","cat",NULL};
int num=0;
void print_words(void)
{
int i=0... 阅读全帖
f****g
发帖数: 313
16
来自主题: JobHunting版 - A simple interview question
The following is my code :S
#include
#include
#include
#define MAXLEN 5
char* countCharInStr(const char* s,
unsigned int len)
{
char *pFast, *pSlow;
unsigned char num[MAXLEN] = {0};
unsigned int count = 0;
unsigned int resC = 0;

if( 0 == len || NULL == s)
{
return NULL;
}

char *res = (char*)malloc(sizeof(char)*len);
if( NULL == res)
{
return NULL;
}

pSlow = s;
pFa... 阅读全帖
n********y
发帖数: 66
17
欢迎拍砖,就用了一个 256 字节的table来检查一个数字是否出现过
#include
#include
#include
#include
char* nextpointer(char* str, int strlen)
{
char *p = str;
char *pend = str + strlen;
char count[256];
memset(count, 0, sizeof(count));
while (p < pend)
{
++count[*p];
if (count[*p] > 1)
{
return p;
}
++p;
}
return p;
}
void longestsingle(char* str, int strlen)
{
char *pstart = str;
char *pend... 阅读全帖
f*******n
发帖数: 8
18
来自主题: 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)
     {
   &... 阅读全帖
z*d
发帖数: 18
19
来自主题: JobHunting版 - 问一道 ama的除法题
#include
#include
int divide (int x, int y) {
int m, n, sign_m, sign_n;
int j, k, l;
if (y == 0) {
printf("Dvided by 0 error! \n");
assert(0);
}
if (x < 0) {
m = -x;
sign_m = -1;
}
else {
m = x;
sign_m = 1;
}
if (y < 0) {
n = -y;
sign_n = -1;
}
else {
n = y;
sign_n = 1;
}
j = 0;
l = m;
while(1) {
k = (j+l)/2;
if (k*n > m)... 阅读全帖
z*d
发帖数: 18
20
来自主题: JobHunting版 - 问一道 ama的除法题
How about this one? Any suggestions or corrections will be highly
appreciated.
#include
#include
int divide (int x, int y) {
long long dividend, divisor, sign_dividend, sign_divisor;
long long low, middle, high;
int quotient;
/* check whether divisor is 0 */
if (y == 0) {
printf("Dvided by 0 error! \n");
assert(0);
}

/* unsign both dividend and divisor and mark the signess */
if (x < 0) {
dividend = -x;
sign... 阅读全帖
l*********r
发帖数: 674
21
来自主题: JobHunting版 - Bloomber 面试题
第二个:神奇啊,我在cygwin下面的gcc居然编译成功而且运行也没问题。
#include
int main() {
char *str = "Hello World"; /* 1 */
memset(str, 'a', 100); /* 2 */
printf("%s", str);
return 0;
}
但是在i has 1337 code的coding panel输入就报错:
Run Status: Compile Error
s*****y
发帖数: 897
22
来自主题: JobHunting版 - 问个算法题5
看了各位大牛的实现后,写了个O(nlgk)的C实现
#include
#include
//assume input array size < 20
#define ARRAY_SIZE 20
/*P[k] —stores the position of the
predecessor of X[k] in the longest increasing subsequence ending at X[k].*/
static int P[ARRAY_SIZE];
/*M[j] stores the position k of the smallest value X[k]
such that there is an increasing subsequence of length j ending
at X[k] on the range k ≤ i (note we have j ≤ k ≤ i here)*/
static int M[ARRAY_SIZE];
void Find_Lis(int input[], int size, int... 阅读全帖
h*****g
发帖数: 944
23
借Google大侠的名字发个贴
我下面的c program为何不work?
#include
#include
int main(){
const char * cstr="Hello";
int array_size=0;
while(cstr!=NULL){

printf("%c\t",*cstr);
cstr++;
}
return 0;
}
菜鸟一个,谢谢了
c*********t
发帖数: 2921
24
来自主题: JobHunting版 - 问道C内存的题?
写了一个很简单的程序,在linux下试了试,是segmentation fault, 程序退出。并且用
valgrind verify了一下,是同样的结果。
#include
// del_stack_variable.c
// this program tries to delete a variable in a function
//
int main()
{
int j=10;
printf("the addr of j =%p, j = %d\n", &j, j);
printf("delete j \n");
free(&j); //this will cause segmentation fault
printf("after that\n");
printf("the addr of j =%p, j = %d\n", &j, j);
return 0;
}
gcc -o del_stack del_stack_variable.c
$ ./del_stack
the addr of ... 阅读全帖
p*********b
发帖数: 47
25
来自主题: JobHunting版 - 收到G家拒信,发面经
9,Unix file path化简,写code
例如 /a/./b/../../c/ ,化简为 /c/
用stack或者d-queue,有些细节需要考虑,例如 /..//.. 还是输出 /
这题不需要额外的数据结构,从尾部向前反过来做。
我之前写的代码
/* The algorithm runs in O(n) time and O(n) space
* It works backwards so that no stack or other buffer is
* needed to skip the parents folder if a "../" is encountered.
* "//" is preserved as is, and "/./" is changed to "/
* I was able to do a inplace version, but string cut and cat are
* essentially array element revomal, which will make running time
* O(n^2).
*
* My ... 阅读全帖
k***t
发帖数: 276
26
来自主题: JobHunting版 - 收到G家拒信,发面经
我也试一个,in-placed的。先把要删掉的mark成'*'。第二次遍历时再删掉。
#include
// /a/b/./../../c/d => /c/d
void path (char *in) {
char *p, *q;
bool isSlash;
char inv='*';
if (!in || !*in) return;
int i=0;
// parse
p=in; isSlash=false;
while (*p) {
/* first slash */
if (*p=='/') {
if (!isSlash) {
isSlash=true;
} else {
*p=inv;
}
p++; continue;
}
isSlash=false;
... 阅读全帖
b*******8
发帖数: 37364
27
来自主题: JobHunting版 - 求顺时针打印矩阵code
以前我写的,测试过:
#include
void main()
{
const int size=7;
int a[size][size];
int x,y,val=0;
for(x=0;x for(y=x;y for(y=x;y for(y=size-x-1;y>x;y--) a[size-x-1][y]=++val;
for(y=size-x-1;y>x;y--) a[y][x]=++val;
}
if (size%2) a[size/2][size/2]=++val;
for(x=0;x for(y=0;y printf("\n");
}
getchar();
}
o****d
发帖数: 2835
28
来自主题: 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... 阅读全帖
c****p
发帖数: 6474
29
来自主题: JobHunting版 - 这道题,怎么做呀?
假设A/B:
#include
int main()
{
int A, B, tB, tA, Q, R;
scanf("%d", &A);
scanf("%d", &B);
tB = B;
if (A {
Q = 0;
R = A;
}
else
{
while(A>tB)
{
tB<<=1;
}
tB >>= 1;
tA = A;
Q = 0;
while(tB >= B)
{
Q <<= 1;
if(tA >= tB)
{
tA -= tB;
Q += 1;
}
tB >>= 1;
}
R =... 阅读全帖
S**I
发帖数: 15689
30
来自主题: JobHunting版 - Microsoft interview question
#include
#include
void reverseStr(char str[], int begin, int end){

char temp;

while(end > begin){
temp = str[begin];
str[begin++] = str[end];
str[end--] = temp;
}
}
void reverseWord(char str[]){

int begin = 0, end = 0, length = (int) strlen(str) - 1;

reverseStr(str, begin, length);

while(end < length){

if(str[end] != ' '){

begin = end;

while(end... 阅读全帖
k***d
发帖数: 4
31
来自主题: JobHunting版 - 一家游戏公司的新鲜面经
#include
#define N 3
#define MAXN 10
void gen(int level){
int i, j;
static int dice[MAXN]; /* global array also works */

if(level <= N){
for(i = 1; i <= 6; i++){
dice[level] = i;
gen(level+1);
}
}else{
for(j = 1; j <= N; j++){
printf("%d", dice[j]);
}
printf("\n");
}
}
int main(){
gen(1);
return 0;
}
j***y
发帖数: 2074
32
在193~194页,书里谈到了一个下面的问题:
---
Signedness of char. In C and C++, it is not specified whether the char data type is signed or unsigned. This can lead to trouble when combining chars and ints, such as in code that calls the int-valued routine getchar(). If you say
? char c; /* should be int */
? c = getchar();
the value of c will be between 0 and 255 if char is unsigned, and between -128 and 127 if char is signed, for the almost universal configuration of 8-bit characters on a two's complement machin... 阅读全帖
m*********2
发帖数: 701
33
wow, good point.
yea, i think what the author saying is:
EOF == -1
0xFF == 255.
that's why you want to use int.
it's large enough to differentiate whether it's -1 or 255.
the short answer is:
getchar() returns int.
and c is int.
so, you are comparing integers, NOT char.

data type is signed or unsigned. This can lead to trouble when combining
chars and ints, such as in code that calls the int-valued routine
getchar(). If you say
between -128 and 127 if char is signed, for the almost universal
co... 阅读全帖
f****4
发帖数: 1359
34
来自主题: 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... 阅读全帖
s*****y
发帖数: 897
35
来自主题: JobHunting版 - 问个编程题
#include
#include
#include
#include
using namespace std;
vector FindSet(int target, const vector &input)
{
vector rtn;
int dp[10][100]; //dp table to save the result
bool used[10][100];
for (int i = 0; i <= target; i++) {
dp[0][i] = 0;
if (i <= 1) {
dp[1][i] = i;
used[1][i] = true;
} else {
dp[1][i] = INT_MAX;
used[1][i] = false;
}
}
for ... 阅读全帖
f****4
发帖数: 1359
36
来自主题: JobHunting版 - virtual destructor (C++)问题
virtual destructor就是干这个活的
下次,你可以自己写个代码检验一下
#include
class A{
public:
virtual ~A()
{
printf("A::~A\n");
}
};
class B: public A
{
public:
virtual ~B()
{
printf("B::~B\n");
}
};
int main()
{
A *p = new B();
delete p;
printf("=====\n");
B b;
A &a = b;
}
l*********8
发帖数: 4642
37
#include
#include
int main(int argc, char * argv[])
{
char * s[4] = {"000", "Fizz", "Buzz", "FizzBuzz"};
char buf[4];
for (int x=1; x<=100; x++) {
s[0] = itoa(x, buf, 10);
printf("%s\n", s[ !(x%3) + 2*!(x%5) ] );
}
return 0;
}
a***r
发帖数: 93
38
来自主题: JobHunting版 - G onsite面经
My solution is about the same as bitwise, it uses an array:
#include
#include
void add_range(int D[], int l, int a, int b) {
if(b for (int i=a; i<=b; i++) {
D[i%l]+=1;
}
}
bool is_overlap_array(int a,int b,int c,int d) {
int D[7]= {};
int l=sizeof(D)/sizeof(D[0]);
add_range(D,l,a,b);
add_range(D,l,c,d);
for (int i=0; i if (D[i]>1) { return true; }
}
return false;
}
void main(int argc, const char *... 阅读全帖
s****j
发帖数: 67
39
来自主题: JobHunting版 - 几道老题 的解答
平面最近点对那题,提供一种方法供参考,该方法看似复杂度o(n^2),但实际使用中几
乎是o(n)(不算nlgn排序的预处理)。主要是利用了三角形两边之差小于第三边的性质
,大大减枝。另外该方法实现简单,比传统o(nlgn)的实现简单很多也直观很多。
代码如下:
#include
#include
#include
#include
#include
using namespace std;
const int maxn=100005;
const double eps=1e-8;
struct Point
{
double x,y,dis;
} p[maxn];
int n;
bool cmpp(const Point &p1,const Point &p2)
{
return p1.dis }
void solve()
{
sort(p,p+n,cmpp);
double ans=1e18,now;
int i,j;
for (i=0;i阅读全帖
f****4
发帖数: 1359
40
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;
... 阅读全帖
S**I
发帖数: 15689
41
来自主题: JobHunting版 - HELP: C programming question
#include
#include
#include
struct StringCount{
char s[100];
int count;
};
int compare(const void * a, const void * b){
return ((*(struct StringCount *)a).count < (*(struct StringCount *)b).count);
}
void printSortedWords(char * s, FILE * file){
int size = strlen(s);
struct StringCount sc[100];
int num = 0;
char newstr[100];
int i = 0;
for(; i < size; i++){
if(s[i] != ' '){
int j = 0;
while(s[i]... 阅读全帖
m****t
发帖数: 555
42
我的程序运行结果是正确的。
这个c程序输出就是 pylhssrtnaatareyeopsfefasmeietawodny
#include
#include
#include
int main()
{
char *A="paypalisthefastersaferwaytosendmoney";
char B[4][10]={};
int ROW = 4;
int COL;
int N;
COL = strlen(A)/ROW;
if (strlen(A)%ROW) {
COL++;
}
N= strlen(A);
int pos=0;
int c, xb=0;
int x, y;
for (c=0; c<= ROW+COL-2; c++) {
x = xb;
y = c-x;
while (x >= 0 && y <= COL-1 && pos B[x][y] = A[pos];
pos++;
x--;
y++;
}
if (xb < ROW... 阅读全帖
s*****y
发帖数: 897
43
写了个简单的,解你这个例子的,
#include
#include
#include
#include
#include
#include
using namespace std;
int matrix[4][4] = {{5,0,0,0},
{9,6,0,0},
{4,6,8,0},
{0,7,1,5}};
int getMax()
{
vector dp_table(4, 0);
for (int i = 0; i < 4; i++)
dp_table[i] = matrix[3][i];
for (int j = 2; j>= 0; j--) {
for (int i = 0; i <= j; i++)
dp_table[i] = max(matr... 阅读全帖
h********r
发帖数: 51
44
来自主题: JobHunting版 - Palantir面经
下面这个方法行不行? O(n), 模拟一次买卖的DP解法。大牛们看看对不对?
#include
#include
#include
void dump(int *x, int l) {

int i;
for (i = 0; i < l; ++ i) {

printf("%d ", x[i]);
}
printf("\n");
}
int main() {
int array[] = {3, 6, 8, 4, 5, 7, 9, 13, 15, 10, 6, 2, 7, 11, 8, 6};
int len = sizeof(array)/sizeof(int);
assert (len >= 4);
int *a = (int*)malloc(sizeof(int)*len);
int *b = (int*)malloc(sizeof(int)*len);
int *c = (int*)malloc(sizeof(... 阅读全帖
s******e
发帖数: 108
45
来自主题: JobHunting版 - amazon interview
update:
收到invite onsite 通知,恩,国人还是挺靠谱的。感谢。
感谢NND的推荐,
不过可能被interviewer的国人给干掉了.
1.经典的stream求median,用2个heap解决。
2.给了一段程序,说一下程序的功能,
然后比较c程序和javascrip程序的区别。这个down掉。
#include
#include
#include
#include
#define MAX_RETRY 3
int remoteCall() {
return random() % 2;
}
void action(times) {
printf("calling remote procedure\n");
if (remoteCall()) {
printf("success!\n");
} else {
printf("error occured!\n");
if (times < MAX_RETRY) {
sleep(1);
... 阅读全帖
z****u
发帖数: 104
46
字符串的 permutation 肯定是比较基础的题了,可是自己写了一下发现要 bug free
真心很难啊。调试了半天才 ok,而且程序看起来很臃肿,这要是在白板上铁定写不出
来啊
求大家指点一下该向哪个方向改进?
#include
#include
#include
char* insert(char* dst, int n, char c, int j)
{
/* Insert char c into string dst at location j */
n++;
dst = (char*) realloc(dst, sizeof(char) * n);
while(j < n)
{
char tmp = dst[j];
dst[j] = c;
c = tmp;
j++;
}
return dst;
}
char** permutation_recursive(char* s, int n, in... 阅读全帖
v***a
发帖数: 365
47
来自主题: 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 *... 阅读全帖
k***t
发帖数: 276
48
来自主题: JobHunting版 - 面经
#3 GetMedian() 感觉写得有点复杂,insert()应该还可以简化。
==============
#include
#include
#include
#include
using namespace std;
class Median {
public:
Median() : maxhc(0), minhc(0) {}
~Median() {}
void insert (int v) {
if (maxhc==minhc) {
if (maxh.empty() || maxh.top()>=v)
_enQ(true, v);
else _enQ(false, v);
} else if (maxhc>minhc) {
if (maxh.top()>v) {
_enQ(false, _deQ(true));
_enQ(true, v);
} e... 阅读全帖
c****p
发帖数: 6474
49
来自主题: JobHunting版 - 计算组合数C(m,n)
#include
#include
int C(int m, int n)
{
int *A, *B;
int tmp;
int i, j, k;
if((m <=0 ) || (n <=0 ))
return -1;
if(n == 1)
return m;
if(n == m)
return 1;
if(m - n < n)
n = m - n;

A = malloc(n*sizeof(int)); // A = {m, m - 1, .... m - n + 1}
B = malloc((n-1)*sizeof(int)); // B = {2, 3, ... n}
for(i = 0; i {
A[i] = m - i;
B[i] = i + 2;
}
A[n-1] = m - n + 1;
//i ... 阅读全帖
c*z
发帖数: 86
50
思路同 yezhuen。
#include
#define V(i) (i + 1) /* Expected value of index i */
#define I(x) (x - 1) /* Expected index of value x */
int go(int *x, int n)
{
int i, t;
for (i = 0; i < n; i ++) {
while (x[i] > 0 && x[i] <= n &&
x[i] != V(i) &&
x[I(x[i])] != x[i]) {
t = I(x[i]);
x[i] = x[t];
x[t] = V(t);
}
}
... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)