Posts Tagged ‘code

星期二 十一月 10, 2009 09:33

一道数学题

有这样一道题:将一个数的末位,放到首位,使得结果是原数的2倍,求这个数。如:21,转换后,变成12,12不是21的2倍,不符合条件。寻找符合条件的答案,乍一想,可能最难的地方是不知道他是一个几位数。这道题是我从柳大侠的博客上剽窃来的一道题,当初人家是用心算给出的结果。很出乎我意料的是,他的答案居然貌似是正确,并且答案的字面让我很震撼:是一个18位数。
我很纳闷,并有些痛苦的感叹。但很快的我决定写一个程序来代替我思考这样一道算术游戏。废话不多说,上代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;

namespace mathGame
{
class findTheNumber
{
static void Main(string[] args)
{
findTheNumber f = new findTheNumber();
f.showTheAnswer();
}

/**
*分析: 设目标为一个N位数,前N-1位为X,末位为Y
*       则,原数=10x+y;变换后=10^(N-1)*y+x;
*       (10x+y)*2=10^(N-1)*y+x
*       19x=(10^(N-1)-2)*y
*       由y为一个一位数,得到10^(N-1)-2为19的倍数,故有:
*
*/
ArrayList getN()
{
int n = 2;
findTheNumber f = new findTheNumber();
BigInteger a = f.mathPower(n-1)-2;
ArrayList al = new ArrayList();
do
{
if (a % 19 == 0)
{
al.Add(n);

}

n++;
a=f.mathPower(n-1)-2;

}while(a.dataLength<100);
return al;
}

BigInteger mathPower(int n)
{
BigInteger i ;
if (n == 0)
return 1;
else
{
return 10 * mathPower(n-1);
}

}
/**
*  通过getN()获取N值后,可得N=18,x=一个17位数*y;
*  满足题目的y值有2-9;
*
*/
void showTheAnswer()
{
BigInteger [...]

calendar

2010年八月
« 七    
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

最近评论