f******n 发帖数: 53 | 1 来自主题: JobHunting版 - M的面试题 如果是考虑多种组合,显然是DP算法。
构建dp[s.length][s.length]
根据dict设置dp[i][j] = 1 其中s[i...j]可以在dict中找到
dp每行会有多个1
根据ilovethisgame的静态算法如下
显示的a[i] 为每个word的第一个字母位置
void printpath (int a[13])
{
for (int i = 0; i < 13; i++)
printf("%d ", a[i]);
printf("n");
}
bool findAllPath(int dp[13][13], int a[13], int s, int e)
{
if (dp[s][e] == 1)
{
a[s]=1;
printpath(a);
return true;
}
int i = s;
bool found = false;
while (i <= e)
{
if (dp[s][i] == 1)
... 阅读全帖 |
|
m**********g 发帖数: 153 | 2 这个验证通过:
#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;
... 阅读全帖 |
|
b*******n 发帖数: 8 | 3 用讨论里的方法写了java 代码,不用字符串操作。
public class NumsWith5 {
public static void numsWith5(int maxvalue, int partial, int pos, boolean
hasFive) {
if (pos == 0) {
if (partial <= maxvalue && hasFive)
System.out.printf(partial + " ");
return;
}
int num = (int)Math.pow(10, pos - 1);
for (int i = 0; i < 10; i++) {
numsWith5(maxvalue, partial + num * i,
pos - 1, hasFive || i == 5);
}
}
publ... 阅读全帖 |
|
b*******n 发帖数: 8 | 4 用讨论里的方法写了java 代码,不用字符串操作。
public class NumsWith5 {
public static void numsWith5(int maxvalue, int partial, int pos, boolean
hasFive) {
if (pos == 0) {
if (partial <= maxvalue && hasFive)
System.out.printf(partial + " ");
return;
}
int num = (int)Math.pow(10, pos - 1);
for (int i = 0; i < 10; i++) {
numsWith5(maxvalue, partial + num * i,
pos - 1, hasFive || i == 5);
}
}
publ... 阅读全帖 |
|
r****7 发帖数: 2282 | 5 Java里边默认是传引用吧
C++写了个,看了下和你的逻辑应该一样的,不过我觉得这个叫DFS是不是牵强了点儿。
。。
#include
#include
using namespace std;
int size;
void print(vector arr)
{
for (int i = 0; i < arr.size(); i++) {
printf("%c ", arr[i]);
}
printf("n");
}
void backtracking(vector arr, int len)
{
if (len == size) {
print(arr);
return;
}
int currLen = len;
len ++;
if (arr[currLen - 1] == '*') {
arr[currLen - 1] = '0';
backtracking(arr, len);
arr[currLe... 阅读全帖 |
|
t**r 发帖数: 3428 | 6 You have two threads one printing even numbers in order and other odd
numbers. Design an algorithm so that it prints numbers in natural order
#include
#include
pthread_mutex_t mlock=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
int flag=0;
void *EVEN(void *param);
void *ODD(void *param);
int main()
{
pthread_t etid,otid;
pthread_create(&etid,NULL,EVEN,NULL);
pthread_create(&otid,NULL,ODD,NULL);
pthread_exit(NULL);
}
void *EVEN(void *param)
{
int x=0... 阅读全帖 |
|
m*****n 发帖数: 2152 | 7 试着运行一下的你的code,发现不能打印出必要信息。
改了一下,就可以了。
#include
#include
pthread_mutex_t mlock=PTHREAD_MUTEX_INITIALIZER;
int count=0;
void *EVEN(void *param);
void *ODD(void *param);
int main()
{
pthread_t etid,otid;
pthread_create(&etid,NULL,EVEN,NULL);
pthread_create(&otid,NULL,ODD,NULL);
pthread_exit(NULL);
}
void *EVEN(void *param)
{
while(count<20)
{
pthread_mutex_lock(&mlock);
if(count%2==0)
{
printf("%d ", count);
count++;
}
pthread_mutex_un... 阅读全帖 |
|
m*********a 发帖数: 3299 | 8 第一题是用递归么?
#include
#include
typedef struct output_string{
char string[20];
struct output_string *next; }output_string;
void stringCopy(char *str_source,char *str_dest,int no_char){
while (no_char--) *str_dest++=*str_source++;
}
void allString(char *str,output_string *output,int char_position){
output_string *tmp;
while ((*str!='?')&(*str!='\0'))
output->string[char_position++]=*str++;
if (*str=='\0') {output->string[char_position]='\0';return;... 阅读全帖 |
|
v***o 发帖数: 287 | 9 来自主题: JobHunting版 - 电面题一个 void diagonalOrder(int matrix[ROW][COL])
{
// There will be ROW+COL-1 lines in the output
for (int line=1; line<=(ROW + COL -1); line++)
{
/* Get column index of the first element in this line of output.
The index is 0 for first ROW lines and line - ROW for remaining
lines */
int start_col = max(0, line-ROW);
/* Get count of elements in this line. The count of elements is
equal to minimum of line number, COL-start_col and ROW... 阅读全帖 |
|
m****s 发帖数: 131 | 10 来自主题: JobHunting版 - 电面题一个 void printDigMatrix(int **m, int r, int c){
for(int i=0; i
int j;
if(i>=r){
j = i-r+1;
}
else j=0;
while(j<=i&&j
printf("%d ", m[i-j][j]);
j++;
}
printf("\n");
}
} |
|
v***o 发帖数: 287 | 11 来自主题: JobHunting版 - 电面题一个 void diagonalOrder(int matrix[ROW][COL])
{
// There will be ROW+COL-1 lines in the output
for (int line=1; line<=(ROW + COL -1); line++)
{
/* Get column index of the first element in this line of output.
The index is 0 for first ROW lines and line - ROW for remaining
lines */
int start_col = max(0, line-ROW);
/* Get count of elements in this line. The count of elements is
equal to minimum of line number, COL-start_col and ROW... 阅读全帖 |
|
m****s 发帖数: 131 | 12 来自主题: JobHunting版 - 电面题一个 void printDigMatrix(int **m, int r, int c){
for(int i=0; i
int j;
if(i>=r){
j = i-r+1;
}
else j=0;
while(j<=i&&j
printf("%d ", m[i-j][j]);
j++;
}
printf("\n");
}
} |
|
m****s 发帖数: 131 | 13 听朋友说过这个题目:
如果你把一个语句注释掉,printf出来结果不一样了。。什么原因。。当然,注释掉的
部分和printf的内容无关 |
|
Q*****a 发帖数: 33 | 14 string LongestMostTwoSubString(string s) {
printf("s.size():%dn", s.size());
// assume char in s are all ASCII, otherwise we can unsigned char and
256 slots
vector charCounts(128, 0);
int maxStart = 0;
int maxLen = 0;
int start = 0, end = 0;
for (;;) {
while (end < s.size() && ++charCounts[s[end]] <= 2) ++end;
int len = end - start;
if (len > maxLen) {
maxStart = start;
maxLen = len;
}
if (end == s.s... 阅读全帖 |
|
Q*****a 发帖数: 33 | 15 string LongestMostTwoSubString(string s) {
printf("s.size():%dn", s.size());
// assume char in s are all ASCII, otherwise we can unsigned char and
256 slots
vector charCounts(128, 0);
int maxStart = 0;
int maxLen = 0;
int start = 0, end = 0;
for (;;) {
while (end < s.size() && ++charCounts[s[end]] <= 2) ++end;
int len = end - start;
if (len > maxLen) {
maxStart = start;
maxLen = len;
}
if (end == s.s... 阅读全帖 |
|
c***r 发帖数: 280 | 16 关于第二题blocking queue,网上找到个code觉得不错,1个mutex, 1个condition
variable.
原贴在这:
http://www.fgdsb.com/2015/01/03/provider--consumer/
template class block_queue {
condition_variable _cond;
mutex _mutex;
std::queue _data;
public:
void push(const T& value) {
unique_lock l(_mutex);
_data.push(value);
l.unlock();
_cond.notify_one();
}
T pop() {
unique_lock l(_mutex);
_cond.wait(l, [&](){ return !_data.em... 阅读全帖 |
|
w**********o 发帖数: 140 | 17 String str = "aabbcc";
for(int i=0; i
System.out.printf("%s", str.charAt(i));
}
System.out.println()
for(int i=0; i<=str.length()-1; ++i){
System.out.printf("%s", str.charAt(i));
}
有差別麼? 沒有.
請先理解做題思路. |
|
y****9 发帖数: 252 | 18 来,走一个
int x = 10;
while (x --> 0) {
printf("%d ", x);
}
你是否听说过 --> 这个运算符
然后 你又是否听说过 <---- 这个运算符
int x = 10;
while (0 <---- x) {
printf("%d ", x);
}
我这都是捣乱的 |
|
c**1 发帖数: 71 | 19 $ cat x.cc; g++ x.cc; ./a.out
#include
void f(char* str) {
int left = 0;
char* p;
for (p = str; *p; ++p) {
if (*p == '(') {
++left;
} else if (left != 0) {
--left;
} else {
*p = ' ';
}
}
for (--p; p >= str && left != 0; --p) {
if (*p == '(') {
*p = ' ';
--left;
}
}
}
int main() {
char buf[] = ")()))))))(()()(())())()))))))))";
printf("%s\n", buf);
f(buf);
printf("%s\n", buf);
return 0;
}
)()))))))(()()(())())()... 阅读全帖 |
|
g*****e 发帖数: 110 | 20 很简单的c代码,求助为什么运行结果不对?
输入 abc回车,然后要输出abc
目前只输出b
#include
int main() {
char str[15];
int i = 0;
printf("input a string:\n");
while (getchar() != '\n')
{
str[i] = getchar();
printf("%c", str[i]);
i++;
}
return 0;
} |
|
t********5 发帖数: 522 | 21 我居然还能看得懂c 哈哈哈哈哈
大概就是这个意思了 不知道能不能编译。。。
while (getchar() != '\n')
{
str[i] = getchar();
printf("%c", str[i]);
i++;
}
=>
while (True)
{
char ch = getchar()
if(ch == '\n') {
break;
}
str[i] = ch;
printf("%c", str[i]);
i++;
} |
|
t*******r 发帖数: 22634 | 22 if (obj.age <= 28)
printf("WARNING: Net is too small! Please increase net size.");
else
printf("ERROR: CPU time slice running out! Please choose sub-optimal."); |
|
t*******r 发帖数: 22634 | 23 if (obj.age <= 28)
printf("WARNING: Net is too small! Please increase net size.");
else
printf("ERROR: CPU time slice running out! Please choose sub-optimal."); |
|
|
W**********r 发帖数: 8927 | 25 In memory of the ...
printf("Hello World\n");
printf("Bye bye ~~\n"); |
|
xt 发帖数: 17532 | 26 我搞到了一个小程序:
#include
#include
#include
#include
#include
int main( int argc, char** argv)
{
char buf[80] = {0};
int myfile;
pid_t pid;
struct psinfo psi;
if ( argc<2 ){
printf("Usage: %s PID\n", argv[0]);
return 1;
}
pid=atoi(argv[1]);
sprintf( buf, "/proc/%d/psinfo", pid);
myfile = open( buf, O_RDONLY );
if (myfile < 0)
perror( "open" );
if ( read( myfile, & psi, sizeof(psi) ) < 0 )
perror( "read" );
close( myfile );
printf( "%s |
|
O***O 发帖数: 264 | 27 好吧,我把源代码给你贴这儿了,老刑该找我麻烦了:)
#include
#include
//函数声明
bool Win(int [4][10]);
bool Analyze(int [],bool);
int main(int argc, char* argv[])
{
//定义手中的牌
int allPai[4][10]={
{6,1,4,1},//万
{3,1,1,1},//筒
{0},//索
{5,2,3}//字
};
if (Win (allPai))
printf("Hu!\n");
else
printf("Not Hu!\n");
return 0;
}
//判断是否胡牌的函数
bool Win (int allPai[4][10])
{
int jiangPos;//“将”的... 阅读全帖 |
|
p****s 发帖数: 32405 | 28 bool afei = TRUE;
if (!(!(!afei)) == TRUE)
printf("Xiaoji is correct!\n");
else
printf("Xiaoji zhege xxx\n");
请验证. |
|
g****s 发帖数: 306 | 29 ERROR: (s)printf(): Incorrect argument to type %s. (arg 0)
program: adm/daemons/channeld.c, object: adm/daemons/channeld, file: /adm/daemons/channeld.c:112
ERROR: (s)printf(): Incorrect argument to type %s. (arg 0)
program: adm/daemons/channeld.c, object: adm/daemons/channeld, file: /adm/daemons/channeld.c:112
if( !arg ) arg = "...";
if( emote ) {
// Support of old behavier of intermud emote.
if( channels[verb]["intermud_emote"] ) arg = who + " " + |
|
n*******k 发帖数: 100 | 30 printf("%cn", buffer[place]);
应该是
printf("%c\n", buffer[place]);
buffer[79]=(char)(0) 这个应该没有任何作用啊。你除余的时候,把第80个字符
buffer[79]又给改写了。 place应该设成78,倒数第二个字符。
另外,在C里面,ascii的0,'\0',NULL不是同一个意思么? |
|
z******d 发帖数: 302 | 31 int main(int argc,char *argv[])
{
if(这是一个坑)
{
printf("这个坑挖得够深了");
printf("谢谢光临!");
exit(0);
}
else
{
cout<<"欢迎当小白!"<
exit(0);
}
} |
|
b*******l 发帖数: 1737 | 32 Thanks, easywebx. That's it. I was wondering why the default php.ini
commented out all these extensions.
The function is available now. However, I still cannot connect to the
database. Below is my code:
ini_set('display_errors', true);
error_reporting(E_ALL);
$mysqli = new mysqli("localhost", "john", "abc123", "test");
If (1 > 0) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
printf("Host information: %s\n", mysqli_get_host_info($mysqli));
mysqli |
|
w**********l 发帖数: 8501 | 33 就是个非常简单的计算code, 我放printf本来是为了debug的,结果一放printf马上就
run了。搞得我找不出问题 |
|
F******k 发帖数: 197 | 34 Don't deal with 汉字 in lex before and don't know the exact error you got,
compiling or runtime? But here are some thoughts you may take:
1) Does your lex return wide char string? I mean yytext from from lex.
2) You need printf(L"%s") or printf("ls") to print out wide char string
3) Could be some typo here [[\u3000]* (duplicated [[, you don't want strings like [[[ or [[\u3000\u3000[[[[, but I may be wrong here) |
|
j**********i 发帖数: 3758 | 35 http://baike.baidu.com/view/3691487.htm
#include
#include
#include
#include
#include
#include
int main()
{
float m_fTimeElapsed;
int j;
long double x;
LARGE_INTEGER m_timeFreq;
LARGE_INTEGER m_timeStart;
LARGE_INTEGER m_timeStop;
QueryPerformanceFrequency(&m_timeFreq);
QueryPerformanceCounter(&m_timeStart);
for(int i=0;i<4000000000;i++){
j=i+1;
// x=(long double)sin(i*3... 阅读全帖 |
|
c*********e 发帖数: 16335 | 36 当初我用c++自己from scratch手写排序的code,没有抄网上一個字,排序非常的慢。后
来发现,居然是printf()太多,让程序变慢了。把printf()删掉后,程序就非常快了。
non |
|
i**p 发帖数: 902 | 37 哪位大牛解释一下这个#1
--------------------output--------
Insert
hashCode(clover)
hashCode(clover2)
equals(clover2, clover)
Search
hashCode(aiko) // #1 why is it used in find("k1")?
Dog@4
hashCode(clover)
equals(clover, clover2)
Dog key
----------code
import java.util.ArrayList;
import java.util.List;
import java.util.*;
class Dog {
public Dog(String n) {
name = n;
}
public String name;
public boolean equals(Object o) {
System.out.printf("equals(%s, %s)n", name, ((Dog) o).... 阅读全帖 |
|
i**p 发帖数: 902 | 38 哪位大牛解释一下这个#1
--------------------output--------
Insert
hashCode(clover)
hashCode(clover2)
equals(clover2, clover)
Search
hashCode(aiko) // #1 why is it used in find("k1")?
Dog@4
hashCode(clover)
equals(clover, clover2)
Dog key
----------code
import java.util.ArrayList;
import java.util.List;
import java.util.*;
class Dog {
public Dog(String n) {
name = n;
}
public String name;
public boolean equals(Object o) {
System.out.printf("equals(%s, %s)n", name, ((Dog) o).... 阅读全帖 |
|
Y**G 发帖数: 1089 | 39 对于volatile成员,java只是标注一下
比如
public class App {
volatile int a = 1;
...
}
用javap看byte code,
...
volatile int a;
flags: ACC_VOLATILE
我以前猜java会对于volatile产生优化的代码,比如
public class App {
volatile int a = 1;
public static void main(String[] args) {
App app = new App();
int x = app.a;
System.out.printf("%d%n", x);
x = app.a;
System.out.printf("%d%n", x);
}
}
但是实际上,在第二次 "x = app.a"的时候,java产... 阅读全帖 |
|
n****i 发帖数: 1024 | 40 主要是第一个comment那边,输入yes or no 然后取出第一个char赋值给tryAgain,我
用了tryAgain = scan.findInLine(".").charAt(0); 跑下来是出现一个框,根本就没
有赋值给tryAgain,求大神帮忙指点下,我哪里出错了。另外我用switch没问题吧
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
private static String[] choices = { "Rock", "Paper", "Scissors" };
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
Random rGen = new Random();
String name;
int playerChoiceNum;
String playerC... 阅读全帖 |
|
S*A 发帖数: 7142 | 41 应该就是那个传进来的string 没有 null terminate 吧。
你可以在 segfault forloop 前面 printf 看一下那个 arg,
如果 printf 也 segfault 就是 caller 的问题了。 |
|
S*A 发帖数: 7142 | 42 不会用 csh.
bash 里面大概是
varname=`sqlplus -silent id/password@oracel_instance <
printf("%s",$1;}'
"%s",$1;}' `
printf( |
|
f**k 发帖数: 906 | 43
struct stat st;
while (stat(cPath,&st) != 0) {
sleep(0.2);
}
printf(" %s is present\n", cPath);
int tempfd = open(cPath, O_RDONLY, 0666);
printf("tempfd %d", tempfd);
这个open的语句停在那里了。就是说文件有了,打开不了。 |
|
d**d 发帖数: 389 | 44 【 以下文字转载自 Programming 讨论区 】
发信人: dxxd (东邪西毒), 信区: Programming
标 题: 请教一个linux下的POSIX timer的问题。
发信站: BBS 未名空间站 (Fri May 13 17:06:15 2011, 美东)
我用linux下面的POSIX timer, timer_create(),timer_settime(),
为什么在调用了timer_settime()以后,立马就有一个time-out callback? 然后再每过
5秒后有一个time out?
难道不是我调用timer_settime()以后,timer开始计时, 等到5秒以后再出现第一
time out callback 吗?
非常感谢!
代码如下:
#include
#include
#include
#include
#include
void
handle (sigval_t v)
{
tim... 阅读全帖 |
|
h****a 发帖数: 70 | 45 Given the following code:
#include
int main(void)
{
int tmp;
tmp = fork();
if(tmp == 0)
{
printf("Hello ")
sleep(1)
}
else if(tmp > 0)
{
printf("World, ")
sleep(1)
}
print "Bye bye"
}
Assuming the call to fork doesn't fail, which of the following is true (zero
or more answers may be correct):
a The execution of this is deterministic.
b The output will be: 'Hello World, Bye bye'
c This ca... 阅读全帖 |
|
f******y 发帖数: 2971 | 46 main(){char q=34, n=10,*a="main() {char q=34,n=10,*a=%c%s%c; printf(a,q,a,q,
n);}%c";printf(a,q,a,q,n);} |
|
c*******h 发帖数: 1096 | 47 main(a){printf(a,34,a="main(a){printf(a,34,a=%c%s%c,34);}",34);}
你的多了些空格
q, |
|
w*s 发帖数: 7227 | 48 i have a c code, it has
printf ("info: ...");
printf ("error: ...");
now this doesn't work for me,
my_exec | grep error | tee my_log.txt
error message is not piped into the log file.
any suggestions ? |
|
R******d 发帖数: 1436 | 49 我捣鼓了一下,awk基本可以实现,加上sed,输出效果就更好一点:
awk -F"\t" '{for(x=1;x<=NF;x++){if($x!=""){sum[x]++;data[NR,x]=$x}}}END{for(
i
=1;i<=NR;i++){for(j=1;j<=NF;j++){if(sum[j]
printf("%s",data[i,j])}else{printf("%st",data[i,j])}};print ""}}' file|sed '
s/\t\{2,\}/\t/g' |
|