您是否曾经想问过编译器“为什么?” 当代码无法正常工作时,我们大多数人都感到沮丧。因此,Mathworks实现了一个很好的小功能why,它回答了这个问题。举一些MATLAB的例子:
why
The programmer suggested it.
why
To fool the tall good and smart system manager.
why(2)
You insisted on it.
why(46)
Bill insisted on it.
您的任务是why用您的语言实现该功能。该函数应在有或没有输入参数的情况下工作(或者使用input 0或-1)。该函数必须被命名why(或者,why(n)用STDIN 编写将导致打印适当的字符串)。
如果未提供任何参数,或者参数为零或负数,则输出字符串应为随机的有效短语。因此,应该有一个功能why,why(),why(0)或why(-1)返回一个随机的句子。
如果给出了输入参数n(函数参数,不是STDIN),则输出应为第n个字符串(定义如下)。因此,why(1)应始终输出(打印/显示)相同的结果。
句子的构成如下(类型1,类型2和特殊)。所有句子都以结尾!。
"Person" "ending" !
"Verb" "adjective" "Person" !
A list of special cases
人员名单:
Stewie
Peter
Homer
The programmer
The system manager
You
结尾列表:
suggested it
insisted on it
did it
动词列表为:
To fool
To satisfy
To please
形容词列表:
the smart
the bald
the tall
the rich
the stupid
特殊情况列表:
How should I know?
Stop asking!
Don't ask!
选择编号的方法是:
句子类型:
Odd number => Type 1
Even number => Type 2
n % 7 = 0 => Type 3 (% is the modulus operator)
名称:使用模数(%)定义第n个名称。
n = 1: 1 % 7 => Stewie
n = 2: 2 % 7 => Peter
...
n = 6: 6 % 7 => You
n = 7: 7 % 7 => How should I know?
n = 11: 11 % 7 => The programmer
n = 14: 14 % 7 => Stop asking!
n = 21: 21 % 7 => Don't ask!
结尾:第n个结尾也使用模数定义。假设结尾(1、2和3)像一样列出(1 2 2 3)。由于数字总是奇数,请使用((n+1)/2 % 4)
n = 1: ((1+1)/2 % 4) => suggested it
n = 3: ((3+1)/2 % 4) => insisted on it
n = 13: ((13+1)/2 % 4) => did it
形容词:使用模数定义第n个形容词。由于数字总是偶数,请使用:(n % 10)/2
n = 2: (2 % 10)/2 => Smart
n = 6: (6 % 10)/2 => The tall
...
动词:第n个动词也使用模数定义。假设动词(1、2和3)的列出方式如下,(1 2 2 3)因为动词的数字始终是偶数,请使用(n % 8) / 2
n = 2: (2 % 8)/2 => To fool
n = 4: (4 % 8)/2 => To satisfy
n = 6: (6 % 8)/2 => To satisfy
n = 8: (8 % 8)/2 => To please
现在,创建随机数的方法应该非常简单,只需选择一个random即可n。
一些例子:
why
You suggested it!
why
To fool the tall Homer!
why
Don't ask!
why(1)
Stewie suggested it!
why(14)
Stop asking!
why(8)
To please the rich Stewie!
适用标准代码高尔夫规则。从挑战发布之日起的一周内将选出一名优胜者。
/2工作。给出分数值。13也应为insisted(14/2 = 7%4 = 3 =坚持的2nd)。
the rich The programmer由于指定原因而遇到类似情况是否重要the?
The和单词To可能应该小写以匹配您的示例……
why还是WHY可以接受的?