昨天在百度知道上看见一个ACM题目,
输入两个4位质数 A B
要求每次换一位数字,在最小步数内,由A换成B,且每变换一位,新数也为质数。
求最小步数。
要源程序。
例子:
1033
1733
3733
3739
3779
8779
8179
由1033到8179
一时兴起,编了两个多小时,终于给摆弄出来啦,可惜我的百度 账号被封了....无奈,只有贴上自己的博客再说~~
输出:
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "vld.h"
using namespace std;
class Number
{
public:
int nNum;//整个数
int n1;//千位
int n2;//百位
int n3;//十位
int n4;//个位
Number(){}
Number(int a,int b){
int init;
cin>>init;
while(init<1000||init>9999||!IsTheNum(init)){
cout<<"请输入1000-9999的质数!!"<<endl;
cin>>init;
}
setNum(init);
}
Number(Number &a){
setNum(a.nNum);
}
void setDigitA(int a){
n1 = a;
nNum = n1*1000+n2*100+n3*10+n4;
}
void setDigitB(int a){
n2 = a;
nNum = n1*1000+n2*100+n3*10+n4;
}
void setDigitC(int a){
n3 = a;
nNum = n1*1000+n2*100+n3*10+n4;
}
void setDigitD(int a){
n4 = a;
nNum = n1*1000+n2*100+n3*10+n4;
}
Number(int init)
{
if (IsTheNum(init))
{
cout<<"ok"<<endl;
setNum(init);
}
else{
cout<<"false"<<endl;
}
}
void setNum(int init){
nNum = init;
n4 = init % 10;
n1 = init / 1000;
n2 = init / 100 - n1 * 10 ;
n3 = init / 10 - n2 * 10 - n1 * 100;
}
bool compare(Number a){
if (nNum == a.nNum)
{
return true;
}
return false;
}
bool IsTheNum(int init){
int s = (int)sqrt((double)init);
for (int i=2;i<=s;i++)
{
if (init % i == 0)
{
return false;
}
}
return true;
}
};
class NumberList
{
public:
Number *num;
NumberList *next;
NumberList *pre;
NumberList(int nn){
num=new Number;
num->setNum(nn);
next = NULL;
pre = NULL;
}
};
class list{
public:
NumberList *head;
NumberList *tail;
NumberList *full;
list(){
head = tail = full = NULL;
}
bool nHaveTheNum(Number *a)
{
NumberList *pListTemp = full;
while (pListTemp)
{
if (pListTemp->num->compare(*a))
{
return true;
}
pListTemp = pListTemp->next;
}
return false;
}
void Push(NumberList *pList){
if(head == NULL)
{
cout<<"PUSH "<<pList->num->nNum<<" OK!"<<endl;
head = pList;
tail = pList;
full = pList;
}
else
{
cout<<"PUSH "<<pList->num->nNum<<" OK!"<<endl;
tail->next = pList;
tail = tail->next;
}
return;
}
NumberList *Pop()
{
if (head == NULL)
{
return NULL;
}
else
{
NumberList *a = head;
cout<<"POP "<<a->num->nNum<<" OK!"<<endl;
head = head->next;
return a;
}
}
void printList(){
NumberList *temp = full;
while (temp)
{
cout<<temp->num->nNum<<endl;
temp = temp->next;
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Number a(1,1); //起始质数
Number b(1,1); //目标质数
list uListOfIterate;//遍历存储结构
int temp1 = a.nNum > b.nNum ? a.nNum:b.nNum;//大数
int temp2 = a.nNum < b.nNum ? a.nNum:b.nNum;//小数
Number t1(temp1);
NumberList uHeadNumberlist(temp2);
uListOfIterate.Push(&uHeadNumberlist);
NumberList *uCurNumberList;
NumberList *uDestNumberList;
while ((uCurNumberList = uListOfIterate.Pop())!= NULL)
{
for (int n = 1;n < 10;n++)
{
NumberList *temp;
temp = new NumberList(uCurNumberList->num->nNum);
temp->num->setDigitA(n);
if (t1.IsTheNum(temp->num->nNum))
{
if (t1.compare(*temp->num))
{
cout<<"Get The Result!!!"<<endl;
temp->pre = uCurNumberList;
uDestNumberList = temp;
goto Exit0;
}
else{
if (uListOfIterate.nHaveTheNum(temp->num))
{ ;}
else{
temp->pre = uCurNumberList;
uListOfIterate.Push(temp);
}
}
}
}
for (int m = 1;m < 10;m++)
{
NumberList *temp1;
temp1 = new NumberList(uCurNumberList->num->nNum);
temp1->num->setDigitB(m);
if (t1.IsTheNum(temp1->num->nNum))
{
if (t1.compare(*temp1->num))
{
cout<<"Get The Result!!!"<<endl;
temp1->pre = uCurNumberList;
uDestNumberList = temp1;
goto Exit0;
}
else{
if (uListOfIterate.nHaveTheNum(temp1->num))
{;}
else{
temp1->pre = uCurNumberList;
uListOfIterate.Push(temp1);
}
}
}
}
for (int p = 1;p < 10;p++)
{
NumberList *temp2;
temp2 = new NumberList(uCurNumberList->num->nNum);
temp2->num->setDigitC(p);
if (t1.IsTheNum(temp2->num->nNum))
{
if (t1.compare(*temp2->num))
{
cout<<"Get The Result!!!"<<endl;
temp2->pre = uCurNumberList;
uDestNumberList = temp2;
goto Exit0;
}
else{
if (uListOfIterate.nHaveTheNum(temp2->num))
{;}
else{
temp2->pre = uCurNumberList;
uListOfIterate.Push(temp2);
}
}
}
}
for (int q = 1;q < 10;q++)
{
NumberList *temp3;
temp3 = new NumberList(uCurNumberList->num->nNum);
temp3->num->setDigitD(q);
if (t1.IsTheNum(temp3->num->nNum))
{
if (t1.compare(*temp3->num))
{
cout<<"Get The Result!!!"<<endl;
temp3->pre = uCurNumberList;
uDestNumberList = temp3;
goto Exit0;
}
else{
if (uListOfIterate.nHaveTheNum(temp3->num))
{;}
else{
temp3->pre = uCurNumberList;
uListOfIterate.Push(temp3);
}
}
}
}
}
Exit0:
int changTime =0;
while (uDestNumberList!=NULL)
{
cout<<uDestNumberList->num->nNum<<endl;
uDestNumberList = uDestNumberList->pre;
changTime++;
}
cout<<"用了:"<<changTime<<"次<<"<<endl;
cin>>changTime;
}