(1)有n个盒子摆成一圈,盒子按顺时针方向依次编号为0,1,2,……,n-1。手机随机放在其中一个盒子中。(n为自然数)
(2)从0号盒子开始摸奖,顺时针方向计数,每遇到第m个盒子就摸奖一次。(m为自然数,m<n)
(3)直到重新摸到0号盒子为止。
例如n=5,m=3,那么摸奖经过的盒子编号依次为0,3,1,4,2,0。
请编写一个完整的程序,随机输入n,m(m<n),程序分析手机有没有不被抽中的机会?如果有,概率是多少? (概率=不被抽中的可能数/n)
6、 采用UDP协议,编写一个Java网络应用程序,该应用分服务器端程序和客户端程序两部分。客户端指定一个服务器上的文件名,让服务器发回该文件的内容,或者提示文件不存在。(服务端程序和客户端程序分别命名为Server.java和Client.java)
7 个解决方案
#1
标记一下,等待高手
#2
服了!
#3
也许高手还可以吧
#4
等高手。。
#5
/*5、 某企业为了促销,搞抽奖宣传活动,奖品为新款手机一部,抽奖规则如下:
(1)有n个盒子摆成一圈,盒子按顺时针方向依次编号为0,1,2,……,n-1。手机随机放在其中一个盒
子中。(n为自然数)
(2)从0号盒子开始摸奖,顺时针方向计数,每遇到第m个盒子就摸奖一次。(m为自然数,m <n)
(3)直到重新摸到0号盒子为止。
例如n=5,m=3,那么摸奖经过的盒子编号依次为0,3,1,4,2,0。
请编写一个完整的程序,随机输入n,m(m <n),程序分析手机有没有不被抽中的机会?如果有,概率是
多少? (概率=不被抽中的可能数/n)(30分)*/
import java.util.Iterator;
import java.util.Scanner;
class Circle {
private Entry header = null;
private int size = 0;
public Circle(int capacity) {
for (int i = 0; i < capacity; ++i)
add();
}
private void add() {
Entry newEntry = new Entry(size++);
if (header == null) {
header = newEntry;
header.previous = header.next = header;
} else {
header.previous.next = newEntry;
newEntry.previous = header.previous;
header.previous = newEntry;
newEntry.next = header;
}
}
public int size() {
return size;
}
public Iterator<Integer> circleIterator() {
return new CircleIterator(header);
}
private class CircleIterator implements Iterator<Integer> {
private Entry current;
CircleIterator(Entry start) {
current = start;
}
public boolean hasNext() {
return current != null;
}
public Integer next() {
if (hasNext()) {
int label = current.label;
current = current.next;
return label;
}
return null;
}
public void remove() {
if (current != null) {
Entry e = current.previous;
if (e == header)
header = e.next;
e.next.previous = e.previous;
e.previous.next = e.next;
--size;
}
}
}
private static class Entry {
int label;
Entry previous, next;
Entry(int label) {
this.label = label;
}
}
}
public class TestQiye {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter m and n");
int n = Integer.parseInt(scanner.next()), m = Integer.parseInt(scanner.next());
int count = 0;
Circle circle = new Circle(n);
Iterator<Integer> iterator = circle.circleIterator();
iterator.next();
do {
for (int i = 1; i < m; ++i)
iterator.next();
++count;
} while (iterator.next().intValue() != 0);
System.out.println(((double) (n-count)) / n);
}
}
(1)有n个盒子摆成一圈,盒子按顺时针方向依次编号为0,1,2,……,n-1。手机随机放在其中一个盒
子中。(n为自然数)
(2)从0号盒子开始摸奖,顺时针方向计数,每遇到第m个盒子就摸奖一次。(m为自然数,m <n)
(3)直到重新摸到0号盒子为止。
例如n=5,m=3,那么摸奖经过的盒子编号依次为0,3,1,4,2,0。
请编写一个完整的程序,随机输入n,m(m <n),程序分析手机有没有不被抽中的机会?如果有,概率是
多少? (概率=不被抽中的可能数/n)(30分)*/
import java.util.Iterator;
import java.util.Scanner;
class Circle {
private Entry header = null;
private int size = 0;
public Circle(int capacity) {
for (int i = 0; i < capacity; ++i)
add();
}
private void add() {
Entry newEntry = new Entry(size++);
if (header == null) {
header = newEntry;
header.previous = header.next = header;
} else {
header.previous.next = newEntry;
newEntry.previous = header.previous;
header.previous = newEntry;
newEntry.next = header;
}
}
public int size() {
return size;
}
public Iterator<Integer> circleIterator() {
return new CircleIterator(header);
}
private class CircleIterator implements Iterator<Integer> {
private Entry current;
CircleIterator(Entry start) {
current = start;
}
public boolean hasNext() {
return current != null;
}
public Integer next() {
if (hasNext()) {
int label = current.label;
current = current.next;
return label;
}
return null;
}
public void remove() {
if (current != null) {
Entry e = current.previous;
if (e == header)
header = e.next;
e.next.previous = e.previous;
e.previous.next = e.next;
--size;
}
}
}
private static class Entry {
int label;
Entry previous, next;
Entry(int label) {
this.label = label;
}
}
}
public class TestQiye {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter m and n");
int n = Integer.parseInt(scanner.next()), m = Integer.parseInt(scanner.next());
int count = 0;
Circle circle = new Circle(n);
Iterator<Integer> iterator = circle.circleIterator();
iterator.next();
do {
for (int i = 1; i < m; ++i)
iterator.next();
++count;
} while (iterator.next().intValue() != 0);
System.out.println(((double) (n-count)) / n);
}
}
#6
先收藏了
#7
import java.util.*;
import java.io.*;
public class LinkedListTest
{
private static LinkedList<Integer> list = new LinkedList<Integer>();
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = -1;
int m = -1;
try
{
n = sc.nextInt();
m = sc.nextInt();
System.out.println("\n没摸到奖的概率是:"+circle(n,m));
}
catch(Exception e)
{
System.out.println("请输入两个整数");
}
}
public static double circle(int n , int m)
{
int count = 0;
double result = 0;
for(int i = 0; i < n; i ++)
{
list.add(i);
}
Object [] box;
box = list.toArray();
int tmp = -1;
int k = 0;
System.out.print(0 + ",");
while(tmp != 0)
{
k += m;
if(k >= box.length)
{
k -= box.length;
}
tmp = (Integer)box[k];
System.out.print(tmp + ",");
count ++;
}
result = (double)(n - count)/n;
return result;
}
}
#1
标记一下,等待高手
#2
服了!
#3
也许高手还可以吧
#4
等高手。。
#5
/*5、 某企业为了促销,搞抽奖宣传活动,奖品为新款手机一部,抽奖规则如下:
(1)有n个盒子摆成一圈,盒子按顺时针方向依次编号为0,1,2,……,n-1。手机随机放在其中一个盒
子中。(n为自然数)
(2)从0号盒子开始摸奖,顺时针方向计数,每遇到第m个盒子就摸奖一次。(m为自然数,m <n)
(3)直到重新摸到0号盒子为止。
例如n=5,m=3,那么摸奖经过的盒子编号依次为0,3,1,4,2,0。
请编写一个完整的程序,随机输入n,m(m <n),程序分析手机有没有不被抽中的机会?如果有,概率是
多少? (概率=不被抽中的可能数/n)(30分)*/
import java.util.Iterator;
import java.util.Scanner;
class Circle {
private Entry header = null;
private int size = 0;
public Circle(int capacity) {
for (int i = 0; i < capacity; ++i)
add();
}
private void add() {
Entry newEntry = new Entry(size++);
if (header == null) {
header = newEntry;
header.previous = header.next = header;
} else {
header.previous.next = newEntry;
newEntry.previous = header.previous;
header.previous = newEntry;
newEntry.next = header;
}
}
public int size() {
return size;
}
public Iterator<Integer> circleIterator() {
return new CircleIterator(header);
}
private class CircleIterator implements Iterator<Integer> {
private Entry current;
CircleIterator(Entry start) {
current = start;
}
public boolean hasNext() {
return current != null;
}
public Integer next() {
if (hasNext()) {
int label = current.label;
current = current.next;
return label;
}
return null;
}
public void remove() {
if (current != null) {
Entry e = current.previous;
if (e == header)
header = e.next;
e.next.previous = e.previous;
e.previous.next = e.next;
--size;
}
}
}
private static class Entry {
int label;
Entry previous, next;
Entry(int label) {
this.label = label;
}
}
}
public class TestQiye {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter m and n");
int n = Integer.parseInt(scanner.next()), m = Integer.parseInt(scanner.next());
int count = 0;
Circle circle = new Circle(n);
Iterator<Integer> iterator = circle.circleIterator();
iterator.next();
do {
for (int i = 1; i < m; ++i)
iterator.next();
++count;
} while (iterator.next().intValue() != 0);
System.out.println(((double) (n-count)) / n);
}
}
(1)有n个盒子摆成一圈,盒子按顺时针方向依次编号为0,1,2,……,n-1。手机随机放在其中一个盒
子中。(n为自然数)
(2)从0号盒子开始摸奖,顺时针方向计数,每遇到第m个盒子就摸奖一次。(m为自然数,m <n)
(3)直到重新摸到0号盒子为止。
例如n=5,m=3,那么摸奖经过的盒子编号依次为0,3,1,4,2,0。
请编写一个完整的程序,随机输入n,m(m <n),程序分析手机有没有不被抽中的机会?如果有,概率是
多少? (概率=不被抽中的可能数/n)(30分)*/
import java.util.Iterator;
import java.util.Scanner;
class Circle {
private Entry header = null;
private int size = 0;
public Circle(int capacity) {
for (int i = 0; i < capacity; ++i)
add();
}
private void add() {
Entry newEntry = new Entry(size++);
if (header == null) {
header = newEntry;
header.previous = header.next = header;
} else {
header.previous.next = newEntry;
newEntry.previous = header.previous;
header.previous = newEntry;
newEntry.next = header;
}
}
public int size() {
return size;
}
public Iterator<Integer> circleIterator() {
return new CircleIterator(header);
}
private class CircleIterator implements Iterator<Integer> {
private Entry current;
CircleIterator(Entry start) {
current = start;
}
public boolean hasNext() {
return current != null;
}
public Integer next() {
if (hasNext()) {
int label = current.label;
current = current.next;
return label;
}
return null;
}
public void remove() {
if (current != null) {
Entry e = current.previous;
if (e == header)
header = e.next;
e.next.previous = e.previous;
e.previous.next = e.next;
--size;
}
}
}
private static class Entry {
int label;
Entry previous, next;
Entry(int label) {
this.label = label;
}
}
}
public class TestQiye {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter m and n");
int n = Integer.parseInt(scanner.next()), m = Integer.parseInt(scanner.next());
int count = 0;
Circle circle = new Circle(n);
Iterator<Integer> iterator = circle.circleIterator();
iterator.next();
do {
for (int i = 1; i < m; ++i)
iterator.next();
++count;
} while (iterator.next().intValue() != 0);
System.out.println(((double) (n-count)) / n);
}
}
#6
先收藏了
#7
import java.util.*;
import java.io.*;
public class LinkedListTest
{
private static LinkedList<Integer> list = new LinkedList<Integer>();
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = -1;
int m = -1;
try
{
n = sc.nextInt();
m = sc.nextInt();
System.out.println("\n没摸到奖的概率是:"+circle(n,m));
}
catch(Exception e)
{
System.out.println("请输入两个整数");
}
}
public static double circle(int n , int m)
{
int count = 0;
double result = 0;
for(int i = 0; i < n; i ++)
{
list.add(i);
}
Object [] box;
box = list.toArray();
int tmp = -1;
int k = 0;
System.out.print(0 + ",");
while(tmp != 0)
{
k += m;
if(k >= box.length)
{
k -= box.length;
}
tmp = (Integer)box[k];
System.out.print(tmp + ",");
count ++;
}
result = (double)(n - count)/n;
return result;
}
}