找工作=简历+面试 ?No ! 拉勾网携手ThoughtWorks 招代码奇才,不看简历,代码说话。无需干爹,不必爸爸是李刚,you can you up.
从看到消息这一秒开始——你就可以潜心写代码,Java ,C++ , C# , Ruby ,Javascript , Python都可以,鼓励使用单元测试,展示你出色的面向对象能力。
5月4日上午9:00前把作品传回来给我们; 5月4日——7日,我们会夜以继日推敲你的代码; 5月8日——9日,通过代码测试的你,会接到我们温柔的电话; 5月10日——11日,来ThoughtWorks感受一天独特面试。(包括逻辑笔试、结对编程、技术对话)
ThoughtWorks被评为全球最难面试科技公司,就算没吃到猪肉,你也将成为一个见过猪跑的人! “世之奇伟、瑰怪、非常之观,常在于险远,而人之所罕至焉,故非有志者不能至也”,国际环境,风口行业,神一样的队友,不看简历,代码说话。程序猿们!爆发吧!扬眉吐气、验明正身的时刻到了!
查阅题目、上传作品戳这里:http://www.lagou.com/subject/s_thoughtworks.html?joinName=shouye
function tw(x, y, z){
for(var i = 1; i <= 100; i++){
if(~(i + '').indexOf(x)){
console.log('Fizz');
}else{
var temp = ((i % x) ? '' : 'Fizz') + ((i % y) ? '' : 'Buzz') + ((i % z) ? '' : 'Whizz');
(temp) ? console.log(temp) : console.log(i);
}
}
}
这个能算10行以内么?
楼主的语气好像有点传销的风格,哈哈,就和那个电视广告一样,不然楼主先给我来一段, ####12行之内并且只用一个变量的9*9乘法表打印的代码,不限语言!有本事挑战吗? 按照你的说话风格就是,对!!你听得没错就是12行,不要40行,也不要30行,只要12行!!12行啊!!!
for(var i = 1; i <=9; i++){
var a1 = ((i - 8) <= 0) ? '' : ((i - 8) + '*' + i + '=' + (i - 8) * i + ' ');
var a2 = ((i - 7) <= 0) ? '' : ((i - 7) + '*' + i + '=' + (i - 7) * i + ' ');
var a3 = ((i - 6) <= 0) ? '' : ((i - 6) + '*' + i + '=' + (i - 6) * i + ' ');
var a4 = ((i - 5) <= 0) ? '' : ((i - 5) + '*' + i + '=' + (i - 5) * i + ' ');
var a5 = ((i - 4) <= 0) ? '' : ((i - 4) + '*' + i + '=' + (i - 4) * i + ' ');
var a6 = ((i - 3) <= 0) ? '' : ((i - 3) + '*' + i + '=' + (i - 3) * i + ' ');
var a7 = ((i - 2) <= 0) ? '' : ((i - 2) + '*' + i + '=' + (i - 2) * i + ' ');
var a8 = ((i - 1) <= 0) ? '' : ((i - 1) + '*' + i + '=' + (i - 1) * i + ' ');
var a9 = i + '*' + i + '=' + i * i + ' ';
console.log(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9);
}
为了能写清楚点,我把9个变量分开写了,其实只要三行就够了。这个算不算?
s = [] do { s[s.length] = []; do { s[s.length - 1].push(‘’ + s.length + ' x ' + (s[s.length - 1].length + 1) + ' = ' + s.length * (s[s.length - 1].length + 1)); } while (s[s.length - 1].length < s.length); s[s.length - 1] = s[s.length - 1].join(' ') } while (s.length < 9)
console.log(s.join(‘\n’))
========================================
1 x 1 = 1 2 x 1 = 2 2 x 2 = 4 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81
Process finished with exit code 0 是要这样的结果吗?
@struCoder 其实这才是最好的答案
var m9 = [
"1×1=1",
"1×2=2 2×2=4",
"1×3=3 2×3=6 3×3=9",
"1×4=4 2×4=8 3×4=12 4×4=16",
"1×5=5 2×5=10 3×5=15 4×5=20 5×5=25",
"1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36",
"1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49",
"1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64",
"1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81"
]
console.log(m9.join('\n'))
http://bbs.csdn.net/topics/390608383
如果是这里面相似的思路就没必要贴了,这个题目本身漏洞太多了,更精准的限制会使题目更有挑战性。
比如必须要使用循环,对只使用一个变量精确的定义(只能是最基本的int类型,不允许位操作),不允许像我第一次解题的方法(这个方法用C实现不了)。也不允许使用数学方法,必须用1-9这个循环。更不允许使用汇编。这么多限制条件加上去后,这题目才真正有难度了。
###C#解决方案,全文20行,实现方法不超过10行,无if,else,while条件语句。
using System;
using System.Linq;
namespace FizzBuzzWhizz
{
class Program
{
static void Main(string[] args)
{
Output(Console.ReadLine());
Console.ReadLine();
}
static void Output(string recive)
{
int[] Input = recive.Split(',').Select(i => Convert.ToInt32(i)).ToArray();
for (int i = 0; i <= 100; i++)
{
Console.WriteLine(i.ToString().Contains(Input[0].ToString()) ? "Fizz" : (0 != i % Input[0] && 0 != i % Input[1] && 0 != i % Input[2]) ? i.ToString() : (0 == i % Input[0] ? "Fizz" : "") + (0 == i % Input[1] ? "Buzz" : "") + (0 == i % Input[2] ? "Whizz" : ""));
}
}
}
}
php 实现
<?php class FizzBuzzWhizz{ private $_special=array(); private $_words=array(); public function __construct(array $special,array $words){ if(in_array(0,$special)){ exit('特殊数中不能含有0'); } if(count($special)!=count($words)){ exit('特殊数和替代词不对称'); } $this->_special=$special; $this->_words=$words; } public function run($num){ $output=''; for($i=1;$i<=$num;$i++){ $output.=$this->_calculate($i); } echo $output; exit(); } private function _calculate($number){ $str=''; if(strpos($number,$this->_special[0]) > 0){ return $this->_words[0]."\r\n"; } foreach($this->_special as $k=>$v){ if($number%$v === 0){ $str.=$this->_words[$k]; } } return $str==''?$number."\r\n": $str."\r\n"; } }$special=array(3,5,7); $words=array(‘Fizz’,’Buzz’,’Whizz’); $obj=new FizzBuzzWhizz($special,$words); $obj->run(100);
$special=array(3,5,7,9); $words=array(‘Fizz’,’Buzz’,’Whizz’,’haha’); $obj=new FizzBuzzWhizz($special,$words); $obj->run(200); 特殊数可以是任意个,数值范围任意(1~xxx) 计算的函数应该进一步独立(偷个懒),以便修改和替换
(function xx(a) { if (a -1 > 0) xx(a-1); for (var i=1;i<=a;++i) { process.stdout.write (“” + i + “x” + a + “=” + i*a + " "); } process.stdout.write(‘\n’); })(9);
!function (p, m, i, c, l) {
while (i++ < c) {
l(((i + '').indexOf(p[0]) + 1) ? m[0] : (p.map(function (n, j) {
return i % n ? '' : m[j]
}).join('') || i))
}
}([ 3, 5, 7 ], [ 'Fizz', 'Buzz', 'Whizz' ], 0, 100, console.log)
在Terminal下運行 seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}'
@struCoder 这个算法,用矩阵计算要比循环效率高的多,时间复杂度是O(0)。 R语言实现,仅4行:
n<-1:9
m1<-matrix(rep(n,each=9),ncol=length(n))
m2<-matrix(rep(n,9),ncol=length(n))
print(paste(m1,"*",m2,"=",m1*m2))
输出:
[1] "1 * 1 = 1" "1 * 2 = 2" "1 * 3 = 3" "1 * 4 = 4" "1 * 5 = 5" "1 * 6 = 6" "1 * 7 = 7" "1 * 8 = 8" "1 * 9 = 9"
[10] "2 * 1 = 2" "2 * 2 = 4" "2 * 3 = 6" "2 * 4 = 8" "2 * 5 = 10" "2 * 6 = 12" "2 * 7 = 14" "2 * 8 = 16" "2 * 9 = 18"
[19] "3 * 1 = 3" "3 * 2 = 6" "3 * 3 = 9" "3 * 4 = 12" "3 * 5 = 15" "3 * 6 = 18" "3 * 7 = 21" "3 * 8 = 24" "3 * 9 = 27"
[28] "4 * 1 = 4" "4 * 2 = 8" "4 * 3 = 12" "4 * 4 = 16" "4 * 5 = 20" "4 * 6 = 24" "4 * 7 = 28" "4 * 8 = 32" "4 * 9 = 36"
[37] "5 * 1 = 5" "5 * 2 = 10" "5 * 3 = 15" "5 * 4 = 20" "5 * 5 = 25" "5 * 6 = 30" "5 * 7 = 35" "5 * 8 = 40" "5 * 9 = 45"
[46] "6 * 1 = 6" "6 * 2 = 12" "6 * 3 = 18" "6 * 4 = 24" "6 * 5 = 30" "6 * 6 = 36" "6 * 7 = 42" "6 * 8 = 48" "6 * 9 = 54"
[55] "7 * 1 = 7" "7 * 2 = 14" "7 * 3 = 21" "7 * 4 = 28" "7 * 5 = 35" "7 * 6 = 42" "7 * 7 = 49" "7 * 8 = 56" "7 * 9 = 63"
[64] "8 * 1 = 8" "8 * 2 = 16" "8 * 3 = 24" "8 * 4 = 32" "8 * 5 = 40" "8 * 6 = 48" "8 * 7 = 56" "8 * 8 = 64" "8 * 9 = 72"
[73] "9 * 1 = 9" "9 * 2 = 18" "9 * 3 = 27" "9 * 4 = 36" "9 * 5 = 45" "9 * 6 = 54" "9 * 7 = 63" "9 * 8 = 72" "9 * 9 = 81"