一个车牌五位数倒立还也是五位数结果等于78633
求效率高的算法using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int count = 0;
int[] nums = { 0, 1, 2, -1, -1, -1, 9, -1, 8, 6 };
for (int i = 10000; i < 99999; i++)
{
//A=i
string A = i.ToString();
//B==A的倒立
string B = "";
for (int j = 4; j >= 0; j--)
{
if (nums[Convert.ToInt32(A.Substring(j, 1))] == -1)
{
break;
}
else
{
B += nums[Convert.ToInt32(A.Substring(j, 1))].ToString();
count++;
}
}
if (B.Length == 5 && (Convert.ToInt32(B) - Convert.ToInt32(A)) == 78633)
{
Console.WriteLine(count);
Console.Write(i);
break;
}
}
}
}
}
--------------------编程问答-------------------- 倒立? --------------------编程问答-------------------- 0倒立为0,1-1,2-5,5-2,6-9,8-8,9-6
首先要求车牌能够倒立,然后要求两者相减结果为78633
LZ是这个意思吗?
我暂且修改了你的
//int[] nums = { 0, 1, 2, -1, -1, -1, 9, -1, 8, 6 };
int[] nums = { 0, 1, 5, -1, -1, 2, 9, -1, 8, 6 };
要是理解错了麻烦指出 --------------------编程问答-------------------- 写了个来玩,继续等答案
const int nValue = 78633;
const int nMin = 00000;//为什么车牌从1万开始,我记得参拜过00001这个车牌,当然壹万之前肯定没有我们要找的答案,笑
const int nMax = 99999;
int[] nums = { 0, 1, 5, -1, -1, 2, 9, -1, 8, 6 };
bool CheckValue(int nValue1, int nValue2)
{
if (nValue2 < nMin || nValue2 > nMax)
return false;
int nValueCheck = 0;
while (nValue1 > 0)
{
int nCur = nums[nValue1 % 10];
if (nCur < 0)
return false;
nValueCheck *= 10;
nValueCheck += nCur;
nValue1 /= 10;
}
return (nValueCheck == nValue2);
}
void Main()
{
int count = 0;
for (int i = nMin; i <= nMax; i++)
{
if (CheckValue(i, i + nValue) || CheckValue(i, i - nValue))
{
count++;
Console.WriteLine("Count:{0}\tNum:{1}\t({2}\t{3})", count, i, i + nValue, i - nValue);
}
}
}
结果
Count:1 Num:10568 (89201 -----)
Count:2 Num:10968 (89601 -----)
Count:3 Num:89201 (----- 10568)
Count:4 Num:89601 (----- 10968) --------------------编程问答-------------------- 抱歉抱歉,上面的做法要转换两次数字,修改一下
const int nValue = 78633;
const int nMin = 00000;
const int nMax = 99999;
int[] nums = { 0, 1, 5, -1, -1, 2, 9, -1, 8, 6 };
int UpSideDown(int nValue1)
{
int nValueCheck = 0;
while (nValue1 > 0)
{
int nCur = nums[nValue1 % 10];
if (nCur < 0)
return -1;
nValueCheck *= 10;
nValueCheck += nCur;
nValue1 /= 10;
}
return nValueCheck;
}
void Main()
{
int count = 0;
for (int i = nMin; i <= nMax; i++)
{
if ((i + nValue) > nMax && (i - nValue < nMin))
continue;
int nValue2 = UpSideDown(i);
if (nValue2 < 0)
continue;
if (Math.Abs(i - nValue2) == nValue )
{
count++;
Console.WriteLine("Count:{0}\tNum:{1}\t({2})", count, i, nValue2);
}
}
} --------------------编程问答--------------------
private void buttonX2_Click(object sender, EventArgs e)--------------------编程问答-------------------- 很遗憾 我的代码没能找到你那个数字 不知道那个数字是否存在 --------------------编程问答-------------------- 我晕 忘记了倒立还要左右互换。。。 --------------------编程问答-------------------- 我原来那个代码有点问题,修改了一下
{
for (int mkl = 10000; mkl < 100000; mkl++)
{
int m = PD(mkl);
if (m != -1)
{
//如果不能倒立 通过得到的位数,将不能倒立的位加1 比如30027下一次就是40027,减少了10000次循环
if (m == 0)
mkl = mkl;
else if (m == 1)
mkl += 10;
else if (m == 2)
mkl += 100;
else if (m == 3)
mkl += 1000;
else if (m == 4)
mkl += 10000;
}
else
{
if (Turn(mkl) - mkl == 78633)
MessageBox.Show(mkl.ToString());
}
}
}
/// <summary>
/// 把字符串转换成倒立的 内容不解释
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
private int Turn(int num)
{
string oldstr = num.ToString();
string newstr = string.Empty;
for (int i = 0; i < 5; i++)
{
if (oldstr[i] == '0')
{
newstr+= "0";
}
else if (oldstr[i] == '1')
{
newstr += "1";
}
else if (oldstr[i] == '2')
{
newstr+= "5";
}
else if (oldstr[i] == '5')
{
newstr+= "2";
}
else if (oldstr[i] == '6')
{
newstr+= "9";
}
else if (oldstr[i] == '8')
{
newstr+= "8";
}
else if (oldstr[i] == '9')
{
newstr+= "6";
}
else
{
MessageBox.Show("ERROR");
}
}
return int.Parse(newstr);
}
/// <summary>
/// 判断一个数字能否倒立 能则返回-1,不能,则返回不能倒立的最高位数,比如30027 返回4(这里把个位算0位十位算1,以此类推)
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
private int PD(int num)
{
int m = -1;
for (int i = 0; i < 5; i++)
{
if (!check(int.Parse(num.ToString()[i].ToString())))
{
m = 4 - i;
break;
}
}
return m;
}
//判断该数字能否倒立
private bool check(int i)
{
if (i != 0 && i != 1 && i != 2 && i != 5 && i != 6 && i != 9)
return false;
else
return true;
}
const int nValue = 78633;
const int nMin = 0;
const int nMax = 99999;
string UpSideDown(string strValue)
{
if (strValue.IndexOfAny("347".ToCharArray()) >= 0)
return "";
string strRtn = "";
foreach (char cCur in strValue.ToCharArray())
{
strRtn = "0152986".ToCharArray()["0125689".IndexOf(cCur)] + strRtn;
}
return strRtn;
}
void Main()
{
int nCount1 = 0;
int nCount2 = 0;
int count = 0;
for (int i = 0; i <= 99999; i++)
{
if ((i + nValue) > nMax && (i - nValue) < nMin)
{
nCount1++;
continue;
}
string strValue2 = UpSideDown(i.ToString().PadLeft(5, '0'));
if (strValue2.Length <= 0)
{
nCount2++;
continue;
}
//if (Math.Abs(int.Parse(strValue2) - i) == nValue)
if (int.Parse(strValue2) - i == nValue)
{
count++;
Console.WriteLine("Count:{0}\tNum:{1}", count, i);
count++;
Console.WriteLine("Count:{0}\tNum:{1}", count, strValue2);
}
}
}
-----------------------------
楼上一语道破梦中人啊
我的想法只是
1 扣掉与78633做加减法后不合法的数字(57266个)
2 用IndexOf直接Continue包含347的字串(32640个)
这个部分没有楼上直接跳一位快
--------------------编程问答-------------------- 这么神奇的问题, --------------------编程问答-------------------- const int nValue = 78633;
const int nMin = 0;
const int nMax = 99999;
string UpSideDown(string strValue, ref int nWrongNum)
{
nWrongNum = strValue.LastIndexOfAny("347".ToCharArray());
if (nWrongNum >= 0)
return "";
string strRtn = "";
foreach (char cCur in strValue.ToCharArray())
{
strRtn = "0152986".ToCharArray()["0125689".IndexOf(cCur)] + strRtn;
}
return strRtn;
}
void Main()
{
int nCount1 = 0;
int nCount2 = 0;
int count = 0;
for (int i = 0; i <= 99999; i++)
{
//if ((i + nValue) > nMax && (i - nValue) < nMin)
if ((i + nValue) > nMax)
{
nCount1++;
continue;
}
int nWrongNum = 0;
string strValue2 = UpSideDown(i.ToString().PadLeft(5, '0'), ref nWrongNum);
if (strValue2.Length <= 0)
{
nCount2++;
switch (nWrongNum)
{
case 4:
i += 1;
break;
case 3:
i += 10;
break;
case 2:
i += 100;
break;
case 1:
i += 1000;
break;
case 0:
i += 10000;
break;
}
i--;
continue;
}
//if (Math.Abs(int.Parse(strValue2) - i) == nValue)
if (int.Parse(strValue2) - i == nValue)
{
count++;
Console.WriteLine("Count:{0}\tNum:{1}", count, i);
count++;
Console.WriteLine("Count:{0}\tNum:{1}", count, strValue2);
}
}
}
----------------------------
参考了楼上的方法
再修改了一下自己的
先排除加法后不合法的78600个数字
再排除2644个含有非法数字的字符
如果不用楼上的算法这里要多出(16075 - 2644)次循环
--------------------编程问答-------------------- 好奇怪的问题啊 --------------------编程问答-------------------- Mark.日后再看。 --------------------编程问答-------------------- for (int i = 0; i <= 21366; i++)
即可 --------------------编程问答-------------------- 恩,楼上那个我考虑过
但是如果题目改变,要求相减数更小的话这个地方也要改成99999-某值
比如 相减后为0的有147对(此时0-99999必须全部循环) 相减后为300的有98对
目前有个烦恼
因为这种数字必定是成对出现的
怎样在得出较小数字对应的那个较大数字时,下次循环到这个较大数字直接跳过?
代码如下
const int nValue = 300;
const int nMin = 0;
const int nMax = 99999;
string UpSideDown(string strValue, ref int nWrongNum)
{
nWrongNum = strValue.LastIndexOfAny("347".ToCharArray());
if (nWrongNum >= 0)
return "";
string strRtn = "";
foreach (char cCur in strValue.ToCharArray())
{
strRtn = "0152986".ToCharArray()["0125689".IndexOf(cCur)] + strRtn;
}
return strRtn;
}
void Main()
{
int nCount1 = 0;
int nCount2 = 0;
int nCount21 = 0;
int nCount3 = 0;
int nCount4 = 0;
int count = 0;
for (int i = 0; i <= 99999; i++)
{
//if ((i + nValue) > nMax && (i - nValue) < nMin)
if ((i + nValue) > nMax)
{
nCount1++;
continue;
}
int nWrongNum = 0;
string strValue2 = UpSideDown(i.ToString().PadLeft(5, '0'), ref nWrongNum);
if (strValue2.Length <= 0)
{
nCount2++;
switch (nWrongNum)
{
case 4:
i += 1;
break;
case 3:
i += 10;
nCount21 += 10 - 1;
break;
case 2:
i += 100;
nCount21 += 100 - 1;
break;
case 1:
i += 1000;
nCount21 += 1000 - 1;
break;
case 0:
i += 10000;
nCount21 += 10000 - 1;
break;
}
i--;
continue;
}
//if (Math.Abs(int.Parse(strValue2) - i) == nValue)
if (int.Parse(strValue2) - i == nValue)
{
count++;
Console.WriteLine("Count:{0}\tNum:{1}", count, i);
count++;
Console.WriteLine("Count:{0}\tNum:{1}", count, strValue2);
}
else if (int.Parse(strValue2) - i == -nValue)
{
nCount3++; // 这里就是我苦恼的源泉啊
}
else
{
nCount4++;
}
}
}
如果用ArrayList什么的记住,那无非是空间换时间。。。。。 --------------------编程问答-------------------- 先纠正我2-5;5-2的看法
2倒立还是2,虽然要在一定字体下
运算结果是
Count:1 Num:10968
Count:2 Num:89601
-----
const int nValue = 78633;
const int nMin = 0;
const int nMax = 99999;
string UpSideDown(string strValue, ref int nWrongNum)
{
nWrongNum = strValue.LastIndexOfAny("3457".ToCharArray());
if (nWrongNum >= 0)
return "";
string strRtn = "";
for (int i = strValue.Length - 1; i >= 0; i--)
{
string strCur = "012986".ToCharArray()["012689".IndexOf(strValue.Substring(i, 1))].ToString();
strRtn += strCur;
if (string.Compare(strRtn, strValue.Substring(0, strRtn.Length)) < 0)
{
return "";
}
}
return strRtn;
}
void Main()
{
int count = 0;
for (int i = 0; i <= 99999; i++)
{
if ((i + nValue) > nMax)
{
continue;
}
int nWrongNum = 0;
string strValue2 = UpSideDown(i.ToString().PadLeft(5, '0'), ref nWrongNum);
if (strValue2.Length <= 0)
{
if (nWrongNum > 0)
{
switch (nWrongNum)
{
case 4:
i += 1;
break;
case 3:
i += 10;
break;
case 2:
i += 100;
break;
case 1:
i += 1000;
break;
case 0:
i += 10000;
break;
}
i--;
}
continue;
}
//if (Math.Abs(int.Parse(strValue2) - i) == nValue)
if (int.Parse(strValue2) - i == nValue)
{
count++;
Console.WriteLine("Count:{0}\tNum:{1}", count, i);
count++;
Console.WriteLine("Count:{0}\tNum:{1}", count, strValue2);
}
}
}
--------------------编程问答-------------------- const int nValue = 78633;
const int nMin = 0;
const int nMax = 99999;
string UpSideDown(string strValue, ref int nWrongNum)
{
nWrongNum = strValue.LastIndexOfAny("3457".ToCharArray());
if (nWrongNum >= 0)
return "";
string strRtn = "";
for (int i = strValue.Length - 1; i >= 0; i--)
{
count1++;
string strCur = "012986".ToCharArray()["012689".IndexOf(strValue.Substring(i, 1))].ToString();
strRtn += strCur;
if (string.Compare(strRtn, strValue.Substring(0, strRtn.Length)) < 0)
{
return "";
}
}
return strRtn;
}
int count1 = 0;
void Main()
{
count1 = 0;
for (int i = 10000; i <= 99999; i++)
{
if ((i + nValue) > nMax)
{
continue;
}
int nWrongNum = 0;
string strValue2 = UpSideDown(i.ToString().PadLeft(5, '0'), ref nWrongNum);
if (strValue2.Length <= 0)
{
if (nWrongNum > 0)
{
switch (nWrongNum)
{
case 4:
i += 1;
break;
case 3:
i += 10;
break;
case 2:
i += 100;
break;
case 1:
i += 1000;
break;
case 0:
i += 10000;
break;
}
i--;
}
continue;
}
//if (Math.Abs(int.Parse(strValue2) - i) == nValue)
if (int.Parse(strValue2) - i == nValue)
{
Console.WriteLine("Count:{0}\tNum:{1}", count1, i);
// Console.WriteLine("Count:{0}\tNum:{1}", count1, strValue2);
}
}
}
修改修改
这次我总算明白过来楼主那个count是用来干吗的了,我太粗心了
从10000开始的话我是877次,如果我没放错地方的话(从0开始就是7075次)
--------------------编程问答-------------------- 插楼MARK
--------------------编程问答-------------------- const int nValue = 78633;
void Main()
{
int count1 = 0;
for (int i = 10000; i <= 99999 - nValue; i++)
{
string strValue1 = i.ToString().PadLeft(5, '0');
string strValue2 = (i + nValue).ToString().PadLeft(5, '0');
bool bWrondNum = false;
int nWrondIndex = -1;
for (int j = strValue1.Length - 1; j >= 0; j--)
{
count1++;
string strChar1 = strValue1.Substring(j, 1);
string strChar2 = strValue2.Substring(strValue2.Length - j - 1, 1);
switch (strChar1)
{
case "6":
if (strChar2 != "9")
bWrondNum = true;
break;
case "9":
if (strChar2 != "6")
bWrondNum = true;
break;
case "3":
case "4":
case "5":
case "7":
nWrondIndex = j;
bWrondNum = true;
break;
default:
if (strChar1 != strChar2)
bWrondNum = true;
break;
}
if (bWrondNum)
break;
}
if (bWrondNum)
{
if (nWrondIndex >= 0)
{
switch (nWrondIndex)
{
case 4:
i += 1;
break;
case 3:
i += 10;
break;
case 2:
i += 100;
break;
case 1:
i += 1000;
break;
case 0:
i += 10000;
break;
}
i--;
}
continue;
}
if (!bWrondNum)
{
Console.WriteLine("Count:{0}\tNum:\t{1}", count1, strValue1);
}
}
}
这次是439次 --------------------编程问答-------------------- 到底谁是LZ? --------------------编程问答-------------------- 什么事倒立?那个糖糖真的很辛苦 --------------------编程问答-------------------- = =我只是打发无聊,这两天心情不太好
谢谢楼上的大大
我猜LZ的意思是把纯数字的车牌转180度
一般意义上的车牌估计只有8能倒转,6和9仔细点看应该有不同吧,1和2很勉强
最后上一个毫无效率算法可言的纯粹图省事的代码
const int nValue = 78633;
void Main()
{
int count = 0;
for (int i = 10000; i < 99999 - nValue; i++)
{
string strValue = i.ToString().PadLeft(5, '0');
if (strValue.LastIndexOfAny("3457".ToCharArray()) >= 0)
continue;
count++;
byte[] data = Encoding.Default.GetBytes(strValue.Replace("9", "A").Replace("6", "9").Replace("A", "6"));
Array.Reverse(data);
if (int.Parse(System.Text.ASCIIEncoding.Default.GetString(data)) - i == nValue)
{
Console.WriteLine("Count:\t{0}\tNum:\t{1}", count, i);
}
}
}
此处count已经失去了原来的意义
好了,回去上班了,虽然有点腻烦该干的还是要干完 --------------------编程问答-------------------- 好东西,收藏了先。再慢慢研究 --------------------编程问答-------------------- mark --------------------编程问答-------------------- 如果0不能倒立,是多么完美的事啊。
补充:.NET技术 , C#