佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

楼主: martinng

高手进来,看看一个interview的问题。做对有赏~

[复制链接]
发表于 24-10-2018 09:20 PM | 显示全部楼层
martinng 发表于 24-10-2018 01:57 PM
yan13的code是用最少行,这个是肯定了。只是他的if (。。。)还可以再更简化。给点贴士:需要用到一点数学。。。

好奇, 可以怎么改法?

  1. <?
  2. $name = array();
  3. $name[] = "Martin";
  4. $name[] = "Jenny";
  5. $name[] = "Wendy";
  6. $name[] = "Siti";
  7. $name[] = "Thomas";
  8. $name[] = "Siva";
  9. $name[] = "Mun Hui";
  10. $name[] = "Richard";
  11. $name[] = "Kumar";
  12. $name[] = "Isah";
  13. $name[] = "Samson";
  14. $name[] = "JiaJia";
  15. $gender = array();
  16. $gender[] = "M";
  17. $gender[] = "F";
  18. $gender[] = "F";
  19. $gender[] = "F";
  20. $gender[] = "M";
  21. $gender[] = "M";
  22. $gender[] = "F";
  23. $gender[] = "M";
  24. $gender[] = "M";
  25. $gender[] = "F";
  26. $gender[] = "M";
  27. $gender[] = "F";
  28. for ($i = 0;$i < count($name) * 2;$i++) {
  29.   if (($gender[$i % count($name) ] === "M" && $i < count($name)) || ($gender[$i % count($name) ] === "F" && $i > count($name))) {
  30.     echo $name[$i % count($name) ] . "-" . $gender[$i % count($name) ] . "<br>";
  31.   }
  32. }
  33. ?>
复制代码


回复

使用道具 举报


ADVERTISEMENT

发表于 24-10-2018 10:08 PM | 显示全部楼层
martinng 发表于 24-10-2018 01:57 PM
yan13的code是用最少行,这个是肯定了。只是他的if (。。。)还可以再更简化。给点贴士:需要用到一点数学。。。

有趣.我東西趕完了再試試.
在想能不能把If的4個條件換成3各.還有有點在意為甚麼容許bool.
回复

使用道具 举报

发表于 25-10-2018 01:08 AM | 显示全部楼层
本帖最后由 褐眼睛 于 25-10-2018 01:15 AM 编辑

不过好像违规了,我加多了一个Array.

  1. const
  2.   StudentCount=12;
  3.   StudentList:array [0..StudentCount-1,0..1] of string=(('Martin','M'),('Jenny','F'),('Wendy','F'),('Siti','F'),('Thomas','M'),('Siva','M'),('Mun Hui','F'),('Richard','M'),('Kumar','M'),('Isah','F'),('Samson','M'),('JiaJia','F'));
  4.   Gender:array [0..1] of string[1]=('M','F');
  5. var
  6.   Count:Integer;
  7. begin
  8.   for Count:=0 to (StudentCount*2)-1 do
  9.     if (StudentList[Count mod StudentCount,1]=Gender[Count div StudentCount]) then
  10.       WriteLn(StudentList[Count mod StudentCount,0] + ' - ' + StudentList[Count mod StudentCount,1]);
  11. end.
复制代码

  1. $fpc -vw main.pas
  2. Free Pascal Compiler version 3.0.2 [2017/03/27] for x86_64
  3. Copyright (c) 1993-2017 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling main.pas
  6. Linking main
  7. 12 lines compiled, 0.1 sec
  8. /usr/bin/ld: warning: link.res contains output sections; did you forget -T?
  9. $main
  10. Martin - M
  11. Thomas - M
  12. Siva - M
  13. Richard - M
  14. Kumar - M
  15. Samson - M
  16. Jenny - F
  17. Wendy - F
  18. Siti - F
  19. Mun Hui - F
  20. Isah - F
  21. JiaJia - F
复制代码
回复

使用道具 举报

发表于 25-10-2018 01:13 AM | 显示全部楼层
本帖最后由 褐眼睛 于 25-10-2018 01:19 AM 编辑

这个就没有违规,不是只是用于这个StudentList. (找漏洞!)

  1. const
  2.   StudentCount=12;
  3.   StudentList:array [0..StudentCount-1,0..1] of string=(('Martin','M'),('Jenny','F'),('Wendy','F'),('Siti','F'),('Thomas','M'),('Siva','M'),('Mun Hui','F'),('Richard','M'),('Kumar','M'),('Isah','F'),('Samson','M'),('JiaJia','F'));
  4. var
  5.   Count:Integer;
  6. begin
  7.   for Count:=0 to (StudentCount*2)-1 do
  8.     if (StudentList[Count mod StudentCount,1]=StudentList[Count div StudentCount,1]) then
  9.       WriteLn(StudentList[Count mod StudentCount,0] + ' - ' + StudentList[Count mod StudentCount,1]);
  10. end.

复制代码

  1. $fpc -vw main.pas
  2. Free Pascal Compiler version 3.0.2 [2017/03/27] for x86_64
  3. Copyright (c) 1993-2017 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling main.pas
  6. Linking main
  7. 11 lines compiled, 0.1 sec
  8. /usr/bin/ld: warning: link.res contains output sections; did you forget -T?
  9. $main
  10. Martin - M
  11. Thomas - M
  12. Siva - M
  13. Richard - M
  14. Kumar - M
  15. Samson - M
  16. Jenny - F
  17. Wendy - F
  18. Siti - F
  19. Mun Hui - F
  20. Isah - F
  21. JiaJia - F
复制代码


http://tpcg.io/O9HSiE
回复

使用道具 举报

发表于 25-10-2018 01:44 AM | 显示全部楼层
改了一改:

  1. type
  2.   StudentType=record
  3.     Name:string;
  4.     Sex:string[1];
  5.   end;

  6. const
  7.   StudentCount=12;

  8.   StudentList:array [0..StudentCount-1] of StudentType=((Name:'Martin';Sex:'M'),(Name:'Jenny';Sex:'F'),(Name:'Wendy';Sex:'F'),(Name:'Siti';Sex:'F'),(Name:'Thomas';Sex:'M'),(Name:'Siva';Sex:'M'),(Name:'Mun Hui';Sex:'F'),(Name:'Richard';Sex:'M'),(Name:'Kumar';Sex:'M'),(Name:'Isah';Sex:'F'),(Name:'Samson';Sex:'M'),(Name:'JiaJia';Sex:'F'));
  9. var
  10.   Count:Integer;

  11. begin
  12.   for Count:=0 to (StudentCount*2)-1 do
  13.     if StudentList[Count mod StudentCount].Sex=StudentList[Count div StudentCount].Sex then
  14.       WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
  15. end.
复制代码


http://tpcg.io/czHVzG
回复

使用道具 举报

 楼主| 发表于 25-10-2018 01:46 AM | 显示全部楼层
褐眼睛 发表于 25-10-2018 01:13 AM
这个就没有违规,不是只是用于这个StudentList. (找漏洞!)





http://tpcg.io/O9HSiE

但是studentList从class变成二维array了。虽然规则没有讲到不能更改studentList这个data structure,但如果能保留问题的一切资料,会更好~

btw,我由衷佩服你的创造力 [em:1:]
回复

使用道具 举报

Follow Us
 楼主| 发表于 25-10-2018 01:59 AM | 显示全部楼层
褐眼睛 发表于 25-10-2018 01:44 AM
改了一改:



http://tpcg.io/czHVzG

有漏洞啊~
只有在第一个是M和第二个是F的情况下,你的code才works。哈哈哈哈,你蛮有鬼才的~

点评

碰巧的!哈哈。  发表于 25-10-2018 05:46 AM
回复

使用道具 举报

 楼主| 发表于 25-10-2018 02:04 AM | 显示全部楼层
星期五晚上我会开估,大家加油!!!
回复

使用道具 举报


ADVERTISEMENT

发表于 25-10-2018 04:28 AM | 显示全部楼层
martinng 发表于 25-10-2018 02:04 AM
星期五晚上我会开估,大家加油!!!


看来我攻克了难题!  耶,不过会很长吗?

  1. type
  2.   StudentType=record
  3.     Name:string;
  4.     Sex:string[1];
  5.   end;

  6. const
  7.   StudentCount=12;

  8.   StudentList:array [0..StudentCount-1] of StudentType=((Name:'Martin';Sex:'M'),(Name:'Jenny';Sex:'F'),(Name:'Wendy';Sex:'F'),(Name:'Siti';Sex:'F'),(Name:'Thomas';Sex:'M'),(Name:'Siva';Sex:'M'),(Name:'Mun Hui';Sex:'F'),(Name:'Richard';Sex:'M'),(Name:'Kumar';Sex:'M'),(Name:'Isah';Sex:'F'),(Name:'Samson';Sex:'M'),(Name:'JiaJia';Sex:'F'));
  9. var
  10.   Count:Integer;
  11. {
  12.     0 XOR 0 = 1
  13.     1 XOR 1 = 0
  14.    
  15.     1st loop scan for M (XOR 1)
  16.     2nd loop scan for F (XOR 0) and ignore M
  17. }
  18. begin
  19.   for Count:=0 to (StudentCount*2)-1 do
  20.     if (StudentList[((Count div StudentCount) xor 0)*(Count mod StudentCount)].Sex='F') or (StudentList[((Count div StudentCount) xor 1)*(Count mod StudentCount)].Sex='M') and not (((Count div StudentCount) xor 1)=0) then
  21.       WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
  22. end.
复制代码

  1. $fpc -vw main.pas
  2. Free Pascal Compiler version 3.0.2 [2017/03/27] for x86_64
  3. Copyright (c) 1993-2017 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling main.pas
  6. Linking main
  7. 27 lines compiled, 0.1 sec
  8. /usr/bin/ld: warning: link.res contains output sections; did you forget -T?
  9. $main
  10. Martin - M
  11. Thomas - M
  12. Siva - M
  13. Richard - M
  14. Kumar - M
  15. Samson - M
  16. Jenny - F
  17. Wendy - F
  18. Siti - F
  19. Mun Hui - F
  20. Isah - F
  21. JiaJia - F
复制代码


http://tpcg.io/PgTmrZ
回复

使用道具 举报

发表于 25-10-2018 05:57 AM | 显示全部楼层
本帖最后由 褐眼睛 于 25-10-2018 06:00 AM 编辑

看来还是很长(而且找到漏洞:第一个不能是F),先让我解释一下它的原理:

当for loop 0..11时,count div 12 = 0
当for loop 12..23时,count div 12 = 1

当0..11,   0 XOR 1=1,1用来乘以 count mod 12 = 0..11 (用来扫描 M)
当12..23, 1 XOR 1=0,0用来乘以 count mod 12 = 0..0

当0..11,   0 XOR 0=0,0用来乘以 count mod 12 = 0..0
当12..23, 1 XOR 0=1,1用来乘以 count mod 12 = 0..11(用来扫描F)

不过有个缺点,必须避免重复的 count mod 12=0

评分

参与人数 1人气 +5 收起 理由
martinng + 5 谢谢分享

查看全部评分

回复

使用道具 举报

发表于 25-10-2018 06:55 AM | 显示全部楼层
没有bug了,可是却还是4个条件condition.

  1. type
  2.   StudentType=record
  3.     Name:string;
  4.     Sex:string[1];
  5.   end;

  6. const
  7.   StudentCount=12;

  8.   StudentList:array [0..StudentCount-1] of StudentType=((Name:'Martin';Sex:'M'),(Name:'Jenny';Sex:'F'),(Name:'Wendy';Sex:'F'),(Name:'Siti';Sex:'F'),(Name:'Thomas';Sex:'M'),(Name:'Siva';Sex:'M'),(Name:'Mun Hui';Sex:'F'),(Name:'Richard';Sex:'M'),(Name:'Kumar';Sex:'M'),(Name:'Isah';Sex:'F'),(Name:'Samson';Sex:'M'),(Name:'JiaJia';Sex:'F'));
  9. var
  10.   Count:Integer;

  11. begin
  12.   for Count:=0 to (StudentCount*2)-1 do

  13.     if (((StudentList[((Count div StudentCount) xor 0)*((Count mod StudentCount))].Sex='F') and not (((Count div StudentCount) xor 0)=0)) or ((StudentList[((Count div StudentCount) xor 1)*((Count mod StudentCount))].Sex='M')) and not (((Count div StudentCount) xor 1)=0))  then
  14.       WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);

  15. end.
复制代码
回复

使用道具 举报

发表于 25-10-2018 07:38 AM | 显示全部楼层
如果被容许用CHR(),那简单得多:

由M到F
  1.   for Count:=0 to (StudentCount*2)-1 do
  2.     if StudentList[Count mod StudentCount].Sex=Chr(77-(Count div StudentCount)*7) then
  3.        WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
复制代码


由F到M
  1.   for Count:=0 to (StudentCount*2)-1 do
  2.     if StudentList[Count mod StudentCount].Sex=Chr(70+(Count div StudentCount)*7) then
  3.        WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
复制代码
回复

使用道具 举报

发表于 25-10-2018 07:52 AM | 显示全部楼层
这个和原来yan13的差不多,只是改了一下:

  1.   for Count:=0 to (StudentCount*2)-1 do
  2.     if ((StudentList[Count mod StudentCount].Sex='F') and (Count div StudentCount=1)) or ((StudentList[Count mod StudentCount].Sex='M') and (Count div StudentCount=0)) then
  3.        WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
复制代码
回复

使用道具 举报

发表于 25-10-2018 08:12 AM | 显示全部楼层
很少看到那麼熱鬧的
回复

使用道具 举报

发表于 25-10-2018 08:34 AM | 显示全部楼层
今天好心情,让我也顺手示范一下如果将12个性别装入一个Integer里面:

010110110001 (从右边看起)

FMFMMFMMFFFM (从右边看起)

M=1, F=0  

只能装16个性别,因为是16-bit. 示范而已,不实用的。

  1. type
  2.   StudentType=record
  3.     Name:string;
  4.     Sex:string[1];
  5.   end;

  6. const
  7.   StudentCount=12;

  8.   StudentList:array [0..StudentCount-1] of StudentType=((Name:'Martin';Sex:'M'),(Name:'Jenny';Sex:'F'),(Name:'Wendy';Sex:'F'),(Name:'Siti';Sex:'F'),(Name:'Thomas';Sex:'M'),(Name:'Siva';Sex:'M'),(Name:'Mun Hui';Sex:'F'),(Name:'Richard';Sex:'M'),(Name:'Kumar';Sex:'M'),(Name:'Isah';Sex:'F'),(Name:'Samson';Sex:'M'),(Name:'JiaJia';Sex:'F'));
  9. var
  10.   Count:Integer;
  11.   Map:Integer;

  12. begin
  13.   for Count:=0 to (StudentCount)-1 do
  14.     Map:=Map or ((((Ord(StudentList[Count mod StudentCount].Sex[1]) and 8) shr 3) shl Count));
  15.   
  16.   WriteLn(Map);
  17. end.
复制代码


  1. $fpc -vw main.pas$fpc -
  2. Free Pascal Compiler version 3.0.2 [2017/03/27] for x86_64
  3. Copyright (c) 1993-2017 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling main.pas
  6. main.pas(17,10) Warning: Variable "Map" does not seem to be initialized
  7. Linking main
  8. 26 lines compiled, 0.1 sec
  9. 1 warning(s) issued
  10. /usr/bin/ld: warning: link.res contains output sections; did you forget -T?
  11. $main
  12. 1457
复制代码


Screenshot 2018-10-25 at 8.29.21 AM.png


回复

使用道具 举报

发表于 25-10-2018 08:35 AM | 显示全部楼层
yan13 发表于 25-10-2018 08:12 AM
很少看到那麼熱鬧的

你想到破解的方法了么?
回复

使用道具 举报


ADVERTISEMENT

发表于 25-10-2018 10:37 AM | 显示全部楼层
褐眼睛 发表于 25-10-2018 08:35 AM
你想到破解的方法了么?

還在忙,我有一點想法.
  1. (<div class="blockcode"><blockquote>(studentList[i % n].sex) == 'M'
复制代码

&& i < n) || ((studentList[i % n].sex) == 'F' && i >= n)
第一組的條件,剛剛好是第二組的反面.就是說利用
  1. i < n
复制代码
true of false, 來改變
(studentList[i % n].sex) == 'M'
在 0 to n-1 的時候, 是true. sex == 'M' 也是 true. true x true = true.
在 n to 2n-1 時, 是 false. 在sex == 'M' 是true時, false x true = false, not printing. sex == 'M' 是false時, false x false (not false) = true.
可能要用NOR gate, 或 Not (condition) 之類的.

评分

参与人数 1积分 +100 收起 理由
褐眼睛 + 100 谢谢分享

查看全部评分

回复

使用道具 举报

发表于 25-10-2018 11:05 AM | 显示全部楼层
yan13 发表于 25-10-2018 10:37 AM
還在忙,我有一點想法.
&& i < n) || ((studentList.sex) == 'F' && i >= n)
第一組的條件,剛剛好是第二組的反面.就是說利用
true of false, 來改變
(studentList.sex) == 'M'
在 0 to n-1 的時 ...

你说的大概和我之前想的一样,会吧?

  1.   for Count:=0 to (StudentCount*2)-1 do
  2.     if (StudentList[((Count div StudentCount) xor 0)*(Count mod StudentCount)].Sex='F') or (StudentList[((Count div StudentCount) xor 1)*(Count mod StudentCount)].Sex='M') then
  3.       WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
复制代码


跟着这个逻辑走,结果是这样的:

  1. 0 - 0
  2. 0 - 1
  3. 0 - 2
  4. 0 - 3
  5. 0 - 4
  6. 0 - 5
  7. 0 - 6
  8. 0 - 7
  9. 0 - 8
  10. 0 - 9
  11. 0 - 10
  12. 0 - 11
  13. 0 - 0
  14. 1 - 0
  15. 2 - 0
  16. 3 - 0
  17. 4 - 0
  18. 5 - 0
  19. 6 - 0
  20. 7 - 0
  21. 8 - 0
  22. 9 - 0
  23. 10 - 0
  24. 11 - 0
复制代码


看起来已经很PERFECT了,问题在于Index 0不能解决,还必需AND其他条件哦。
回复

使用道具 举报

发表于 25-10-2018 11:18 AM | 显示全部楼层
yan13 发表于 25-10-2018 10:37 AM
還在忙,我有一點想法.
&& i < n) || ((studentList.sex) == 'F' && i >= n)
第一組的條件,剛剛好是第二組的反面.就是說利用
true of false, 來改變
(studentList.sex) == 'M'
在 0 to n-1 的時 ...



用yan13的方法,我们终于成功啦!!!

  1. type
  2.   Student=record
  3.     Name:string;
  4.     Sex:string[1];
  5.   end;

  6. const
  7.   StudentCount=12;

  8.   StudentList:array [0..StudentCount-1] of Student=((Name:'Martin';Sex:'M'),(Name:'Jenny';Sex:'F'),(Name:'Wendy';Sex:'F'),(Name:'Siti';Sex:'F'),(Name:'Thomas';Sex:'M'),(Name:'Siva';Sex:'M'),(Name:'Mun Hui';Sex:'F'),(Name:'Richard';Sex:'M'),(Name:'Kumar';Sex:'M'),(Name:'Isah';Sex:'F'),(Name:'Samson';Sex:'M'),(Name:'JiaJia';Sex:'F'));
  9. var
  10.   Count:Integer;

  11. begin
  12.   for Count:=0 to (StudentCount*2)-1 do
  13.     if not (Count<StudentCount) xor (StudentList[Count mod StudentCount].Sex='M') then
  14.       WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
  15. end.
复制代码


$fpc -vw main.pas$fpc -
Free Pascal Compiler version 3.0.2 [2017/03/27] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling main.pas
Linking main
20 lines compiled, 0.1 sec
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
$main
Martin - M
Thomas - M
Siva - M
Richard - M
Kumar - M
Samson - M
Jenny - F
Wendy - F
Siti - F
Mun Hui - F
Isah - F
JiaJia - F


试跑?http://tpcg.io/ymSbS8
回复

使用道具 举报

发表于 25-10-2018 11:27 AM | 显示全部楼层
正是NOT和XOR起了作用。

0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

我也不知道是怎样攻克了这个难题,有点莫名其妙。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 20-4-2024 09:46 PM , Processed in 0.058979 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表