用java编写生成5位数的数(数字中不包含4),并且每次生成的都不能和之前生成的有重复
答案:2 悬赏:70 手机版
解决时间 2021-02-24 04:16
- 提问者网友:人傍凄凉立暮秋
- 2021-02-23 04:25
用java编写生成5位数的数(数字中不包含4),并且每次生成的都不能和之前生成的有重复
最佳答案
- 五星知识达人网友:平生事
- 2021-02-23 06:04
package zhidao;
import java.util.Arrays;
import java.util.LinkedList;
public class RecursionSubNSort
{
public static void main ( String[] args )
{
String[] A = { "1", "2", "3", "5", "6", "7", "8", "9", "0" };
LinkedList list = new LinkedList ();
recursionSub (list, 5, A, 0, -1);
System.out.println ("符合条件的5位数有:");
for ( String[] strings : list )
{
System.out.println (Arrays.toString (strings).replaceAll ("[\\[\\],\\s]", ""));
}
}
private static LinkedList recursionSub ( LinkedList list, int count, String[] array, int ind,
int start, int... indexs )
{
start++;
if (start > count - 1)
{
return null;
}
if (start == 0)
{
indexs = new int[array.length];
}
for ( indexs[start] = 0; indexs[start] < array.length; indexs[start]++ )
{
recursionSub (list, count, array, 0, start, indexs);
if (start == count - 1)
{
String[] temp = new String[count];
for ( int i = count - 1; i >= 0; i-- )
{
temp[start - i] = array[indexs[start - i]];
}
if (!"0".equals (temp[0]))
{
list.add (temp);
}
}
}
return list;
}
}
import java.util.Arrays;
import java.util.LinkedList;
public class RecursionSubNSort
{
public static void main ( String[] args )
{
String[] A = { "1", "2", "3", "5", "6", "7", "8", "9", "0" };
LinkedList
recursionSub (list, 5, A, 0, -1);
System.out.println ("符合条件的5位数有:");
for ( String[] strings : list )
{
System.out.println (Arrays.toString (strings).replaceAll ("[\\[\\],\\s]", ""));
}
}
private static LinkedList
int start, int... indexs )
{
start++;
if (start > count - 1)
{
return null;
}
if (start == 0)
{
indexs = new int[array.length];
}
for ( indexs[start] = 0; indexs[start] < array.length; indexs[start]++ )
{
recursionSub (list, count, array, 0, start, indexs);
if (start == count - 1)
{
String[] temp = new String[count];
for ( int i = count - 1; i >= 0; i-- )
{
temp[start - i] = array[indexs[start - i]];
}
if (!"0".equals (temp[0]))
{
list.add (temp);
}
}
}
return list;
}
}
全部回答
- 1楼网友:摆渡翁
- 2021-02-23 07:36
按照你的要求编写的,输出从0到max中用二进制表示和十进制表示都是回文数的java程序如下:
import java.util.scanner;
public class ccf {
public static void main(string[] args) {
scanner sc=new scanner(system.in);
system.out.print("请输入一个整数:");
int max=sc.nextint();
for(int i=0;i<=max;i++){
string num = string.valueof(i);
string num2=integer.tobinarystring(i);
string reversenum=new stringbuffer(num).reverse().tostring();
string reversenum2=new stringbuffer(num2).reverse().tostring();
if(reversenum.equals(num) && reversenum2.equals(num2)){
system.out.println(i+"用二进制表示"+num2+"和十进制表示"+i+"都是回文数");
}
}
}
}运行结果:
请输入一个整数:1000
0用二进制表示0和十进制表示0都是回文数
1用二进制表示1和十进制表示1都是回文数
3用二进制表示11和十进制表示3都是回文数
5用二进制表示101和十进制表示5都是回文数
7用二进制表示
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯