当前位置:网站首页>一共有n位整数,一位能被1整除,二位能被2整除,三位能被3整除,...,n位能被n整除。 求最大的满足这个条件的数字。(回溯法)
一共有n位整数,一位能被1整除,二位能被2整除,三位能被3整除,...,n位能被n整除。 求最大的满足这个条件的数字。(回溯法)
2022-07-20 05:42:00 【疯疯癫癫才自由】
问题:一共有n位整数,一位能被1整除,二位能被2整除,三位能被3整除,...,n位能被n整除。
求最大的满足这个条件的数字。
*/
/**
1)从小到大开始回溯;
从第一位开始枚举,枚举到第i位,当第i位不满足时,就改变i-1位的值,i-1位不满足,
就改变i-2位的值,依次类推,
/**
问题:一共有n位整数,一位能被1整除,二位能被2整除,三位能被3整除,...,n位能被n整除。
求最大的满足这个条件的数字。
*/
/**
1)从小到大开始回溯;
从第一位开始枚举,枚举到第i位,当第i位不满足时,就改变i-1位的值,i-1位不满足,
就改变i-2位的值,依次类推,
*/
/**
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
void find_back(int index);
const int maxn=50;
int data[2*maxn],temp[2*maxn];
int max_index=0;
int ans=0;
int main()
{
//cout << sizeof(data)/sizeof(data[0]) << endl;
memset(temp,0,sizeof(temp));
temp[1]=1; //从1开始搜索
find_back(1);
for(int i=1;i<=max_index;++i)
cout << data[i];
cout << endl;
cout << "Hello world!" << endl;
cout << "ans:" << ans << endl;
cout << "max_index:" << max_index << endl;
return 0;
}
void find_back(int index)
{
while(temp[1]<=9)
{
if(index>=max_index)
{
max_index=index;
for(int j=1;j<=max_index;++j)
data[j]=temp[j];
}
++index;
ans=max(ans,index);
int r=0;
for(int j=1;j<=index;++j)
{
r=r*10+temp[j];
r%=index;
}
if(r!=0) //当此时的数字不能被r整除时,要减去余数
{
temp[index]=temp[index]-r+index;//避免出现负数
while(temp[index]>9&&index>1)
{
temp[index]=0; //如果第index号位的数字大于9并且不是第1号位,就将index号重新从0开始搜索
--index; //枚举index号位的上一位
temp[index]+=index;
}
}
}
}
*/
2)从大到小开始回溯
/**
2)从大到小开始回溯
*/
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cstring>
using namespace std;
void find_back(int index);
const int maxn=50;
int data[2*maxn],temp[2*maxn];
int max_index=0;
int ans=0;
int main()
{
fill(begin(temp),end(temp),9);
for(int i=1;i<=maxn;++i)
cout << temp[i];
cout << endl;
find_back(1);
for(int i=1;i<=max_index;++i)
cout << data[i];
cout << endl;
cout << "Hello world!" << endl;
cout << "ans:" << ans << endl;
cout << "max_index:" << max_index << endl;
return 0;
}
void find_back(int index)
{
while(temp[1]>0)
{
if(index>max_index)
{
max_index=index;
for(int j=1;j<=max_index;++j)
data[j]=temp[j];
}
++index;
ans=max(ans,index);
int r=0;
for(int j=1;j<=index;++j)
{
r=r*10+temp[j];
r%=index;
}
if(r!=0)
{
temp[index]=temp[index]-r; //因为时是从最大值开始枚举的,所以只需要减去r即可
while(temp[index]<0&&index>1)
{
temp[index]=9;
--index;
temp[index]-=index;
}
}
}
}
边栏推荐
猜你喜欢
RIoTBoard开发板系列笔记(七)—— Framebuffer的使用
kettle优化之提高MySQL读写速度
解决错误:org.apache.ibatis.binding.BindingException
学习记录五
Model graduation thesis on traffic safety management
mark down学习
【kali的sshd服务开启】
Given a positive integer n, it is expressed as the addition of numbers 1,3,7,15. Please code to find the combination that minimizes the total number of occurrences of the above numbers (each number ca
RIoTBoard开发板系列笔记(六)—— buildroot构建系统镜像
【STM32F103RCT6】CAN通信
随机推荐
三种办法:字符串逆序排列(而非逆序打印)
pinctrl
kettle优化之提高MySQL读写速度
关于毕业前后的道路
Unity2D 自定义Scriptable Tiles的理解与使用(一)——了解TileBase类
矿业工程毕业论文题目
unity 根据索引 获取字典的键值,
启新聚势 云谱新篇|海泰方圆与四川联通达成生态战略合作
并发编程day01
针对嵌入式设备外接设备的驱动开发心得
水文与水资源毕业论文题目【213个】
pinctrl
Keras 载入历史.h5格式模型报错: AttributeError ‘str‘ object has no attribute ‘decode‘
STM32CubeMonitor的使用第一部分-数据绘图以及仪表显示
[Unity脚本优化] Optimizing garbage collection in Unity games
jsqlparser和pagehelper的jar包下载
解决tensoflow2.x的No module named:tensorflow.contrib
Model thesis of urban planning and design
Graduation thesis title of power system and automation [selected]
vsCode配置Eslint+Prettier结合使用详细配置步骤,规范化开发