用JAVA编写程序"36选7”,从1到36数中任意输入7个数,其中数字不重复 并打印出来
3个回答
2013-08-03
展开全部
public class test {
public static void main(String[] args) {
int a[] = new int[36];
for(int i = 0; i < 36; ++i)
a[i] = i+1;
select(a, 7, 36);
}
static void select(int[] a, int n, int m) {
qsort(a, 0, m-1);
reverse(a, n, m-1);
print(a, 0, n-1);
int p = n;
while(true) {
if(p == 0)
break;
int k = p;
if(a[--p] < a[k]) {
int r = m;
while(a[--r] < a[p]);
if(p == 0 && r == 1)
break;
if(r < n)
continue;
swap(a, r, p);
if(k < n) {
qsort(a, k, r-1);
reverse(a, n, r-1);
}
print(a, 0, n-1);
p = n;
}
}
}
static int c = 0;
static void print(int[] a, int i, int j) {
System.out.print("No." + ++c + " ");
for(int k = i; k <= j; ++k)
System.out.print(a[k]+" ");
System.out.println();
}
static void swap(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
static void reverse(int[] a, int i, int j) {
while(i < j)
swap(a, i++, j--);
}
static void qsort(int[] a, int i, int j) {
while(i < j) {
int s = i;
for(int k = i; k < j; ++k)
if(a[k] < a[j])
swap(a, s++, k);
swap(a, s, j);
qsort(a, i, s-1);
i = s + 1;
}
}
}
public static void main(String[] args) {
int a[] = new int[36];
for(int i = 0; i < 36; ++i)
a[i] = i+1;
select(a, 7, 36);
}
static void select(int[] a, int n, int m) {
qsort(a, 0, m-1);
reverse(a, n, m-1);
print(a, 0, n-1);
int p = n;
while(true) {
if(p == 0)
break;
int k = p;
if(a[--p] < a[k]) {
int r = m;
while(a[--r] < a[p]);
if(p == 0 && r == 1)
break;
if(r < n)
continue;
swap(a, r, p);
if(k < n) {
qsort(a, k, r-1);
reverse(a, n, r-1);
}
print(a, 0, n-1);
p = n;
}
}
}
static int c = 0;
static void print(int[] a, int i, int j) {
System.out.print("No." + ++c + " ");
for(int k = i; k <= j; ++k)
System.out.print(a[k]+" ");
System.out.println();
}
static void swap(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
static void reverse(int[] a, int i, int j) {
while(i < j)
swap(a, i++, j--);
}
static void qsort(int[] a, int i, int j) {
while(i < j) {
int s = i;
for(int k = i; k < j; ++k)
if(a[k] < a[j])
swap(a, s++, k);
swap(a, s, j);
qsort(a, i, s-1);
i = s + 1;
}
}
}
2013-08-03
展开全部
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Lottery {
private static List<Integer> getRandom(Integer count) {
List<Integer> temp = new ArrayList<Integer>(), result = new ArrayList<Integer>();
for (int i = 1; i <= count; i++) {
temp.add(i);
}
for (int i = 1, length = temp.size(); i <= length; i++) {
int index = new Random().nextInt(temp.size());
result.add(temp.get(index));
temp.remove(index);
}
return result;
}
public static void main(String [] args) {
List<Integer> nums = getRandom(36);
for(int i=0;i<7;i++) {
System.out.print("("+nums.get(i)+") ");
}
}
}
import java.util.List;
import java.util.Random;
public class Lottery {
private static List<Integer> getRandom(Integer count) {
List<Integer> temp = new ArrayList<Integer>(), result = new ArrayList<Integer>();
for (int i = 1; i <= count; i++) {
temp.add(i);
}
for (int i = 1, length = temp.size(); i <= length; i++) {
int index = new Random().nextInt(temp.size());
result.add(temp.get(index));
temp.remove(index);
}
return result;
}
public static void main(String [] args) {
List<Integer> nums = getRandom(36);
for(int i=0;i<7;i++) {
System.out.print("("+nums.get(i)+") ");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-03
展开全部
public class Choose36_7 {
public static boolean[] state;
public static void doChoose(){
doClean();
int i = 0;
int temp;
while(i<7){
temp = (int) (Math.random()*36+1);
if(state[temp]==false){
state[temp]=true;
System.out.print(temp+" ");
i++;
}
}
}
private static void doClean(){
state = new boolean[37];
}
public static void main(String[] args){
doChoose();
}
}
public static boolean[] state;
public static void doChoose(){
doClean();
int i = 0;
int temp;
while(i<7){
temp = (int) (Math.random()*36+1);
if(state[temp]==false){
state[temp]=true;
System.out.print(temp+" ");
i++;
}
}
}
private static void doClean(){
state = new boolean[37];
}
public static void main(String[] args){
doChoose();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询