《编程之美》2.20程序理解和时间分析
2010年9月1日
1 条评论
最近在看《编程之美》,为找工作面试做准备。该书中2.20程序理解和时间分析一题没有给出解答,所以简单写一下我自己的答案。
题目如下:
阅读以下C#代码,回答问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | using System; using System.Collections.Generic; using System.Text; namespace FindTheNumber { class Program { static void Main(string[] args) { int [] rg = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17, 18,19,20,21,22,23,24,25,26,27,28,29,30,31}; for(Int64 i = 1; i < Int64.MaxValue; i++) { int hit = 0; int hit1 = -1; int hit2 = -1; for (int j = 0; (j < rg.Length) && (hit <= 2); j++) { if((i % rg[j]) != 0) { hit++; if(hit == 1) { hit1 = j; } else if (hit == 2) { hit2 = j; } else break; } } if((hit == 2) && (hit1 + 1 == hit2)) { Console.WriteLine("found {0}", i); } } } } } |
1> 这个程序要找的是符合什么条件的数?
2> 这样的数存在么?符合这一条件的最小的数是什么?
3> 在电脑上运行这一程序,你估计要多长时间才能输出第一个结果?时间精确到分钟(电脑配置:单核CPU2.0GHz,内存和硬盘资源充足)