s*x
3 楼
快看,很快就会被删
S*E
4 楼
Does it require a sim card, or just linked to a cell phone to get data?
c*2
5 楼
Okay. Converted it from "exposed":
T*G
6 楼
congrats, 包子
w*b
7 楼
made in China
波和罩都是
波和罩都是
j*y
9 楼
请问老兄这是哪个公司的面试题啊?
l*o
10 楼
没看到啊
j*y
12 楼
老大,程序看不太懂,能否加点注释啊?
比如当i为0的时候,result[0]=0吧?因为digitsmaps[0]==""。这时候递归调用
doPrintTelephoneWords(phoneNum, 1, result)是要达成一个什么目的呢?
真是看不明白啊。
比如当i为0的时候,result[0]=0吧?因为digitsmaps[0]==""。这时候递归调用
doPrintTelephoneWords(phoneNum, 1, result)是要达成一个什么目的呢?
真是看不明白啊。
E8
13 楼
吃
r*k
14 楼
看着像假的。
j*y
15 楼
又看了一会儿,似乎有些明白,的确是巧妙的递推。但有些疑问:
1. char result[PHONE_NUMBER_LENGTH];应改为char result[PHONE_NUMBER_LENGTH+1]?
2. void printTelephoneWords( int phoneNum[] ){
char result[PHONE_NUMBER_LENGTH];
doPrintTelephoneWords( phoneNum, 0, result );
}似乎应该改为:
void printTelephoneWords( int phoneNum[] ){
char result[PHONE_NUMBER_LENGTH+1]; /* +1 for the string-ending 0x0
character */
for (int i = 0; i < 3; i++) {
doPrintTelephoneWords( phoneNum, 0, result );
}
}
否则出来的似乎只是da、db、dc而已。
我不太懂算法和数据结构,不对之处敬请指教。
1. char result[PHONE_NUMBER_LENGTH];应改为char result[PHONE_NUMBER_LENGTH+1]?
2. void printTelephoneWords( int phoneNum[] ){
char result[PHONE_NUMBER_LENGTH];
doPrintTelephoneWords( phoneNum, 0, result );
}似乎应该改为:
void printTelephoneWords( int phoneNum[] ){
char result[PHONE_NUMBER_LENGTH+1]; /* +1 for the string-ending 0x0
character */
for (int i = 0; i < 3; i++) {
doPrintTelephoneWords( phoneNum, 0, result );
}
}
否则出来的似乎只是da、db、dc而已。
我不太懂算法和数据结构,不对之处敬请指教。
f*0
16 楼
这样也可以吃包子?
c*2
18 楼
1) from facebook
2) result[..+1] you can do that, but not necessary since C does index from 0
, the last idx is alwasy reseved for 0.
char try[2];
Either of these are fine:
try[0]='a';
try[1]='b';
try[2]='\0';
or strcpy(try,"ab");
3) curDigit is better named currentidx (from phoneNum[])
2) result[..+1] you can do that, but not necessary since C does index from 0
, the last idx is alwasy reseved for 0.
char try[2];
Either of these are fine:
try[0]='a';
try[1]='b';
try[2]='\0';
or strcpy(try,"ab");
3) curDigit is better named currentidx (from phoneNum[])
f*g
19 楼
恭喜!恭喜!
p*n
20 楼
是不是假牙掉下来了?
c*2
21 楼
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 doPrintTelephoneWords( char *phoneNum, int len, int curIdx, char *
result)
{
int i;
int mapidx=phoneNum[curIdx]-'0';
if((mapidx>9)||(mapidx<0))
mapidx=10;
if( curIdx == len){
result[len]='\0';
printf("%s\n", result );
return;
}
for( i = 0; i result[curIdx] = digitsmaps[mapidx][i];
doPrintTelephoneWords( phoneNum, len, curIdx + 1, result);
}
}
void printTelephoneWords( char *phoneNum )
{
char *result;
int len=strlen(phoneNum);
if(!phoneNum)
return;
result=(char*)malloc(len);
if(!result)
return;
doPrintTelephoneWords(phoneNum, len, 0, result);
free(result);
}
int main (int argc, char *argv[])
{
int i;
for(i=1;i printf("Mappings of [%s]:\n",argv[i]);
printTelephoneWords(argv[i]);
printf("-------------\n");
}
return 0;
}
==================================================
A sample run:
telwords.exe 23 780
Mappings of [23]:
AD
AE
AF
BD
BE
BF
CD
CE
CF
==================================================
/* 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 doPrintTelephoneWords( char *phoneNum, int len, int curIdx, char *
result)
{
int i;
int mapidx=phoneNum[curIdx]-'0';
if((mapidx>9)||(mapidx<0))
mapidx=10;
if( curIdx == len){
result[len]='\0';
printf("%s\n", result );
return;
}
for( i = 0; i
doPrintTelephoneWords( phoneNum, len, curIdx + 1, result);
}
}
void printTelephoneWords( char *phoneNum )
{
char *result;
int len=strlen(phoneNum);
if(!phoneNum)
return;
result=(char*)malloc(len);
if(!result)
return;
doPrintTelephoneWords(phoneNum, len, 0, result);
free(result);
}
int main (int argc, char *argv[])
{
int i;
for(i=1;i
printTelephoneWords(argv[i]);
printf("-------------\n");
}
return 0;
}
==================================================
A sample run:
telwords.exe 23 780
Mappings of [23]:
AD
AE
AF
BD
BE
BF
CD
CE
CF
u*7
22 楼
pai baozi
j*y
24 楼
又琢磨了半天,你是对的。确实不需要再加一个循环,我搞错了。
就以你原来的程序为例:
doPr(p, 0, res) ->
currDigIdx=0, i=0, res[0]='d', doPr(p, 1, res) ->
currDigIdx=1, i=0, res[1]='a', doPr(p, 2, res) ->
print("da"), return ->
/* recursion-cycle finished for doPr(p, 2, res), but not finished yet for
doPr(p, 1, res) */
currDigIdx=1, i=1, res[1]='b', doPr(p, 2, res) ->
print("db"), return ->
currDigIdx=1, i=2, res[1]='c', doPr(p, 2, res) ->
print("dc"), return ->
/* now doPr(p, 1, res) is finished, return to the for-loop in doPr(p, 0,,
res) */
currDigIdx=0, i=1, res[0]='e', do(p, 1, res) ->
...
这个程序中,递归的流程保证了多重嵌套循环的依次展开,实在是很漂亮的设计。
不过,老大的程序似乎还是有些小问题的,还是拿原来的程序为例:
---
#define PHONE_NUMBER_LENGTH 2
...
if( curDigit == PHONE_NUMBER_LENGTH ){
result[2]='\0';
printf("%s\n", result );
return;
}
...
void printTelephoneWords( int phoneNum[] ){
char result[PHONE_NUMBER_LENGTH];
doPrintTelephoneWords( phoneNum, 0, result );
}
---
char result[2]定义了有效的内存访问空间是result[0]和result[1],result[2]='\0'
实质上已经是越界方位了,很危险,没有core dump只能说是幸运。
所以,我觉得应该改成char result[PHONE_NUMBER_LENGTH + 1];
就以你原来的程序为例:
doPr(p, 0, res) ->
currDigIdx=0, i=0, res[0]='d', doPr(p, 1, res) ->
currDigIdx=1, i=0, res[1]='a', doPr(p, 2, res) ->
print("da"), return ->
/* recursion-cycle finished for doPr(p, 2, res), but not finished yet for
doPr(p, 1, res) */
currDigIdx=1, i=1, res[1]='b', doPr(p, 2, res) ->
print("db"), return ->
currDigIdx=1, i=2, res[1]='c', doPr(p, 2, res) ->
print("dc"), return ->
/* now doPr(p, 1, res) is finished, return to the for-loop in doPr(p, 0,,
res) */
currDigIdx=0, i=1, res[0]='e', do(p, 1, res) ->
...
这个程序中,递归的流程保证了多重嵌套循环的依次展开,实在是很漂亮的设计。
不过,老大的程序似乎还是有些小问题的,还是拿原来的程序为例:
---
#define PHONE_NUMBER_LENGTH 2
...
if( curDigit == PHONE_NUMBER_LENGTH ){
result[2]='\0';
printf("%s\n", result );
return;
}
...
void printTelephoneWords( int phoneNum[] ){
char result[PHONE_NUMBER_LENGTH];
doPrintTelephoneWords( phoneNum, 0, result );
}
---
char result[2]定义了有效的内存访问空间是result[0]和result[1],result[2]='\0'
实质上已经是越界方位了,很危险,没有core dump只能说是幸运。
所以,我觉得应该改成char result[PHONE_NUMBER_LENGTH + 1];
u*h
25 楼
baozi
i*a
27 楼
short legs
e*3
28 楼
明显是假的,真的那可能这么浑圆挺拔?
相关阅读
Proview奇袭美利坚!换手机的感慨兼谈看林书豪比赛时注意到的NBA球场隔壁版信誓旦旦的说ipad屏retina不可能Stove IphonesMxTube录得youtube视频能考出来吗???求问,现在买mac mini合适吗?有人有mac mini 2,1 (2007)的原装碟吗?有啥给猫或者狗玩儿的app?苹果:禁售iPad需考虑社会影响为什么ebay卖的新iphone 4s比官网上unlock的还便宜?【Apple can sell iPads in Shanghai, court says】最早很多人猜是明天宣布新产品[通知] Apple 举办博彩:博彩周四Mar01 AAPL股票涨跌恭喜板斧们上任!!花街发布智冠消息的时候ATT 交了两次押金 至今未退美国记者独家调查富士康内幕iphone4国内多少钱?同学们你们都用什么键盘鼠标?我喜欢苹果产品,反感苹果文化,反感果粉