gpt4 book ai didi

java回溯生成数字

转载 作者:行者123 更新时间:2023-12-01 05:04:10 25 4
gpt4 key购买 nike

我编写了一个小程序来生成具有两个条件的数字:

  1. 包含从 1 到 9 的所有数字,因此数字如 123456789
  2. 该数字必须能被最后一位数字整除 例如 442 因为 4 % 2 == 0 和 4 % 4 == 0

这是我的回溯算法:

static void backTrack(int value)
{
//Check if the number has all 9 digits, that it is dividable
if(isNine(value) && isDiv(value))
{
//System.out.println(value);
System.out.println("Found solution.");
System.out.println(aantal);
aantal++;
}
else
{

if(howMany(value) >= 9)
return;

for(int i = 1; i < 10; i++)
{
value = value * 10 + i;
if(value % i == 0 && howMany(value) <= 9)
{
//System.out.println(value);
backTrack(value);
}
value = value / 10;
}
}
}

//Gives length of integer for example 124 must give 3, 13 gives 2
static int howMany(int value)
{
int test = value % 10;
value = value / 10;
int teller = 0;

while(test != 0)
{
teller++;
test = value % 10;
value = value / 10;
}
return teller;
}

//Checks if the number is dividable by the last digit of the number and keeps recursive doing this for the whole number so 442 = YES 235 = NO
static boolean isDiv(int value)
{
int test = value % 10;
value = value / 10;

while(test != 0)
{
if(value % test == 0)
{
test = value % 10;
value = value / 10;
}
else
return false;
}
return true;
}

//Checks if the number has all digits from 1 to 9
static boolean isNine(int value)
{
boolean values[] = new boolean[10];
int test = value % 10;
int counter = 0;

for(int i = 1; i < values.length; i++)
values[i] = false;

while( test != 0)
{
if(values[test])
return false;
else
{
values[test] = true;
value = value /10;
test = value % 10;
}
}

for(int i = 1; i < values.length; i++)
{
if(values[i])
counter++;
}

if(counter == 9)
return true;
else
return false;
}

它永远不会找到解决方案,我测试了所有子功能,这些功能运行得很好。

我的回溯方案有问题吗? System.out.println(aantal) 只是一个 var,用于计算我找到了多少个解决方案。

我从backtrack(0);

开始

最佳答案

static void backTrack(int value)
{
//Check if the number has all 9 digits, that it is dividable
if(isNine(value)) // CHANGED this if test.
{
System.out.println("Found solution.");
System.out.println(value);
aantal++;
}
else
{

if(howMany(value) >= 9)
return;

for(int i = 1; i < 10; i++)
{
value = value * 10 + i;
if(value % i == 0 && howMany(value) <= 9)
{
//System.out.println(value);
backTrack(value);
}
value = value / 10;
}
}
}

//Gives length of integer for example 124 must give 3, 13 gives 2
static int howMany(int value)
{
int test = value % 10;
value = value / 10;
int teller = 0;

while(test != 0)
{
teller++;
test = value % 10;
value = value / 10;
}
return teller;
}


//Checks if the number has all digits from 1 to 9
static boolean isNine(int value)
{
boolean values[] = new boolean[10];
int test = value % 10;
int counter = 0;

for(int i = 1; i < values.length; i++)
values[i] = false;

while( test != 0)
{
if(values[test])
return false;
else
{
values[test] = true;
value = value /10;
test = value % 10;
}
}

for(int i = 1; i < values.length; i++)
{
if(values[i])
counter++;
}

if(counter == 9)
return true;
else
return false;
}
}

关于java回溯生成数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12958968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com