黑洞6714
来源:互联网 发布:ubuntu tmp 清理 编辑:程序博客网 时间:2024/05/16 01:41
#file:hole.h
class hole
{
public:
void convert(int &org);
void compare(int &org,int n=4);
void sub(int &max,int &min);
void show();
private:
int origin;
static int step;
int max,min;
};
#file:hole.cpp
#include "hole.h"
#include <iostream.h>
int hole::step=0;
void hole::convert(int &org)
{
origin=org;
if(org!=6174)
{
// step++;
compare(org);
}
}
void hole::sub(int &max,int &min)
{
step++;
origin=max-min;
convert(origin);
}
void hole::compare(int &org,int n)
{
int temp;//,k;
static int a[4]={0};
// int a[4]={0};
if(0==n) //这里采用了数据结构中的冒泡排序算法
{
for(int i=1;i<=3;i++)
{
// k=i;
for(int j=0;j<4-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
min = a[0]*1000+a[1]*100+a[2]*10+a[3];
max=a[3]*1000+a[2]*100+a[1]*10+a[0];
if(max==min || max==0 || min==0)
{
cout<<"此数据无法从黑洞中逃脱,步数无限...."<<endl;
return;
}
//Test
//cout<<"max="<<max<<endl;
//cout<<"min="<<min<<endl;
sub(max,min);
}
else //这里采用了递归调用的算法
{
a[n-1]=org%10;
org=org/10;
hole::compare(org,n-1);
}
}
void hole::show()
{
if(max==min || max==0 || min==0)
{
return ;
}
else
{
cout<<"Total step is :"<<step<<endl;
}
}
#file:hole_main.cpp
#include <iostream>
using namespace std;
#include "Hole.h"
int main()
{
int x;
cout<<"please input data (四位整数) :"<<endl;
cin>>x;
hole hl;
hl.convert(x);
hl.show();
return 0;
}
/////这是我的测试数据..........
黑洞6174问题:
为什么对所有数据的组合进行的测试,只有少数的几组数据得到的结果需要7步,而其同一组数据的其他组合
却得不到7?
1235
7
5321
7
2135
7
1325
7
2315
7
2513
7
2531
7
1523
7
1532
7
//////////////////////////////////////////
3125
7
3215
7
3251
7
3521
7
3512
7
/////////////////////////////////////////////
5123
7
5132
7
5231
7
5213
7
5312
7
5321
7
////////////////////////////////////////
8888 此数据无法从黑洞中逃脱,步数无限...."<<
5555 此数据无法从黑洞中逃脱,步数无限...."<<
经过测试所有的四位数字只要不相等就就可以得到需要7步才可以退出程序.....
就需要经过7步才可以保证相减的差等于6714.............
- 黑洞6714
- 黑洞
- 黑洞
- 双施瓦希黑洞
- 数学黑洞
- 什么是黑洞
- 数字黑洞
- 解密黑洞
- 寻找黑洞
- 数字黑洞
- Linux黑洞
- 性能黑洞
- 黑洞数
- 数字黑洞
- 黑洞数
- 数字黑洞
- 黑洞数
- 数字黑洞
- 在WinForm中控制GIF动画的启停的一种方法
- 网络嗅探教程:使用Sniffer Pro监控网络流量
- 质数算法
- 网络嗅探教程:使用Sniffer Pro监控网络流量 2
- 网络嗅探教程:使用Sniffer Pro监控网络流量 3
- 黑洞6714
- 一个关于javascript变量的类型
- delphi创建DLL动态链接库 [转贴]
- My first job - Goodbaby Group in Shanghai
- 如何将EXCEL内容导入mysql
- 浅谈在Delphi中Dll调试方法以及怎样在Dll中使用数据模块DataModule(或表单Form)
- 实时以态网络---EtherCAT
- Delphi指针详解
- 转:算法的力量