|
高手进来,看看一个interview的问题。做对有赏~
[复制链接]
|
|
发表于 24-10-2018 09:20 PM
|
显示全部楼层
好奇, 可以怎么改法?
- <?
- $name = array();
- $name[] = "Martin";
- $name[] = "Jenny";
- $name[] = "Wendy";
- $name[] = "Siti";
- $name[] = "Thomas";
- $name[] = "Siva";
- $name[] = "Mun Hui";
- $name[] = "Richard";
- $name[] = "Kumar";
- $name[] = "Isah";
- $name[] = "Samson";
- $name[] = "JiaJia";
- $gender = array();
- $gender[] = "M";
- $gender[] = "F";
- $gender[] = "F";
- $gender[] = "F";
- $gender[] = "M";
- $gender[] = "M";
- $gender[] = "F";
- $gender[] = "M";
- $gender[] = "M";
- $gender[] = "F";
- $gender[] = "M";
- $gender[] = "F";
- for ($i = 0;$i < count($name) * 2;$i++) {
- if (($gender[$i % count($name) ] === "M" && $i < count($name)) || ($gender[$i % count($name) ] === "F" && $i > count($name))) {
- echo $name[$i % count($name) ] . "-" . $gender[$i % count($name) ] . "<br>";
- }
- }
- ?>
复制代码
|
|
|
|
|
|
|
|
发表于 24-10-2018 10:08 PM
|
显示全部楼层
有趣.我東西趕完了再試試.
在想能不能把If的4個條件換成3各.還有有點在意為甚麼容許bool.
|
|
|
|
|
|
|
|
发表于 25-10-2018 01:08 AM
|
显示全部楼层
本帖最后由 褐眼睛 于 25-10-2018 01:15 AM 编辑
不过好像违规了,我加多了一个Array.
- const
- StudentCount=12;
- 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'));
- Gender:array [0..1] of string[1]=('M','F');
- var
- Count:Integer;
- begin
- for Count:=0 to (StudentCount*2)-1 do
- if (StudentList[Count mod StudentCount,1]=Gender[Count div StudentCount]) then
- WriteLn(StudentList[Count mod StudentCount,0] + ' - ' + StudentList[Count mod StudentCount,1]);
- end.
复制代码
- $fpc -vw main.pas
- 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
- 12 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
复制代码 |
|
|
|
|
|
|
|
发表于 25-10-2018 01:13 AM
|
显示全部楼层
本帖最后由 褐眼睛 于 25-10-2018 01:19 AM 编辑
这个就没有违规,不是只是用于这个StudentList. (找漏洞!)
- const
- StudentCount=12;
- 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'));
- var
- Count:Integer;
- begin
- for Count:=0 to (StudentCount*2)-1 do
- if (StudentList[Count mod StudentCount,1]=StudentList[Count div StudentCount,1]) then
- WriteLn(StudentList[Count mod StudentCount,0] + ' - ' + StudentList[Count mod StudentCount,1]);
- end.
复制代码
- $fpc -vw main.pas
- 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
- 11 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/O9HSiE
|
|
|
|
|
|
|
|
发表于 25-10-2018 01:44 AM
|
显示全部楼层
改了一改:
- type
- StudentType=record
- Name:string;
- Sex:string[1];
- end;
- const
- StudentCount=12;
- 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'));
- var
- Count:Integer;
- begin
- for Count:=0 to (StudentCount*2)-1 do
- if StudentList[Count mod StudentCount].Sex=StudentList[Count div StudentCount].Sex then
- WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
- end.
复制代码
http://tpcg.io/czHVzG
|
|
|
|
|
|
|
|
楼主 |
发表于 25-10-2018 01:46 AM
|
显示全部楼层
但是studentList从class变成二维array了。虽然规则没有讲到不能更改studentList这个data structure,但如果能保留问题的一切资料,会更好~
btw,我由衷佩服你的创造力 [em:1:]
|
|
|
|
|
|
|
|
楼主 |
发表于 25-10-2018 01:59 AM
|
显示全部楼层
有漏洞啊~
只有在第一个是M和第二个是F的情况下,你的code才works。哈哈哈哈,你蛮有鬼才的~
|
|
|
|
|
|
|
|
楼主 |
发表于 25-10-2018 02:04 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 25-10-2018 04:28 AM
|
显示全部楼层
看来我攻克了难题! 耶,不过会很长吗?
- type
- StudentType=record
- Name:string;
- Sex:string[1];
- end;
- const
- StudentCount=12;
- 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'));
- var
- Count:Integer;
- {
- 0 XOR 0 = 1
- 1 XOR 1 = 0
-
- 1st loop scan for M (XOR 1)
- 2nd loop scan for F (XOR 0) and ignore M
- }
- begin
- for Count:=0 to (StudentCount*2)-1 do
- 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
- WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
- end.
复制代码
- $fpc -vw main.pas
- 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
- 27 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/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
|
评分
-
查看全部评分
|
|
|
|
|
|
|
发表于 25-10-2018 06:55 AM
|
显示全部楼层
没有bug了,可是却还是4个条件condition.
- type
- StudentType=record
- Name:string;
- Sex:string[1];
- end;
- const
- StudentCount=12;
- 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'));
- var
- Count:Integer;
- begin
- for Count:=0 to (StudentCount*2)-1 do
- 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
- WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
- end.
复制代码 |
|
|
|
|
|
|
|
发表于 25-10-2018 07:38 AM
|
显示全部楼层
如果被容许用CHR(),那简单得多:
由M到F
- for Count:=0 to (StudentCount*2)-1 do
- if StudentList[Count mod StudentCount].Sex=Chr(77-(Count div StudentCount)*7) then
- WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
复制代码
由F到M
- for Count:=0 to (StudentCount*2)-1 do
- if StudentList[Count mod StudentCount].Sex=Chr(70+(Count div StudentCount)*7) then
- WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
复制代码 |
|
|
|
|
|
|
|
发表于 25-10-2018 07:52 AM
|
显示全部楼层
这个和原来yan13的差不多,只是改了一下:
- for Count:=0 to (StudentCount*2)-1 do
- 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
- 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. 示范而已,不实用的。
- type
- StudentType=record
- Name:string;
- Sex:string[1];
- end;
- const
- StudentCount=12;
- 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'));
- var
- Count:Integer;
- Map:Integer;
- begin
- for Count:=0 to (StudentCount)-1 do
- Map:=Map or ((((Ord(StudentList[Count mod StudentCount].Sex[1]) and 8) shr 3) shl Count));
-
- WriteLn(Map);
- 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
- main.pas(17,10) Warning: Variable "Map" does not seem to be initialized
- Linking main
- 26 lines compiled, 0.1 sec
- 1 warning(s) issued
- /usr/bin/ld: warning: link.res contains output sections; did you forget -T?
- $main
- 1457
复制代码
|
|
|
|
|
|
|
|
发表于 25-10-2018 08:35 AM
|
显示全部楼层
你想到破解的方法了么?
|
|
|
|
|
|
|
|
发表于 25-10-2018 10:37 AM
|
显示全部楼层
還在忙,我有一點想法.- (<div class="blockcode"><blockquote>(studentList[i % n].sex) == 'M'
复制代码
&& i < n) || ((studentList[i % n].sex) == 'F' && 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) 之類的.
|
评分
-
查看全部评分
|
|
|
|
|
|
|
发表于 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 的時 ...
你说的大概和我之前想的一样,会吧?
- for Count:=0 to (StudentCount*2)-1 do
- 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
- WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
复制代码
跟着这个逻辑走,结果是这样的:
- 0 - 0
- 0 - 1
- 0 - 2
- 0 - 3
- 0 - 4
- 0 - 5
- 0 - 6
- 0 - 7
- 0 - 8
- 0 - 9
- 0 - 10
- 0 - 11
- 0 - 0
- 1 - 0
- 2 - 0
- 3 - 0
- 4 - 0
- 5 - 0
- 6 - 0
- 7 - 0
- 8 - 0
- 9 - 0
- 10 - 0
- 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的方法,我们终于成功啦!!!
- type
- Student=record
- Name:string;
- Sex:string[1];
- end;
- const
- StudentCount=12;
- 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'));
- var
- Count:Integer;
- begin
- for Count:=0 to (StudentCount*2)-1 do
- if not (Count<StudentCount) xor (StudentList[Count mod StudentCount].Sex='M') then
- WriteLn(StudentList[Count mod StudentCount].Name + ' - ' + StudentList[Count mod StudentCount].Sex);
- 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
我也不知道是怎样攻克了这个难题,有点莫名其妙。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|