「javam报数」java数据上报方案
本篇文章给大家谈谈javam报数,以及java数据上报方案对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
n个小孩围成一个圈,从一开始数,数到七的人出列,求依次出列的次序. 求用java写
这个问题是很经典的编程问题,叫约瑟夫环问题,我之前有写程序,所以直接拷给你吧,我初始的n值为13,你也可以自己修改
//YueSeFu.java
public class YueSeFu {
public static void main(String[] args)
{
final int n=13,s=1,m=5;//n为总人数,从第1个人开始报数,报数到m的出圈
int[] p=new int[n];//数组p用于标记已出圈的人
int[] q=new int[n];//数组q存放出队的顺序
int i,j,k,N=0;
k=s-2;//k从1开始数出圈人的下标
for(i=1;i=n;i++)
{
for(j=1;j=m;j++)//从1到m报数,计算出圈人的下标k
{
if(k==n-1)//当出圈人的下标达到末尾时
{
k=0;//出圈人的下标从0开始
}
else
k++;//否则下标+1
if(p[k]==1)//若平p[k]=1,说明下标为k的人已出圈
{
j--;//由于让过已出圈的人,所以j要-1,以保证每次数过m个人
}
}
p[k]=1;//将下标为k的数组元素置1,表示出圈
q[N++]=k+1;//将下标为k的人的编号k+1,存入数组元素q[N]中
}
System.out.println("出圈顺序为:");
for(i=0;in;i++)
{
System.out.print(q[i]+" ");
}
}
}
JAVA新手求救
源代码如下:
**************************************************
Person类(Person.java)如下:
public class Person {
String name;
Integer password;
public Person(String name, Integer password) {
this.name = name;
this.password = password;
}
}
**************************************************
主类(Circle.java)如下:
import java.util.ArrayList;
public class Circle {
ArrayListPerson arr = new ArrayListPerson();
public boolean isEmpty() {
return arr.size() == 0 ? true : false;
}
public void out() {
ArrayListPerson outarr = new ArrayListPerson();
int removeId = 0;
int nextPwd = arr.get(0).password;
while (!arr.isEmpty()) {
removeId = ((removeId + nextPwd - 1 = arr.size()) ? ((removeId + nextPwd - 1) % (arr.size())) : (removeId + nextPwd - 1));
nextPwd = arr.get(removeId).password;
outarr.add(arr.get(removeId));
arr.remove(removeId);
}
System.out.println("\n出圈顺序如下:");
for (int i = 0; i outarr.size(); i++) {
System.out.println("Name: " + outarr.get(i).name + ", Password: "
+ outarr.get(i).password);
}
}
public static void main(String[] args) {
Circle c = new Circle();
for (int i = 1; i = 20; i++) {
Integer pwd = (int) Math.round(Math.random() * 4 + 1);
Person person = new Person("person" + i, pwd);
c.arr.add(person);
}
System.out.println("初始值如下:");
for(int i = 0; i c.arr.size(); i++) {
System.out.println("Name: " + c.arr.get(i).name + ", Password: " + c.arr.get(i).password);
}
c.out();
}
}
**************************************************
运行结果如下:
初始值如下:
Name: person1, Password: 4
Name: person2, Password: 4
Name: person3, Password: 4
Name: person4, Password: 2
Name: person5, Password: 4
Name: person6, Password: 4
Name: person7, Password: 2
Name: person8, Password: 4
Name: person9, Password: 1
Name: person10, Password: 2
Name: person11, Password: 4
Name: person12, Password: 4
Name: person13, Password: 1
Name: person14, Password: 4
Name: person15, Password: 4
Name: person16, Password: 3
Name: person17, Password: 5
Name: person18, Password: 5
Name: person19, Password: 1
Name: person20, Password: 5
出圈顺序如下:
Name: person4, Password: 2
Name: person6, Password: 4
Name: person10, Password: 2
Name: person12, Password: 4
Name: person16, Password: 3
Name: person19, Password: 1
Name: person20, Password: 5
Name: person7, Password: 2
Name: person9, Password: 1
Name: person11, Password: 4
Name: person17, Password: 5
Name: person5, Password: 4
Name: person15, Password: 4
Name: person3, Password: 4
Name: person18, Password: 5
Name: person14, Password: 4
Name: person13, Password: 1
Name: person1, Password: 4
Name: person8, Password: 4
Name: person2, Password: 4
**************************************************
注:因初始值随机产生,运行结果不一定相同。
求解约瑟夫环问题(Java)
package 约瑟夫环;
import java.util.LinkedList;
import java.util.List;
/**
* 约瑟夫环问题的一种描述是:编号为1.2.3…….n的n个人按顺时针方向围坐一圈 ,每人手持一个密码(正整数),
* 开始任意选一个整数作为报数上限值,从第一个人开始顺时针自1开始顺序报数,报到m时停止报数。报m的人出列,
* 将他的密码作为新的m值,从他顺时针下一个人开始重新从1开始报数,
* 如此下去直到所有的人全部都出列为止。试设计程序实现,按照出列的顺序打印各人的编号。
* @author Administrator
*
*/
public class Question2 {
class person {
int password;
int number;
int state = 1;
public person(int password, int number) {
this.password = password;
this.number = number;
}
public person(int number){
this.number = number;
}
}
public int ListLength(Listperson list) {
int count = 0;
if (list != null) {
for (person p : list) {
if (p.state != 0) {
count++;
}
}
}
return count;
}
public void cacle() {
// 初始化数据
Listperson list = new LinkedListperson();
list.add(new person(3,1));
list.add(new person(1,2));
list.add(new person(7,3));
list.add(new person(2,4));
list.add(new person(4,5));
list.add(new person(8,6));
list.add(new person(4,7));
int position = -1;//初始位置
int m = 20; //第一次报多少的人出来
int count = 0;//已经报了多少人
while (ListLength(list) != 0) {
position = (position + 1) % list.size();// 位置定位
if (((person) list.get(position)).state != 0) {
count++;
}
if (count == m) {
person p = list.get(position);
System.out.print(p.number+" ");
p.state = 0;
m = p.password;
list.set(position, p);
count = 0;
}
}
}
public static void main(String[] args) {
Question2 q= new Question2();
q.cacle();
}
}
跟这差不多的。
JAVA 课后题
/*******************1017*******************/
public class S1017 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = Integer.parseInt(in.nextLine().trim());
for (int i = 1; i = count; i++) {
for (int j = 1; j = i; j++) {
System.out.print(j + "*" + i + "=" + (i * j) + " ");
}
System.out.println();
}
}
}
/*******************1018*******************/
public class S1018 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = Integer.parseInt(in.nextLine().trim()) + 1;
int[][] yanghui = new int[count][count];
for (int i = 0; i count; i++) {
for (int j = 0; j i; j++) {
try {
yanghui[i][j] = yanghui[i - 1][j - 1] + yanghui[i - 1][j];
} catch (Throwable e) {
yanghui[i][j] = 1;
}
System.out.print(yanghui[i][j] + " ");
}
System.out.println();
}
}
}
/*******************1019*******************/
public class S1019 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.nextLine().trim();
String[] numStrs = input.split(" ");
int n = Integer.parseInt(numStrs[0]);
int m = Integer.parseInt(numStrs[1]);
ListObject list = new ArrayListObject();
for (int i = 0; i n; i++) {
list.add(i);
}
int count = 0;
while (list.size() != 1) {
count = (count + m) % list.size();
list.remove(count);
count -- ;
}
System.out.println(list.get(0));
}
}
/*******************1020*******************/
public class S1020 {
public static void main(String[] args) {
SetCharacter end = new HashSetCharacter();
int count = 10000;
String str;
char ch;
for (int i = 1; i = count; i++) {
str = String.valueOf(i);
ch = str.charAt(str.length() - 1);
if (end.contains(ch)) {
System.out.println(i);
} else if (isTongGou(i)) {
end.add(ch);
System.out.println(i);
}
}
}
public static boolean isTongGou(int num) {
String str = String.valueOf(num);
char ch = str.charAt(str.length() - 1);
String str2 = String.valueOf(num * num);
char ch2 = str2.charAt(str2.length() - 1);
return ch == ch2;
}
}
/*******************1021*******************/
public class S1021 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = Integer.parseInt(in.nextLine().trim());
int[] counts = new int[count];
for (int i = 0; i counts.length; i++) {
counts[i] = Integer.parseInt(in.nextLine().trim());
}
for (int i = 0; i counts.length; i++) {
print(counts[i]);
System.out.println();
}
}
public static void print(int count) {
for (int i = 1; i = count; i += 2) {
for (int j = 1; j = (5 - i) / 2; j++) {
System.out.print("-");
}
for (int j = 0; j i; j++) {
System.out.print("*");
}
for (int j = 1; j = (5 - i) / 2; j++) {
System.out.print("-");
}
System.out.println();
}
for (int i = count - 2; i 0; i -= 2) {
for (int j = 1; j = (5 - i) / 2; j++) {
System.out.print("-");
}
for (int j = 0; j i; j++) {
System.out.print("*");
}
for (int j = 1; j = (5 - i) / 2; j++) {
System.out.print("-");
}
System.out.println();
}
}
}
/*******************1022*******************/
public class S1022 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = Integer.parseInt(in.nextLine().trim());
int[] counts = new int[count];
for (int i = 0; i counts.length; i++) {
counts[i] = Integer.parseInt(in.nextLine().trim());
}
for (int i = 0; i counts.length; i++) {
System.out.println(fibonacci(counts[i]));
}
}
public static int fibonacci(int n) {
int total = 0;
int[] f = new int[n];
for (int i = 0; i f.length; i++) {
if (i == 0) {
f[i] = 0;
} else {
try {
f[i] = f[i - 2] + f[i - 1];
} catch (Throwable e) {
f[i] = 1;
}
}
total += f[i];
}
return total;
}
}
/*******************1023*******************/
public class S1023 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] input;
int maxCount = -1;
int maxDay = 0;
for (int i = 1; i = 7; i++) {
input = in.nextLine().trim().split(" ");
int temp = Integer.parseInt(input[0]) + Integer.parseInt(input[1]) - 8;
if (temp 0) {
if (temp maxCount) {
maxCount = temp;
maxDay = i;
}
}
}
System.out.println(maxDay);
}
}
关于javam报数和java数据上报方案的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。