问网易笔试的一道题

yxriyin
博客专家认证
2011-10-11 08:42:30
见过难的,没见过这么难的,网易真变态。
其中有一道题我要问问大家,给个思路,最好能有代码。
题意很简单,写一个程序,打印出以下的序列。
(a),(b),(c),(d),(e)........(z)
(a,b),(a,c),(a,d),(a,e)......(a,z),(b,c),(b,d).....(b,z),(c,d).....(y,z)
(a,b,c),(a,b,d)....(a,b,z),(a,c,d)....(x,y,z)
....
(a,b,c,d,.....x,y,z)
规律很简单,但要我打印出来,尼玛想破脑袋也没想出好方法,让我用26层循环吗?
...全文
2093 61 打赏 收藏 转发到动态 举报
写回复
用AI写文章
61 条回复
切换为时间正序
请发表友善的回复…
发表回复
Guimashisqll 2013-11-29
  • 打赏
  • 举报
回复
引用 45 楼 zju3020912063 的回复:
难道没人觉得这是个27进制数序列么。
的确是的,但是你能按照人家的顺序打印么?2的26次方已经不小了,好像存储在调整位置也是一个问题啊。
陳約翰 2011-12-29
  • 打赏
  • 举报
回复
用disjoint set來做

union-find
Victor YU223 2011-10-14
  • 打赏
  • 举报
回复
58楼卖萌了
smartgotodo 2011-10-13
  • 打赏
  • 举报
回复
来个直接打印的先

// 163.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

static CONST TCHAR Letters[] = TEXT("abcdefghijklmnopqrstuvwxyz");
static CONST INT MAX_COUNT = 26;

static VOID PrintSubRange(INT Begin, INT Length)
{
_tprintf_s(TEXT("("));
INT End = Begin + Length;
for (INT ii=Begin; ii<End; ++ii)
{
_tprintf_s(TEXT("%c,"), Letters[ii]);
}
}

static VOID PrintSubRangeAndAllTail(INT Begin, INT Length)
{
INT End = Begin + Length;
for (INT ii=End; ii<MAX_COUNT; ++ii)
{
PrintSubRange(Begin, Length);

if (ii != MAX_COUNT -1 )
{
_tprintf_s(TEXT("%c), "), Letters[ii]);
}
else
{
_tprintf_s(TEXT("%c) "), Letters[ii]);
}
}
}

static VOID PrintAll()
{
for (INT Length=0; Length<=MAX_COUNT; ++Length)
{
for (INT Begin= 0; Begin < MAX_COUNT - Length; ++Begin)
{
PrintSubRangeAndAllTail(Begin, Length);
if (Length == 0)
{
break;
}
}
_tprintf_s(TEXT("\r\n"));
}
}

int _tmain(int argc, _TCHAR* argv[])
{
PrintAll();
return 0;
}

yefeng60 2011-10-13
  • 打赏
  • 举报
回复
33行代码搞定!
yefeng60 2011-10-13
  • 打赏
  • 举报
回复
PHP代码实现如下:

<?php
$characters=array();
for($i=97;$i<=122;$i++)
$characters[]=chr($i);

$lists=array();

for($k=0;$k<=25;$k++){
$sub_lists=array();

for($i=0;$i<=25-$k;$i++){
$units=array();
if($k==0){
$units[]='('.$characters[$i].')';
}else{
for($j=$i+1;$j<=25-($k-1);$j++){
$items=array();
$items[]=$characters[$i];
for($l=0;$l<=$k-1;$l++){
$items[]=$characters[$j+$l];
}
$units[]='('.implode(',',$items).')';
}
}
$sub_lists[]=implode(',',$units);
}
$lists[]=implode(',',$sub_lists);
}

for($m=0;$m<=25;$m++)
echo $lists[$m].'<br>';

?>
gotope 2011-10-13
  • 打赏
  • 举报
回复
看看,标记
xz0404 2011-10-12
  • 打赏
  • 举报
回复
printf("%s,%c",;lpstr,ch[i])

改 printf("%s%c",lpstr,ch[i]);
xz0404 2011-10-12
  • 打赏
  • 举报
回复
char ch[26] = "abcdefghijklmnopqrstuvwxyz";

void paint(char* lpstr,int k)
{
for(int i=k,i<26,i++)
{
printf("(");
printf("%s,%c",;lpstr,ch[i])
printf(")");
}
if(k++<26)
{
lpstr[k*2] = ',';
lpstr[k*2+1] = ch[k];
lpstr[k*2+2] = 0;
paint(lpstr,k);
}
};
xuleicsu 2011-10-12
  • 打赏
  • 举报
回复
这个和状态机有么关系?
不就是求集合子集嘛
四不舍五不入 2011-10-12
  • 打赏
  • 举报
回复

char cs[27];
int csLen=1;
void printCharsTuple(char from,int len)
{
int temp=csLen;
if(len==1)
{
cs[csLen]=from;
csLen++;
cs[csLen]='\0';
cout<<cs<<"),";
}
else
{
cs[csLen]=from;
csLen++;
for(char c=from+1;c<='z';c++)
printCharsTuple(c,len-1);
}
csLen=temp;
}

int main()
{
cs[0]='(';
for(int len=1;len<=26;len++)
{
for(char c='a';c<='z';c++)
printCharsTuple(c,len);
cout<<endl;
}
}
四不舍五不入 2011-10-12
  • 打赏
  • 举报
回复
char cs[27];
int csLen=1;
void printCharsTuple(char from,int len)
{
int temp=csLen;
if(len==1)
{
cs[csLen]=from;
csLen++;
cs[csLen]='\0';
cout<<cs<<"),";
}
else
{
cs[csLen]=from;
csLen++;
for(char c=from+1;c<='z';c++)
printCharsTuple(c,len-1);
}
csLen=temp;
}

int main()
{
cs[0]='(';
for(int len=1;len<=26;len++)
{
for(char c='a';c<='z';c++)
printCharsTuple(c,len);
cout<<endl;
}
}
土豆小张 2011-10-12
  • 打赏
  • 举报
回复
发现一个bug:

list1[j] = chr(ord(list1[j-1]) + 1)

要改成
土豆小张 2011-10-12
  • 打赏
  • 举报
回复
注意调整python的代码格式
土豆小张 2011-10-12
  • 打赏
  • 举报
回复
不用递归也可以,已经测试过了,可以工作,而且肯定正确

#!/usr/bin/python

import sys

def print_list(list):
print "".join(list)

# print the max value for specific index
def max_val_for_index(index):
max_25 = ord('z')
return max_25 - 25 + index

# initialize the list
list1 = []
for i in range(26):
list1.append('')

# begin, set the last value is 'a'
max = 25
list1[max] = 'a'
print_list(list1)

while True:

# loop each value from max index to min index in the list
# to find the first one that is not the max value in the position
# then add that value by 1, then set later ones minus 1 with left one
for i in range(26):
index = 25 - i

# deal with empty value
if list1[index] == "":
idx_val = 0
else:
idx_val = ord(list1[index])

idx_max = max_val_for_index(index)

if idx_val + 1 <= idx_max:

# if the one in index 0 need to be added by 1, then all strings were listed
if index == 0 and list1[index] == 'a':
sys.exit()

if list1[index] == "":
list1[index] = 'a'
else:
list1[index] = chr(ord(list1[index]) + 1)

for j in range(index+1, 26):
list1[j] = chr(ord(list1[index]) + 1)

break

print_list(list1)
四不舍五不入 2011-10-12
  • 打赏
  • 举报
回复
这么多不靠谱的人,贴了代码的没一个是对的
xz0404 2011-10-12
  • 打赏
  • 举报
回复
char ch[28] = " abcdefghijklmnopqrstuvwxyz";
#define item_count 0;
void inc(int *lpstr,int j = 25,int updata = 1)
{
if(updata)
{
if(lpstr[j] ++ > j)
{
inc(lpstr,j-1,1);
lpstr[j] = lpstr[j-1] + 1;
}
else
{
inc(lpstr,j-1,0);
}
printf(",");
}
else if(lpstr[j-1]>0)
{

inc(lpstr,j-1,0);
printf(",");
}
printf("%c",ch[lpstr[j]]);
};



int _tmain(int argc, _TCHAR* argv[])
{
int lpstr[27];
for(int i = 0; i< 27; i++)lpstr[i] = 0;

while(lpstr[item_count] < 2)
{
printf("(");
inc(lpstr);
printf(")");
}
return 0;
}
zsjbsg 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 45 楼 zju3020912063 的回复:]
难道没人觉得这是个27进制数序列么。
[/Quote]

额。。。
蠢蠢的程序园 2011-10-12
  • 打赏
  • 举报
回复
没难度。。asc码两层循环就可以。。
a是96不知道有没有记错。。错了也没关系查查BA。然后++就b~直到z。
不过他应该叫别人写流程和约束,不应该叫别人写代码,傻B,才会去花时间去背asc2码表,我只是用多了,记住 了几个。。
第一层就是1~26.
第二层也是1~26.
关键的判断是第二层包括号是要根椐第一层循环,数量去包(记住到z结束带上(“)”)号就行了。)。
zju3020912063 2011-10-12
  • 打赏
  • 举报
回复
难道没人觉得这是个27进制数序列么。
加载更多回复(41)

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧