用java编写一个程序,内容为匆1-15这几个数中随机选6个不重复数,输出所有结果 20
输出结果如下:1:1,2,3,4,5,62:1,2,3,4,5,7......5005:10.11.12.13.14.15...
输出结果如下:
1:1,2,3,4,5,6
2:1,2,3,4,5,7
......
5005:10.11.12.13.14.15 展开
1:1,2,3,4,5,6
2:1,2,3,4,5,7
......
5005:10.11.12.13.14.15 展开
3个回答
展开全部
程序如下。
import java.util.Stack;
public class MyUitl {
private Stack<Integer> values = new Stack<Integer>();
public static void main(String[] args){
MyUitl myUitl= new MyUitl();
myUitl.Calculate(1);
}
public void Calculate(int num){
if(values.size() == 5){
printValue(values);
return ;
}
for(int i=num;i<=15;i++){
//移除多余元素
while(!values.isEmpty() && values.peek() >= num){
values.pop();
}
//增加新元素
values.add(i);
Calculate(i+1);
}
}
private void printValue(Stack values) {
for(int i=0;i<values.size();i++){
System.out.print(values.get(i)+" ");
}
System.out.println("");
}
}
import java.util.Stack;
public class MyUitl {
private Stack<Integer> values = new Stack<Integer>();
public static void main(String[] args){
MyUitl myUitl= new MyUitl();
myUitl.Calculate(1);
}
public void Calculate(int num){
if(values.size() == 5){
printValue(values);
return ;
}
for(int i=num;i<=15;i++){
//移除多余元素
while(!values.isEmpty() && values.peek() >= num){
values.pop();
}
//增加新元素
values.add(i);
Calculate(i+1);
}
}
private void printValue(Stack values) {
for(int i=0;i<values.size();i++){
System.out.print(values.get(i)+" ");
}
System.out.println("");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.math.*;
public class Main {
public static int[] getRandom(){
int[] from = new int[15];
for(int i = 0; i < 15; i++){
from[i] = i+1;
}
int[] to = new int[6];
int choose = 0;
int temp;
for(int i = 0; i < 6;i++){
choose = (int)(Math.random()*(14-i));
to[i] = from[choose];
temp = from[14-i];
from[14-i] = from[choose];
from[choose] = temp;
}
return to;
}
public static void main(String[] args) {
int test[] = {1,2,3,4,5,6};
for(int i = 0; i < 20; i++){
test = getRandom();
System.out.print((i+1)+ +
public class Main {
public static int[] getRandom(){
int[] from = new int[15];
for(int i = 0; i < 15; i++){
from[i] = i+1;
}
int[] to = new int[6];
int choose = 0;
int temp;
for(int i = 0; i < 6;i++){
choose = (int)(Math.random()*(14-i));
to[i] = from[choose];
temp = from[14-i];
from[14-i] = from[choose];
from[choose] = temp;
}
return to;
}
public static void main(String[] args) {
int test[] = {1,2,3,4,5,6};
for(int i = 0; i < 20; i++){
test = getRandom();
System.out.print((i+1)+ +
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
public class Test
{
public static void main(String[] args)
{
Set<Integer> findNum = new HashSet();
while(findNum.size()<6){
Integer randomNum = (int)(Math.random()*15)+1;
findNum.add(randomNum);
}
Iterator itr = findNum.iterator();
String findNumStr = "";
while(itr.hasNext()){
findNumStr+=itr.next()+",";
}
findNumStr = findNumStr.substring(0,findNumStr.length()-1);
System.out.println(findNumStr);
}
}
import java.util.Set;
import java.util.Iterator;
public class Test
{
public static void main(String[] args)
{
Set<Integer> findNum = new HashSet();
while(findNum.size()<6){
Integer randomNum = (int)(Math.random()*15)+1;
findNum.add(randomNum);
}
Iterator itr = findNum.iterator();
String findNumStr = "";
while(itr.hasNext()){
findNumStr+=itr.next()+",";
}
findNumStr = findNumStr.substring(0,findNumStr.length()-1);
System.out.println(findNumStr);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询