2008年7月4日星期五
google treasure hunt -----network 网络寻址
这个自己看吧,本人对网络外行:-(
Question
Below is a diagram of a computer network. The nodes are hosts on the network, and the lines between them are links. A packet is sent out from host F with a destination of 74.1.70.230. Which nodes does the packet pass through on its way to the destination? (include start and final node in your answer)
Enter the nodes the packet passes through below
(Note: Answer must start with F, end with the destination node name, and contain only node names.)
google treasure hunt ----zip
问题:
统计下面zip文件夹中满足下面条件的数据,并把它们的乘积返回。所有文件名或文件路径中包含foo,且以.txt结尾的文件中的第4行的总数
所有文件名或文件路径中包含EFG ,且以.js结尾的文件中的第5行的总数
提示:如果相应的行不存在,不统计
Question
Here is a random zip archive for you to download:
GoogleTreasureHunt08_18057390569245304937.zip
Unzip the archive, then process the resulting files to obtain a numeric result. You'll be taking the sum of lines from files matching a certain description, and multiplying those sums together to obtain a final result. Note that files have many different extensions, like '.pdf' and '.js', but all are plain text files containing a small number of lines of text.
Sum of line 4 for all files with path or name containing foo and ending in .txt
Sum of line 5 for all files with path or name containing EFG and ending in .js
Hint: If the requested line does not exist, do not increment the sum.
Multiply all the above sums together and enter the product below.
(Note: Answer must be an exact, decimal representation of the number.)
程序:
static Int64 sum(string folder, string ext, string foundstring, Int64 linenum)
{
Int64 result=0;
string[] files = Directory.GetFiles(folder, ext, SearchOption.AllDirectories);
foreach (string file in files)
{
if (file.ToLower().Contains(foundstring))
using (StreamReader reader = new StreamReader(file))
{
Int64 count = 0;
string line;
while ((line=reader.ReadLine())!=null)
{
count++;
if (count == linenum)
{
Int64 temp = Convert.ToInt64(line);
result+=temp;
break;
}
}
}
}
return result;
}
google tresure hunt 机器人
有一个机器人在36x36的格子上的左上角,移动到右下角,每次只能向下,或向右移动一步。
共有多少条路径?这个问题很简单,组合数学中的问题。
Question
A robot is located at the top-left corner of a 36 x 36 grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
Note: The grid below is 7x3, and is used to illustrate the problem. It is not drawn to scale.
*Image not to scale.
How many possible unique paths are there?
(Note: Answer must be an exact, decimal representation of the number.)
解法:对于n x m的格子,路径数为组合中 n+m-2个数中n-1的组合数,
分析,机器人共需要走n+m-2步到达目的地。这些步中有n-1步是需要向右的。正好是组合数学中的组合定义。
数目可能比较大,会超出整形的最大值。编程的话,需要大数的支持。
有开源的一些项目可以使用。.net的话,可以用IntX codeplex上的项目。
2008年7月3日星期四
google寻宝 google treasure hunt prime 素数问题
题目翻译如下:
找一个最小的素数,使其满足一下条件:
分别可以表示成连续11,37,347,1157个素数之和。
如41是满足下面条件的最小素数:
同时满足连续3个,和6个的素数之和。
11 + 13 + 17 = 41,
2 + 3 + 5 + 7 + 11 + 13 = 41
题目中的数字是动态生成的。这是给我生成的题目。解法都一样。
Question:
Find the smallest number that can be expressed as
the sum of 11 consecutive prime numbers,
the sum of 37 consecutive prime numbers,
the sum of 347 consecutive prime numbers,
the sum of 1157 consecutive prime numbers,
and is itself a prime number.
For example, 41 is the smallest prime number that can be expressed as
the sum of 3 consecutive primes (11 + 13 + 17 = 41) and
the sum of 6 consecutive primes (2 + 3 + 5 + 7 + 11 + 13 = 41).
此题的答案是:9778121
程序:
static bool isPrime(Int64 num)
{
if ((num == 2) || (num == 3))
return true;
Int64 sqrti = (Int64)Math.Sqrt(num) + 1;
for (Int64 i = 2; i < sqrti; i++)
{
if (num % i == 0)
return false;
}
return true;
}
static void Main(string[] args)
{
Int64 result = 0;
bool found = false;
Int64 endprime;
Int64 startprime = 2;
Int64 sumtotal = sum(startprime, 1157, out endprime);
Console.WriteLine(sumtotal);
Console.WriteLine(endprime);
while (!found)
{
while (!isPrime(sumtotal))
{
sumtotal = sumtotal + endprime - startprime;
startprime = findPrimeAfter(startprime);
endprime = findPrimeAfter(endprime);
}
Console.WriteLine("find temp prime sum:" + sumtotal);
if (splitprime(sumtotal, 11) && splitprime(sumtotal, 37) && splitprime(sumtotal, 347))
{
result = sumtotal;
found = true;
}
else
{
sumtotal = sumtotal + endprime - startprime;
startprime = findPrimeAfter(startprime);
endprime = findPrimeAfter(endprime);
}
}
Console.WriteLine("result:" + result);
}
static bool splitprime(Int64 prime, Int64 slicenum)
{
Int64 average = prime / slicenum;
Int64 lowprime = findPrimeBefore(average, slicenum);
// Int64 highprime = findPrimeAfter(average, slicenum);
Int64 startprime = lowprime;
Int64 endprime;
Int64 slicetotalnum = sum(startprime, slicenum, out endprime);
while ((startprime < average) && (slicetotalnum <= prime))
{
if (slicetotalnum == prime)
{
Int64 temp = startprime;
Console.Write(prime+"=");
while (slicenum-- > 0)
{
Console.Write(temp + "+");
temp = findPrimeAfter(temp);
}
Console.WriteLine();
return true;
}
slicetotalnum = slicetotalnum + endprime - startprime;
startprime = findPrimeAfter(startprime);
endprime = findPrimeAfter(endprime);
}
return false;
}
static Int64 sum(Int64 startprimenum, Int64 primenum, out Int64 endprimenum)
{
Int64 total = 0;
Int64 prime = startprimenum;
for (int i = 0; i < primenum; i++)
{
total += prime;
prime = findPrimeAfter(prime);
}
endprimenum = prime;
return total;
}
static Int64 findPrimeBefore(Int64 prime)
{
if (prime == 2)
return 2;
if (prime == 3)
return 2;
if (prime % 2 == 0)
prime = prime - 1;
else
{
prime = prime - 2;
}
while (!isPrime(prime) && prime > 0)
{
prime = prime - 2;
}
return prime;
}
static Int64 findPrimeBefore(Int64 prime, Int64 before)
{
while (before-- > 0 && prime > 0)
prime = findPrimeBefore(prime);
return prime;
}
static Int64 findPrimeAfter(Int64 prime, Int64 after)
{
while (after-- > 0)
prime = findPrimeAfter(prime);
return prime;
}
static Int64 findPrimeAfter(Int64 prime)
{
if (prime == 2)
return 3;
if ((prime & 1) == 1)
prime = prime + 2;
else
{
prime = prime + 1;
}
while (!isPrime(prime) && prime > 0)
{
prime = prime + 2;
}
return prime;
}
