<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mingbo &#187; code</title>
	<atom:link href="http://shao.mingbo.de/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://shao.mingbo.de</link>
	<description>包括教育技术，编程，互联网等方面的文章及随想。</description>
	<lastBuildDate>Sun, 25 Jul 2010 05:18:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>一道数学题</title>
		<link>http://shao.mingbo.de/2009/11/10/a-math-game/</link>
		<comments>http://shao.mingbo.de/2009/11/10/a-math-game/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 01:33:56 +0000</pubDate>
		<dc:creator>邵 明博</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[数学]]></category>

		<guid isPermaLink="false">http://shao.mingbo.de/?p=197</guid>
		<description><![CDATA[有这样一道题：将一个数的末位，放到首位，使得结果是原数的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&#60;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 [...]]]></description>
			<content:encoded><![CDATA[<p>有这样一道题：将一个数的末位，放到首位，使得结果是原数的2倍，求这个数。如：21，转换后，变成12，12不是21的2倍，不符合条件。寻找符合条件的答案，乍一想，可能最难的地方是不知道他是一个几位数。这道题是我从柳大侠的博客上剽窃来的一道题，当初人家是用心算给出的结果。很出乎我意料的是，他的答案居然貌似是正确，并且答案的字面让我很震撼：是一个18位数。</p>
<p>我很纳闷，并有些痛苦的感叹。但很快的我决定写一个程序来代替我思考这样一道算术游戏。废话不多说，上代码：</p>
<pre name="code" class="c-sharp">
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&lt;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 x;
int count = 0;
findTheNumber f = new findTheNumber();
ArrayList nlist = f.getN();
try
{
FileStream fs = new FileStream("numGame.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
foreach (int n in nlist)
{
for (int y = 2; y &lt;= 9; y++)
{
x = (f.mathPower(n - 1) - 2) * y / 19;
sw.WriteLine("Answer {0}:{1}", ++count, (10 * x + y));
}
}
sw.Close();
}
catch
{
Console.WriteLine("异常");
Console.ReadLine();
}
}
}
}
</pre>
<p>代码中已经包含了我的具体分析，值得指出的是BigInteger这个类需要自己来实现，否则会在答案输出上只能输出9个答案。（因为long的数值限制）另外，这里附带一个参考答案（1000位数以内的所有答案）下载。<a class="downloadlink" href="http://shao.mingbo.de/wp-content/plugins/download-monitor/download.php?id=1" title="Version1.0 downloaded 53 times" >参考答案 (53)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://shao.mingbo.de/2009/11/10/a-math-game/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
